-
Notifications
You must be signed in to change notification settings - Fork 8
/
MainPage.tsx
350 lines (318 loc) · 9.53 KB
/
MainPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React, { useEffect, useState } from 'react';
import { StatusBar, StyleSheet, View, TouchableOpacity, Image, Text } from 'react-native';
import { launchCamera, launchImageLibrary } from 'react-native-image-picker';
import { FaceRecognitionSdkView, FaceSDKModule } from 'face-recognition-sdk';
import SQLite from 'react-native-sqlite-storage';
import PersonView from './PersonView';
import Person from './Person';
const MainPage = ({ navigation }) => {
const [persons, setPersons] = useState([]);
useEffect(() => {
// This function will be executed after the component is mounted
const initSDKAsync = async () => {
try {
var ret = await FaceSDKModule.setActivation("BePdghcCJ6VWDMNYaYHLMJ3QhslLDlJHwq52hSiPswhbF42izz+0eE4qo6v9v0bY4QK0RwsZuqe1" +
"5Ea18VOoUwIVR3pP6ozY2xQbbKF/RAgQ8pMy2pqSslCULMcj3yRJz72XRbuDWDO5NpbavHCP5aIT" +
"BiAjgaVYgGrHbl/iqb4PP2O8cjRd7QmHj1Mye1vQWeQobzUaWtH4/2rF5X/BQKiz8qL/E7jccC6h" +
"6SsyEfxZUyZFiPSv/XKRClM4mxCJiWmIEaEvJpn0HTmqqIt8dGkhE/ptQjyi3d83uzna7yDRRe+j" +
"8fRYqeJlI7hgrMXU5We7xp1w5W0akIikG2Myrg==");
console.log("set activation:", ret);
ret = await FaceSDKModule.initSDK();
console.log("init sdk:", ret);
} catch (error) {
console.error("Error setting activation:", error);
}
};
const createDB = () => {
// Create or open the database
const db = SQLite.openDatabase({ name: 'person1.db', location: 'default' }, () => { }, errorCB);
// Create the table
db.transaction((tx) => {
tx.executeSql(
'CREATE TABLE person(name text, faceJpg text, templates text)',
[],
() => console.log('Table created successfully'),
(error) => console.log('Error creating table:', error)
);
});
}
initSDKAsync();
createDB();
loadAllPersons();
// Return a cleanup function to unsubscribe or perform cleanup
return () => {
// console.log('Component unmounted');
};
}, []); // The empty dependency array ensures the effect runs only once
const errorCB = (err) => {
console.log("SQL Error: " + err);
};
const loadAllPersons = async () => {
try {
const db = SQLite.openDatabase({ name: 'person1.db', location: 'default' }, () => { }, errorCB);
await db.transaction((tx) => {
tx.executeSql(
'SELECT * FROM person',
[],
(_, result) => {
const rows = result.rows;
const persons = [];
for (let i = 0; i < rows.length; i++) {
const row = rows.item(i);
console.log("person: ", row.name);
persons.push({
name: row.name,
faceJpg: row.faceJpg,
templates: row.templates,
});
}
setPersons(persons);
},
(error) => {
console.log('Error loading persons:', error);
}
);
});
} catch (error) {
console.log('Error opening database:', error);
}
};
const insertPerson = async (person) => {
try {
const db = SQLite.openDatabase({ name: 'person1.db', location: 'default' }, () => { }, errorCB);
await db.transaction((tx) => {
tx.executeSql(
'INSERT INTO person (name, faceJpg, templates) VALUES (?, ?, ?)',
[person.name, person.faceJpg, person.templates],
(_, result) => {
setPersons([...persons, person]);
console.log('Person inserted successfully1');
},
(error) => {
console.log('Error inserting person:', error);
}
);
});
} catch (error) {
console.log('Error opening database:', error);
}
}
const deletePerson = async (index) => {
try {
const db = SQLite.openDatabase({ name: 'person1.db', location: 'default' }, () => { }, errorCB);
const person = persons[index];
await db.transaction((tx) => {
tx.executeSql(
'DELETE FROM person WHERE name = ?',
[person.name],
(_, result) => {
console.log('Person deleted successfully');
const updatedPersons = [...persons];
updatedPersons.splice(index, 1);
setPersons(updatedPersons);
},
(error) => {
console.log('Error deleting person:', error);
}
);
});
} catch (error) {
console.log('Error opening database:', error);
}
}
const registerPerson = async (uri) => {
var faceBoxes = await FaceSDKModule.extractFaces(uri);
faceBoxes.forEach((face) => {
const randomNumber = 10000 + Math.floor(Math.random() * 10000); // Generate a random number between 10000 and 19999
const person = {
name: 'Person' + randomNumber.toString(),
faceJpg: face.faceJpg,
templates: face.templates,
};
insertPerson(person)
});
};
const enrollPerson = async () => {
var options = {
mediaType: 'photo'
};
launchImageLibrary(options, (response) => {
if (response.didCancel) {
return;
}
if (response.errorCode) {
console.log('Error code:', response.errorCode);
return;
}
console.log('Selected image:', response.assets[0]?.uri);
registerPerson(response.assets[0]?.uri);
});
};
const goToFaceRecognition = () => {
// Implement navigation logic to FaceRecognitionView here
navigation.navigate('FaceRecognition', {persons});
};
const goToSettings = () => {
// Implement navigation logic to SettingsPage here
navigation.navigate('SettingsPage');
};
const goToAbout = () => {
navigation.navigate('About');
};
return (
<View style={styles.container}>
<StatusBar barStyle="light-content" backgroundColor="#1C1B1F" />
<View style={styles.appBar}>
<Text style={styles.appBarTitle}>Face Recognition</Text>
</View>
<View style={styles.body}>
<View style={styles.card}>
<Image
source={require('./assets/ic_sparkles.png')}
style={styles.cardIcon}
/>
<Text style={styles.cardText}>
We offer SDKs for Face Recognition, Liveness Detection, and ID Card Recognition.
</Text>
</View>
<View style={{ height: 12 }} />
<View style={styles.buttonsRow}>
<TouchableOpacity
style={styles.button}
onPress={enrollPerson}
>
<Image
source={require('./assets/person_search_icon.png')}
style={styles.buttonIcon}
/>
<Text style={styles.buttonText}>Enroll</Text>
</TouchableOpacity>
<View style={{ width: 16 }} />
<TouchableOpacity
style={styles.button}
onPress={goToFaceRecognition}
>
<Image
source={require('./assets/person_search_icon.png')}
style={styles.buttonIcon}
/>
<Text style={styles.buttonText}>Identify</Text>
</TouchableOpacity>
</View>
<View style={styles.buttonsRow}>
<TouchableOpacity
style={styles.button}
onPress={goToSettings}
>
<Image
source={require('./assets/settings_icon.png')}
style={styles.buttonIcon}
/>
<Text style={styles.buttonText}>Settings</Text>
</TouchableOpacity>
<View style={{ width: 16 }} />
<TouchableOpacity
style={styles.button}
onPress={goToAbout}
>
<Image
source={require('./assets/info_icon.png')}
style={styles.buttonIcon}
/>
<Text style={styles.buttonText}>About</Text>
</TouchableOpacity>
</View>
<View style={styles.personView}>
<PersonView personList={persons} deletePerson={deletePerson} />
</View>
</View>
<View style={styles.footer}>
<Image
source={require('./assets/ic_kby.png')}
style={styles.footerIcon}
/>
</View>
<View style={{ height: 16 }} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#1C1B1F',
},
appBar: {
height: 70,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1C1B1F',
},
appBarTitle: {
fontSize: 18,
color: '#FFFFFF',
},
body: {
marginTop: 8,
flex: 1,
marginHorizontal: 16,
},
card: {
backgroundColor: '#49454F',
borderRadius: 12,
padding: 10,
flexDirection: 'row',
alignItems: 'center',
},
cardIcon: {
width: 24,
height: 24,
marginRight: 10,
},
cardText: {
fontSize: 14,
color: '#FFFFFF',
},
buttonsRow: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 12,
},
button: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#4F378B',
borderRadius: 12,
paddingVertical: 10,
},
buttonIcon: {
width: 24,
height: 24,
marginRight: 4,
},
buttonText: {
fontSize: 15,
color: '#FFFFFF',
},
personView: {
flex: 1,
// Implement styles for PersonView component here
},
footer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 4,
},
footerIcon: {
width: 200,
height: 28,
resizeMode: 'contain'
},
footerText: {
fontSize: 18,
color: '#3C3C3C',
},
});
export default MainPage;