react-native-face-match-sdk 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,18 @@ npm install react-native-face-match-sdk
|
|
|
24
24
|
npm install ../react-native-face-match-sdk
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
### Dependencies
|
|
28
|
+
|
|
29
|
+
| Package | Needed for |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| [`react-native-fs`](https://github.com/itinance/react-native-fs) | Saving a base64 reference photo to a file. The SDK reads image files, not base64 strings, so install it if your reference photo comes as base64 (for example from an API). The [full example](#full-example-verify-a-live-selfie-against-a-base64-photo) uses it. |
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install react-native-fs
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
On iOS, run `pod install` again after installing it (see [iOS](#ios)).
|
|
38
|
+
|
|
27
39
|
### Installing from a local folder
|
|
28
40
|
|
|
29
41
|
`npm install ../react-native-face-match-sdk` creates a symlink, and Metro does not
|
|
@@ -77,7 +89,13 @@ the app at startup.
|
|
|
77
89
|
pod 'onnxruntime-c', :podspec => '../node_modules/react-native-face-match-sdk/ios/onnxruntime-c.podspec'
|
|
78
90
|
```
|
|
79
91
|
|
|
80
|
-
3.
|
|
92
|
+
3. Add the camera permission text to `ios/<App>/Info.plist`, inside the top
|
|
93
|
+
`<dict>`. iOS closes the app when the camera opens without it:
|
|
94
|
+
|
|
95
|
+
```xml
|
|
96
|
+
<key>NSCameraUsageDescription</key>
|
|
97
|
+
<string>Camera is used to verify your face.</string>
|
|
98
|
+
```
|
|
81
99
|
|
|
82
100
|
4. Install pods:
|
|
83
101
|
|
|
@@ -85,6 +103,21 @@ the app at startup.
|
|
|
85
103
|
cd ios && pod install
|
|
86
104
|
```
|
|
87
105
|
|
|
106
|
+
The relevant part of `ios/Podfile` then looks like this:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
platform :ios, '15.5'
|
|
110
|
+
|
|
111
|
+
target 'YourApp' do
|
|
112
|
+
config = use_native_modules!
|
|
113
|
+
use_react_native!(:path => config[:reactNativePath])
|
|
114
|
+
|
|
115
|
+
pod 'onnxruntime-c', :podspec => '../node_modules/react-native-face-match-sdk/ios/onnxruntime-c.podspec'
|
|
116
|
+
|
|
117
|
+
# ... rest of the target (post_install etc.)
|
|
118
|
+
end
|
|
119
|
+
```
|
|
120
|
+
|
|
88
121
|
The app's other pods must also target iOS 15.5 or higher. If `pod install`
|
|
89
122
|
warns about a lower deployment target, raise it in the Podfile's
|
|
90
123
|
`post_install` block.
|
|
@@ -149,6 +182,114 @@ photo so it does not stay on the device.
|
|
|
149
182
|
The similarity score is calibrated to 0-100. 80 and above is a match and 65-79 is
|
|
150
183
|
a possible match.
|
|
151
184
|
|
|
185
|
+
### Full example: verify a live selfie against a base64 photo
|
|
186
|
+
|
|
187
|
+
This screen asks for the camera permission, runs the liveness check, captures a
|
|
188
|
+
selfie automatically, and compares it with a reference photo given as base64.
|
|
189
|
+
It needs [`react-native-fs`](#dependencies).
|
|
190
|
+
|
|
191
|
+
```tsx
|
|
192
|
+
import React, {useRef, useState} from 'react';
|
|
193
|
+
import {
|
|
194
|
+
Button,
|
|
195
|
+
PermissionsAndroid,
|
|
196
|
+
Platform,
|
|
197
|
+
StyleSheet,
|
|
198
|
+
Text,
|
|
199
|
+
View,
|
|
200
|
+
} from 'react-native';
|
|
201
|
+
import {CachesDirectoryPath, writeFile} from 'react-native-fs';
|
|
202
|
+
import {
|
|
203
|
+
FaceCamera,
|
|
204
|
+
FaceMatch,
|
|
205
|
+
type CaptureResult,
|
|
206
|
+
type FaceCameraHandle,
|
|
207
|
+
} from 'react-native-face-match-sdk';
|
|
208
|
+
|
|
209
|
+
const REFERENCE_BASE64 = ''; // PASTE_YOUR_BASE64_STRING_HERE
|
|
210
|
+
|
|
211
|
+
// The SDK reads image files, not base64, so save the base64 to the cache folder.
|
|
212
|
+
async function base64ToFile(base64: string): Promise<string> {
|
|
213
|
+
const path = `${CachesDirectoryPath}/reference.jpg`;
|
|
214
|
+
await writeFile(path, base64.replace(/^data:image\/\w+;base64,/, ''), 'base64');
|
|
215
|
+
return `file://${path}`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export default function App() {
|
|
219
|
+
const camera = useRef<FaceCameraHandle>(null);
|
|
220
|
+
const [scanning, setScanning] = useState(false);
|
|
221
|
+
const [status, setStatus] = useState('Tap "Start" to verify your face');
|
|
222
|
+
|
|
223
|
+
const start = async () => {
|
|
224
|
+
if (!REFERENCE_BASE64.trim()) {
|
|
225
|
+
return setStatus('Please add your reference image (REFERENCE_BASE64) first');
|
|
226
|
+
}
|
|
227
|
+
if (Platform.OS === 'android') {
|
|
228
|
+
const granted = await PermissionsAndroid.request(
|
|
229
|
+
PermissionsAndroid.PERMISSIONS.CAMERA,
|
|
230
|
+
);
|
|
231
|
+
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
|
|
232
|
+
return setStatus('Camera permission denied');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
setScanning(true);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const onCapture = async (captured: CaptureResult) => {
|
|
239
|
+
setScanning(false);
|
|
240
|
+
setStatus('Comparing…');
|
|
241
|
+
try {
|
|
242
|
+
// 1. Embedding of the reference (base64) photo
|
|
243
|
+
const uri = await base64ToFile(REFERENCE_BASE64);
|
|
244
|
+
const reference = await FaceMatch.generateReferenceEmbedding(uri);
|
|
245
|
+
|
|
246
|
+
// 2. Compare it with the embedding of the live selfie
|
|
247
|
+
const result = await FaceMatch.compareEmbeddings(
|
|
248
|
+
reference.embedding,
|
|
249
|
+
captured.embedding,
|
|
250
|
+
);
|
|
251
|
+
setStatus(`${result.label}\nScore: ${result.similarityScore.toFixed(1)} / 100`);
|
|
252
|
+
} catch (e: any) {
|
|
253
|
+
setStatus(`Error: ${e.message}`);
|
|
254
|
+
} finally {
|
|
255
|
+
FaceMatch.discardCapture(captured.uri); // delete the selfie from the device
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
return (
|
|
260
|
+
<View style={styles.container}>
|
|
261
|
+
{scanning && (
|
|
262
|
+
<FaceCamera
|
|
263
|
+
ref={camera}
|
|
264
|
+
style={styles.camera}
|
|
265
|
+
autoStart
|
|
266
|
+
requireLiveness
|
|
267
|
+
autoCaptureOnLiveness
|
|
268
|
+
voiceLanguage="en"
|
|
269
|
+
onCameraState={e => {
|
|
270
|
+
if (e.nativeEvent.state === 'started') camera.current?.startLiveness();
|
|
271
|
+
}}
|
|
272
|
+
onLiveness={e => setStatus(e.nativeEvent.instruction)}
|
|
273
|
+
onCapture={e => onCapture(e.nativeEvent)}
|
|
274
|
+
onError={e => setStatus(`Error: ${e.nativeEvent.message}`)}
|
|
275
|
+
/>
|
|
276
|
+
)}
|
|
277
|
+
<Text style={styles.status}>{status}</Text>
|
|
278
|
+
{!scanning && <Button title="Start" onPress={start} />}
|
|
279
|
+
</View>
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const styles = StyleSheet.create({
|
|
284
|
+
container: {flex: 1, alignItems: 'center', justifyContent: 'center'},
|
|
285
|
+
camera: {width: 300, height: 400},
|
|
286
|
+
status: {fontSize: 18, textAlign: 'center', margin: 20},
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
On iOS the camera permission prompt comes up by itself the first time the
|
|
291
|
+
camera opens, using the `NSCameraUsageDescription` text from `Info.plist`.
|
|
292
|
+
|
|
152
293
|
## What is in the package
|
|
153
294
|
|
|
154
295
|
```
|
package/package.json
CHANGED