react-native-vision-camera-spoof-detector 1.2.1 → 1.2.5
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 +12 -3
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofingAdvanced.kt +18 -2
- package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorFactory.kt +4 -0
- package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +404 -214
- package/index.d.ts +14 -0
- package/index.js +50 -3
- package/ios/HybridSpoofDetectorOutput.swift +186 -2
- package/nitrogen/generated/android/VisionCameraSpoofDetectorOnLoad.cpp +4 -0
- package/nitrogen/generated/android/c++/JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult.hpp +80 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SpoofDetectedFace_.hpp +98 -0
- package/nitrogen/generated/android/c++/JHybridSpoofDetectorFactorySpec.cpp +15 -0
- package/nitrogen/generated/android/c++/JHybridSpoofDetectorFactorySpec.hpp +1 -0
- package/nitrogen/generated/android/c++/JSpoofDetectedFace.hpp +58 -0
- package/nitrogen/generated/android/c++/JSpoofDetectedFaceBounds.hpp +69 -0
- package/nitrogen/generated/android/c++/JSpoofDetectorOutputOptions.hpp +56 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/Func_void_SpoofDetectedFaceBounds_AntiSpoofingResult.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/Func_void_std__vector_SpoofDetectedFace_.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorFactorySpec.kt +4 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectedFace.kt +51 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectedFaceBounds.kt +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectorOutputOptions.kt +44 -4
- package/nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Bridge.cpp +16 -0
- package/nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Bridge.hpp +116 -0
- package/nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Umbrella.hpp +7 -0
- package/nitrogen/generated/ios/c++/HybridSpoofDetectorFactorySpecSwift.hpp +13 -0
- package/nitrogen/generated/ios/swift/Func_void_SpoofDetectedFaceBounds_AntiSpoofingResult.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__vector_SpoofDetectedFace_.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridSpoofDetectorFactorySpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridSpoofDetectorFactorySpec_cxx.swift +15 -0
- package/nitrogen/generated/ios/swift/SpoofDetectedFace.swift +29 -0
- package/nitrogen/generated/ios/swift/SpoofDetectedFaceBounds.swift +44 -0
- package/nitrogen/generated/ios/swift/SpoofDetectorOutputOptions.swift +167 -1
- package/nitrogen/generated/shared/c++/HybridSpoofDetectorFactorySpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridSpoofDetectorFactorySpec.hpp +1 -0
- package/nitrogen/generated/shared/c++/SpoofDetectedFace.hpp +84 -0
- package/nitrogen/generated/shared/c++/SpoofDetectedFaceBounds.hpp +95 -0
- package/nitrogen/generated/shared/c++/SpoofDetectorOutputOptions.hpp +41 -2
- package/package.json +1 -1
- package/src/specs/SpoofDetectorFactory.nitro.ts +47 -24
package/index.js
CHANGED
|
@@ -3,29 +3,76 @@ import { NativeModules } from 'react-native';
|
|
|
3
3
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
4
|
|
|
5
5
|
const { FaceAntiSpoof } = NativeModules;
|
|
6
|
+
let sharedSpoofDetectorFactory;
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
const getSpoofDetectorFactory = () => {
|
|
9
|
+
if (!sharedSpoofDetectorFactory) {
|
|
10
|
+
sharedSpoofDetectorFactory = NitroModules.createHybridObject('SpoofDetectorFactory');
|
|
11
|
+
}
|
|
12
|
+
return sharedSpoofDetectorFactory;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const useFaceAntiSpoofFrameOutput = ({
|
|
16
|
+
onResult,
|
|
17
|
+
onFacesDetected,
|
|
18
|
+
onStableFaceDetected,
|
|
19
|
+
...options
|
|
20
|
+
} = {}) => {
|
|
8
21
|
const resultRef = useRef(onResult);
|
|
9
22
|
resultRef.current = onResult;
|
|
23
|
+
const facesRef = useRef(onFacesDetected);
|
|
24
|
+
facesRef.current = onFacesDetected;
|
|
25
|
+
const stableFaceRef = useRef(onStableFaceDetected);
|
|
26
|
+
stableFaceRef.current = onStableFaceDetected;
|
|
10
27
|
const errorRef = useRef(options.onError);
|
|
11
28
|
errorRef.current = options.onError;
|
|
29
|
+
const hasFaceCallback = typeof onFacesDetected === 'function';
|
|
30
|
+
const hasStableFaceCallback = typeof onStableFaceDetected === 'function';
|
|
12
31
|
const factory = useMemo(
|
|
13
|
-
|
|
32
|
+
getSpoofDetectorFactory,
|
|
14
33
|
[],
|
|
15
34
|
);
|
|
16
35
|
return useMemo(
|
|
17
36
|
() => factory.createSpoofDetectorOutput({
|
|
37
|
+
targetResolution: options.targetResolution,
|
|
38
|
+
antispoofLevel: options.antispoofLevel,
|
|
39
|
+
stableFrames: options.stableFrames,
|
|
40
|
+
requiredLiveFrames: options.requiredLiveFrames,
|
|
41
|
+
laplacianThreshold: options.laplacianThreshold,
|
|
42
|
+
maxNeuralSpoofScore: options.maxNeuralSpoofScore,
|
|
43
|
+
uiUpdateIntervalMs: options.uiUpdateIntervalMs,
|
|
18
44
|
onResult(result) {
|
|
19
45
|
resultRef.current?.(result);
|
|
20
46
|
},
|
|
47
|
+
onFacesDetected: hasFaceCallback
|
|
48
|
+
? (faces) => facesRef.current?.(faces)
|
|
49
|
+
: undefined,
|
|
50
|
+
onStableFaceDetected: hasStableFaceCallback
|
|
51
|
+
? (face, result) => stableFaceRef.current?.(face, result)
|
|
52
|
+
: undefined,
|
|
21
53
|
onError(error) {
|
|
22
54
|
errorRef.current?.(error);
|
|
23
55
|
},
|
|
24
56
|
}),
|
|
25
|
-
[
|
|
57
|
+
[
|
|
58
|
+
factory,
|
|
59
|
+
options.targetResolution,
|
|
60
|
+
options.antispoofLevel,
|
|
61
|
+
options.stableFrames,
|
|
62
|
+
options.requiredLiveFrames,
|
|
63
|
+
options.laplacianThreshold,
|
|
64
|
+
options.maxNeuralSpoofScore,
|
|
65
|
+
options.uiUpdateIntervalMs,
|
|
66
|
+
hasFaceCallback,
|
|
67
|
+
hasStableFaceCallback,
|
|
68
|
+
],
|
|
26
69
|
);
|
|
27
70
|
};
|
|
28
71
|
|
|
72
|
+
export const resetFaceAntiSpoofFrameOutput = (output) => {
|
|
73
|
+
getSpoofDetectorFactory().resetSpoofDetectorOutput(output);
|
|
74
|
+
};
|
|
75
|
+
|
|
29
76
|
export default FaceAntiSpoof;
|
|
30
77
|
|
|
31
78
|
// Utility functions
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import AVFoundation
|
|
2
2
|
import Foundation
|
|
3
|
+
import ImageIO
|
|
3
4
|
import NitroModules
|
|
5
|
+
import Vision
|
|
4
6
|
import VisionCamera
|
|
5
7
|
|
|
6
8
|
final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutput {
|
|
@@ -13,6 +15,25 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
13
15
|
private var isDisposed = false
|
|
14
16
|
private var frameCounter = 0
|
|
15
17
|
private var lastFrameBrightness: Float = 0
|
|
18
|
+
private var captured = false
|
|
19
|
+
private var stableCount = 0
|
|
20
|
+
private var lastFaceBounds: SpoofDetectedFaceBounds?
|
|
21
|
+
private var consecutiveLiveFrames = 0
|
|
22
|
+
private var isLive = false
|
|
23
|
+
private var lastSpoofResult: AntiSpoofingResult?
|
|
24
|
+
private var lastFaceCount = -1
|
|
25
|
+
private var lastFacesUpdateAt = 0.0
|
|
26
|
+
private var lastResultUpdateAt = 0.0
|
|
27
|
+
|
|
28
|
+
private var antispoofLevel: Double { options.antispoofLevel ?? 0.8 }
|
|
29
|
+
private var stableFrames: Int { max(1, Int(options.stableFrames ?? 1)) }
|
|
30
|
+
private var requiredLiveFrames: Int { max(1, Int(options.requiredLiveFrames ?? 1)) }
|
|
31
|
+
private var laplacianThreshold: Double { options.laplacianThreshold ?? 2000 }
|
|
32
|
+
private var maxNeuralSpoofScore: Double { options.maxNeuralSpoofScore ?? 0.1 }
|
|
33
|
+
private var uiUpdateInterval: Double { max(0, options.uiUpdateIntervalMs ?? 500) / 1000 }
|
|
34
|
+
private var isLowMemoryDevice: Bool {
|
|
35
|
+
ProcessInfo.processInfo.physicalMemory <= 4 * 1024 * 1024 * 1024
|
|
36
|
+
}
|
|
16
37
|
|
|
17
38
|
let output = AVCaptureVideoDataOutput()
|
|
18
39
|
let requiresAudioInput = false
|
|
@@ -27,9 +48,19 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
27
48
|
}
|
|
28
49
|
|
|
29
50
|
var targetResolution: ResolutionRule {
|
|
30
|
-
let
|
|
51
|
+
let requestedResolution = options.targetResolution.map {
|
|
31
52
|
margelo.nitro.camera.Size(width: $0.width, height: $0.height)
|
|
32
53
|
} ?? margelo.nitro.camera.Size(width: 720.0, height: 1280.0)
|
|
54
|
+
let resolution: margelo.nitro.camera.Size
|
|
55
|
+
if isLowMemoryDevice && requestedResolution.width > 640 {
|
|
56
|
+
let scale = 640 / requestedResolution.width
|
|
57
|
+
resolution = margelo.nitro.camera.Size(
|
|
58
|
+
width: 640,
|
|
59
|
+
height: max(1, (requestedResolution.height * scale).rounded())
|
|
60
|
+
)
|
|
61
|
+
} else {
|
|
62
|
+
resolution = requestedResolution
|
|
63
|
+
}
|
|
33
64
|
return .closestTo(resolution)
|
|
34
65
|
}
|
|
35
66
|
|
|
@@ -60,6 +91,23 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
60
91
|
output.setSampleBufferDelegate(nil, queue: nil)
|
|
61
92
|
}
|
|
62
93
|
|
|
94
|
+
func resetState() {
|
|
95
|
+
queue.async { [weak self] in
|
|
96
|
+
guard let self else { return }
|
|
97
|
+
self.stateLock.lock()
|
|
98
|
+
self.captured = false
|
|
99
|
+
self.stableCount = 0
|
|
100
|
+
self.lastFaceBounds = nil
|
|
101
|
+
self.consecutiveLiveFrames = 0
|
|
102
|
+
self.isLive = false
|
|
103
|
+
self.lastSpoofResult = nil
|
|
104
|
+
self.lastFaceCount = -1
|
|
105
|
+
self.lastFacesUpdateAt = 0
|
|
106
|
+
self.lastResultUpdateAt = 0
|
|
107
|
+
self.stateLock.unlock()
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
63
111
|
private func process(_ sampleBuffer: CMSampleBuffer) {
|
|
64
112
|
stateLock.lock()
|
|
65
113
|
guard !isDisposed, !isBusy else {
|
|
@@ -82,6 +130,10 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
82
130
|
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
|
83
131
|
}
|
|
84
132
|
|
|
133
|
+
if options.onFacesDetected != nil || options.onStableFaceDetected != nil {
|
|
134
|
+
processDetectedFaces(detectFaces(pixelBuffer))
|
|
135
|
+
}
|
|
136
|
+
|
|
85
137
|
guard shouldProcessFrame(pixelBuffer),
|
|
86
138
|
let input = makeRGBInput(from: pixelBuffer) else {
|
|
87
139
|
stateLock.lock()
|
|
@@ -98,7 +150,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
98
150
|
|
|
99
151
|
do {
|
|
100
152
|
let result = try FaceAntiSpoofManager.process(input: input)
|
|
101
|
-
|
|
153
|
+
processAntiSpoofResult(
|
|
102
154
|
AntiSpoofingResult(
|
|
103
155
|
isLive: result.isLive,
|
|
104
156
|
label: result.isLive ? "Live Face" : "Spoof Face",
|
|
@@ -113,6 +165,138 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
113
165
|
}
|
|
114
166
|
}
|
|
115
167
|
|
|
168
|
+
private var visionImageOrientation: CGImagePropertyOrientation {
|
|
169
|
+
switch outputOrientation {
|
|
170
|
+
case .right: return .right
|
|
171
|
+
case .down: return .down
|
|
172
|
+
case .left: return .left
|
|
173
|
+
default: return .up
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private func detectFaces(_ pixelBuffer: CVPixelBuffer) -> [SpoofDetectedFace] {
|
|
178
|
+
let request = VNDetectFaceRectanglesRequest()
|
|
179
|
+
do {
|
|
180
|
+
try VNImageRequestHandler(
|
|
181
|
+
cvPixelBuffer: pixelBuffer,
|
|
182
|
+
orientation: visionImageOrientation,
|
|
183
|
+
options: [:]
|
|
184
|
+
).perform([request])
|
|
185
|
+
|
|
186
|
+
let width = CVPixelBufferGetWidth(pixelBuffer)
|
|
187
|
+
let height = CVPixelBufferGetHeight(pixelBuffer)
|
|
188
|
+
return (request.results ?? []).map { observation in
|
|
189
|
+
SpoofDetectedFace(
|
|
190
|
+
bounds: makeBufferBounds(
|
|
191
|
+
from: observation.boundingBox,
|
|
192
|
+
width: width,
|
|
193
|
+
height: height
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
options.onError?(error)
|
|
199
|
+
return []
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private func processDetectedFaces(_ faces: [SpoofDetectedFace]) {
|
|
204
|
+
let now = ProcessInfo.processInfo.systemUptime
|
|
205
|
+
if faces.count == 1 {
|
|
206
|
+
let bounds = faces[0].bounds
|
|
207
|
+
stableCount += 1
|
|
208
|
+
lastFaceBounds = bounds
|
|
209
|
+
} else {
|
|
210
|
+
captured = false
|
|
211
|
+
stableCount = 0
|
|
212
|
+
lastFaceBounds = nil
|
|
213
|
+
consecutiveLiveFrames = 0
|
|
214
|
+
isLive = false
|
|
215
|
+
lastSpoofResult = nil
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let shouldNotify = faces.count != lastFaceCount || now - lastFacesUpdateAt >= uiUpdateInterval
|
|
219
|
+
if shouldNotify {
|
|
220
|
+
lastFaceCount = faces.count
|
|
221
|
+
lastFacesUpdateAt = now
|
|
222
|
+
options.onFacesDetected?(faces)
|
|
223
|
+
}
|
|
224
|
+
notifyStableFaceIfReady()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private func processAntiSpoofResult(_ result: AntiSpoofingResult) {
|
|
228
|
+
lastSpoofResult = result
|
|
229
|
+
if result.neuralNetworkScore >= maxNeuralSpoofScore {
|
|
230
|
+
consecutiveLiveFrames = 0
|
|
231
|
+
isLive = false
|
|
232
|
+
} else {
|
|
233
|
+
let targetPercent = Int((antispoofLevel * 100).rounded())
|
|
234
|
+
let currentPercent = Int((result.confidence * 100).rounded())
|
|
235
|
+
let combinedPercent = Int((result.combinedScore * 100).rounded())
|
|
236
|
+
let meetsConfidence = currentPercent >= targetPercent ||
|
|
237
|
+
result.confidence >= antispoofLevel ||
|
|
238
|
+
combinedPercent >= targetPercent ||
|
|
239
|
+
result.combinedScore >= antispoofLevel
|
|
240
|
+
let meetsLaplacian = result.laplacianScore == 0 ||
|
|
241
|
+
result.laplacianScore >= laplacianThreshold
|
|
242
|
+
if meetsConfidence && meetsLaplacian {
|
|
243
|
+
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
244
|
+
} else {
|
|
245
|
+
consecutiveLiveFrames = max(0, consecutiveLiveFrames - 1)
|
|
246
|
+
}
|
|
247
|
+
isLive = consecutiveLiveFrames >= requiredLiveFrames
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let now = ProcessInfo.processInfo.systemUptime
|
|
251
|
+
if now - lastResultUpdateAt >= uiUpdateInterval {
|
|
252
|
+
lastResultUpdateAt = now
|
|
253
|
+
options.onResult(result)
|
|
254
|
+
}
|
|
255
|
+
notifyStableFaceIfReady()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private func notifyStableFaceIfReady() {
|
|
259
|
+
guard !captured,
|
|
260
|
+
stableCount >= stableFrames,
|
|
261
|
+
isLive,
|
|
262
|
+
consecutiveLiveFrames >= requiredLiveFrames,
|
|
263
|
+
let bounds = lastFaceBounds,
|
|
264
|
+
let result = lastSpoofResult,
|
|
265
|
+
let callback = options.onStableFaceDetected else { return }
|
|
266
|
+
captured = true
|
|
267
|
+
callback(bounds, result)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private func makeBufferBounds(
|
|
271
|
+
from box: CGRect,
|
|
272
|
+
width: Int,
|
|
273
|
+
height: Int
|
|
274
|
+
) -> SpoofDetectedFaceBounds {
|
|
275
|
+
let left = Double(box.minX)
|
|
276
|
+
let top = Double(1 - box.maxY)
|
|
277
|
+
let right = Double(box.maxX)
|
|
278
|
+
let bottom = Double(1 - box.minY)
|
|
279
|
+
let raw: (left: Double, top: Double, right: Double, bottom: Double)
|
|
280
|
+
|
|
281
|
+
switch outputOrientation {
|
|
282
|
+
case .right:
|
|
283
|
+
raw = (top, 1 - right, bottom, 1 - left)
|
|
284
|
+
case .down:
|
|
285
|
+
raw = (1 - right, 1 - bottom, 1 - left, 1 - top)
|
|
286
|
+
case .left:
|
|
287
|
+
raw = (1 - bottom, left, 1 - top, right)
|
|
288
|
+
default:
|
|
289
|
+
raw = (left, top, right, bottom)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return SpoofDetectedFaceBounds(
|
|
293
|
+
x: raw.left * Double(width),
|
|
294
|
+
y: raw.top * Double(height),
|
|
295
|
+
width: (raw.right - raw.left) * Double(width),
|
|
296
|
+
height: (raw.bottom - raw.top) * Double(height)
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
|
|
116
300
|
private func shouldProcessFrame(_ pixelBuffer: CVPixelBuffer) -> Bool {
|
|
117
301
|
frameCounter += 1
|
|
118
302
|
guard frameCounter % 3 == 0 else { return false }
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
|
|
18
18
|
#include "JHybridSpoofDetectorFactorySpec.hpp"
|
|
19
19
|
#include "JFunc_void_AntiSpoofingResult.hpp"
|
|
20
|
+
#include "JFunc_void_std__vector_SpoofDetectedFace_.hpp"
|
|
21
|
+
#include "JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult.hpp"
|
|
20
22
|
#include "JFunc_void_std__exception_ptr.hpp"
|
|
21
23
|
#include <NitroModules/DefaultConstructableObject.hpp>
|
|
22
24
|
|
|
@@ -44,6 +46,8 @@ void registerAllNatives() {
|
|
|
44
46
|
// Register native JNI methods
|
|
45
47
|
margelo::nitro::camera::spoofdetector::JHybridSpoofDetectorFactorySpec::CxxPart::registerNatives();
|
|
46
48
|
margelo::nitro::camera::spoofdetector::JFunc_void_AntiSpoofingResult_cxx::registerNatives();
|
|
49
|
+
margelo::nitro::camera::spoofdetector::JFunc_void_std__vector_SpoofDetectedFace__cxx::registerNatives();
|
|
50
|
+
margelo::nitro::camera::spoofdetector::JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx::registerNatives();
|
|
47
51
|
margelo::nitro::camera::spoofdetector::JFunc_void_std__exception_ptr_cxx::registerNatives();
|
|
48
52
|
|
|
49
53
|
// Register Nitro Hybrid Objects
|
package/nitrogen/generated/android/c++/JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult.hpp
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include <functional>
|
|
12
|
+
|
|
13
|
+
#include "SpoofDetectedFaceBounds.hpp"
|
|
14
|
+
#include "AntiSpoofingResult.hpp"
|
|
15
|
+
#include <functional>
|
|
16
|
+
#include <NitroModules/JNICallable.hpp>
|
|
17
|
+
#include "JSpoofDetectedFaceBounds.hpp"
|
|
18
|
+
#include "JAntiSpoofingResult.hpp"
|
|
19
|
+
#include <string>
|
|
20
|
+
|
|
21
|
+
namespace margelo::nitro::camera::spoofdetector {
|
|
22
|
+
|
|
23
|
+
using namespace facebook;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Represents the Java/Kotlin callback `(face: SpoofDetectedFaceBounds, result: AntiSpoofingResult) -> Unit`.
|
|
27
|
+
* This can be passed around between C++ and Java/Kotlin.
|
|
28
|
+
*/
|
|
29
|
+
struct JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult: public jni::JavaClass<JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult> {
|
|
30
|
+
public:
|
|
31
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/Func_void_SpoofDetectedFaceBounds_AntiSpoofingResult;";
|
|
32
|
+
|
|
33
|
+
public:
|
|
34
|
+
/**
|
|
35
|
+
* Invokes the function this `JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult` instance holds through JNI.
|
|
36
|
+
*/
|
|
37
|
+
void invoke(const SpoofDetectedFaceBounds& face, const AntiSpoofingResult& result) const {
|
|
38
|
+
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JSpoofDetectedFaceBounds> /* face */, jni::alias_ref<JAntiSpoofingResult> /* result */)>("invoke");
|
|
39
|
+
method(self(), JSpoofDetectedFaceBounds::fromCpp(face), JAntiSpoofingResult::fromCpp(result));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* An implementation of Func_void_SpoofDetectedFaceBounds_AntiSpoofingResult that is backed by a C++ implementation (using `std::function<...>`)
|
|
45
|
+
*/
|
|
46
|
+
class JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx final: public jni::HybridClass<JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx, JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult> {
|
|
47
|
+
public:
|
|
48
|
+
static jni::local_ref<JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult::javaobject> fromCpp(const std::function<void(const SpoofDetectedFaceBounds& /* face */, const AntiSpoofingResult& /* result */)>& func) {
|
|
49
|
+
return JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx::newObjectCxxArgs(func);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public:
|
|
53
|
+
/**
|
|
54
|
+
* Invokes the C++ `std::function<...>` this `JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx` instance holds.
|
|
55
|
+
*/
|
|
56
|
+
void invoke_cxx(jni::alias_ref<JSpoofDetectedFaceBounds> face, jni::alias_ref<JAntiSpoofingResult> result) {
|
|
57
|
+
_func(face->toCpp(), result->toCpp());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public:
|
|
61
|
+
[[nodiscard]]
|
|
62
|
+
inline const std::function<void(const SpoofDetectedFaceBounds& /* face */, const AntiSpoofingResult& /* result */)>& getFunction() const {
|
|
63
|
+
return _func;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public:
|
|
67
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/Func_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx;";
|
|
68
|
+
static void registerNatives() {
|
|
69
|
+
registerHybrid({makeNativeMethod("invoke_cxx", JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx::invoke_cxx)});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private:
|
|
73
|
+
explicit JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult_cxx(const std::function<void(const SpoofDetectedFaceBounds& /* face */, const AntiSpoofingResult& /* result */)>& func): _func(func) { }
|
|
74
|
+
|
|
75
|
+
private:
|
|
76
|
+
friend HybridBase;
|
|
77
|
+
std::function<void(const SpoofDetectedFaceBounds& /* face */, const AntiSpoofingResult& /* result */)> _func;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
} // namespace margelo::nitro::camera::spoofdetector
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JFunc_void_std__vector_SpoofDetectedFace_.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include <functional>
|
|
12
|
+
|
|
13
|
+
#include "SpoofDetectedFace.hpp"
|
|
14
|
+
#include <vector>
|
|
15
|
+
#include <functional>
|
|
16
|
+
#include <NitroModules/JNICallable.hpp>
|
|
17
|
+
#include "JSpoofDetectedFace.hpp"
|
|
18
|
+
#include "SpoofDetectedFaceBounds.hpp"
|
|
19
|
+
#include "JSpoofDetectedFaceBounds.hpp"
|
|
20
|
+
|
|
21
|
+
namespace margelo::nitro::camera::spoofdetector {
|
|
22
|
+
|
|
23
|
+
using namespace facebook;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Represents the Java/Kotlin callback `(faces: Array<SpoofDetectedFace>) -> Unit`.
|
|
27
|
+
* This can be passed around between C++ and Java/Kotlin.
|
|
28
|
+
*/
|
|
29
|
+
struct JFunc_void_std__vector_SpoofDetectedFace_: public jni::JavaClass<JFunc_void_std__vector_SpoofDetectedFace_> {
|
|
30
|
+
public:
|
|
31
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/Func_void_std__vector_SpoofDetectedFace_;";
|
|
32
|
+
|
|
33
|
+
public:
|
|
34
|
+
/**
|
|
35
|
+
* Invokes the function this `JFunc_void_std__vector_SpoofDetectedFace_` instance holds through JNI.
|
|
36
|
+
*/
|
|
37
|
+
void invoke(const std::vector<SpoofDetectedFace>& faces) const {
|
|
38
|
+
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JArrayClass<JSpoofDetectedFace>> /* faces */)>("invoke");
|
|
39
|
+
method(self(), [&](auto&& __input) {
|
|
40
|
+
size_t __size = __input.size();
|
|
41
|
+
jni::local_ref<jni::JArrayClass<JSpoofDetectedFace>> __array = jni::JArrayClass<JSpoofDetectedFace>::newArray(__size);
|
|
42
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
43
|
+
const auto& __element = __input[__i];
|
|
44
|
+
auto __elementJni = JSpoofDetectedFace::fromCpp(__element);
|
|
45
|
+
__array->setElement(__i, *__elementJni);
|
|
46
|
+
}
|
|
47
|
+
return __array;
|
|
48
|
+
}(faces));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* An implementation of Func_void_std__vector_SpoofDetectedFace_ that is backed by a C++ implementation (using `std::function<...>`)
|
|
54
|
+
*/
|
|
55
|
+
class JFunc_void_std__vector_SpoofDetectedFace__cxx final: public jni::HybridClass<JFunc_void_std__vector_SpoofDetectedFace__cxx, JFunc_void_std__vector_SpoofDetectedFace_> {
|
|
56
|
+
public:
|
|
57
|
+
static jni::local_ref<JFunc_void_std__vector_SpoofDetectedFace_::javaobject> fromCpp(const std::function<void(const std::vector<SpoofDetectedFace>& /* faces */)>& func) {
|
|
58
|
+
return JFunc_void_std__vector_SpoofDetectedFace__cxx::newObjectCxxArgs(func);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public:
|
|
62
|
+
/**
|
|
63
|
+
* Invokes the C++ `std::function<...>` this `JFunc_void_std__vector_SpoofDetectedFace__cxx` instance holds.
|
|
64
|
+
*/
|
|
65
|
+
void invoke_cxx(jni::alias_ref<jni::JArrayClass<JSpoofDetectedFace>> faces) {
|
|
66
|
+
_func([&](auto&& __input) {
|
|
67
|
+
size_t __size = __input->size();
|
|
68
|
+
std::vector<SpoofDetectedFace> __vector;
|
|
69
|
+
__vector.reserve(__size);
|
|
70
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
71
|
+
auto __element = __input->getElement(__i);
|
|
72
|
+
__vector.push_back(__element->toCpp());
|
|
73
|
+
}
|
|
74
|
+
return __vector;
|
|
75
|
+
}(faces));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public:
|
|
79
|
+
[[nodiscard]]
|
|
80
|
+
inline const std::function<void(const std::vector<SpoofDetectedFace>& /* faces */)>& getFunction() const {
|
|
81
|
+
return _func;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public:
|
|
85
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/Func_void_std__vector_SpoofDetectedFace__cxx;";
|
|
86
|
+
static void registerNatives() {
|
|
87
|
+
registerHybrid({makeNativeMethod("invoke_cxx", JFunc_void_std__vector_SpoofDetectedFace__cxx::invoke_cxx)});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private:
|
|
91
|
+
explicit JFunc_void_std__vector_SpoofDetectedFace__cxx(const std::function<void(const std::vector<SpoofDetectedFace>& /* faces */)>& func): _func(func) { }
|
|
92
|
+
|
|
93
|
+
private:
|
|
94
|
+
friend HybridBase;
|
|
95
|
+
std::function<void(const std::vector<SpoofDetectedFace>& /* faces */)> _func;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
} // namespace margelo::nitro::camera::spoofdetector
|
|
@@ -15,6 +15,10 @@ namespace margelo::nitro::camera::spoofdetector { struct SpoofDetectorOutputOpti
|
|
|
15
15
|
namespace margelo::nitro::camera::spoofdetector { struct Size; }
|
|
16
16
|
// Forward declaration of `AntiSpoofingResult` to properly resolve imports.
|
|
17
17
|
namespace margelo::nitro::camera::spoofdetector { struct AntiSpoofingResult; }
|
|
18
|
+
// Forward declaration of `SpoofDetectedFace` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::camera::spoofdetector { struct SpoofDetectedFace; }
|
|
20
|
+
// Forward declaration of `SpoofDetectedFaceBounds` to properly resolve imports.
|
|
21
|
+
namespace margelo::nitro::camera::spoofdetector { struct SpoofDetectedFaceBounds; }
|
|
18
22
|
|
|
19
23
|
#include <memory>
|
|
20
24
|
#include <VisionCamera/HybridCameraOutputSpec.hpp>
|
|
@@ -30,6 +34,13 @@ namespace margelo::nitro::camera::spoofdetector { struct AntiSpoofingResult; }
|
|
|
30
34
|
#include <NitroModules/JNICallable.hpp>
|
|
31
35
|
#include "JAntiSpoofingResult.hpp"
|
|
32
36
|
#include <string>
|
|
37
|
+
#include "SpoofDetectedFace.hpp"
|
|
38
|
+
#include <vector>
|
|
39
|
+
#include "JFunc_void_std__vector_SpoofDetectedFace_.hpp"
|
|
40
|
+
#include "JSpoofDetectedFace.hpp"
|
|
41
|
+
#include "SpoofDetectedFaceBounds.hpp"
|
|
42
|
+
#include "JSpoofDetectedFaceBounds.hpp"
|
|
43
|
+
#include "JFunc_void_SpoofDetectedFaceBounds_AntiSpoofingResult.hpp"
|
|
33
44
|
#include <exception>
|
|
34
45
|
#include "JFunc_void_std__exception_ptr.hpp"
|
|
35
46
|
|
|
@@ -71,5 +82,9 @@ namespace margelo::nitro::camera::spoofdetector {
|
|
|
71
82
|
auto __result = method(_javaPart, JSpoofDetectorOutputOptions::fromCpp(options));
|
|
72
83
|
return __result->getJHybridCameraOutputSpec();
|
|
73
84
|
}
|
|
85
|
+
void JHybridSpoofDetectorFactorySpec::resetSpoofDetectorOutput(const std::shared_ptr<margelo::nitro::camera::HybridCameraOutputSpec>& output) {
|
|
86
|
+
static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<margelo::nitro::camera::JHybridCameraOutputSpec::JavaPart> /* output */)>("resetSpoofDetectorOutput");
|
|
87
|
+
method(_javaPart, std::dynamic_pointer_cast<margelo::nitro::camera::JHybridCameraOutputSpec>(output)->getJavaPart());
|
|
88
|
+
}
|
|
74
89
|
|
|
75
90
|
} // namespace margelo::nitro::camera::spoofdetector
|
|
@@ -55,6 +55,7 @@ namespace margelo::nitro::camera::spoofdetector {
|
|
|
55
55
|
public:
|
|
56
56
|
// Methods
|
|
57
57
|
std::shared_ptr<margelo::nitro::camera::HybridCameraOutputSpec> createSpoofDetectorOutput(const SpoofDetectorOutputOptions& options) override;
|
|
58
|
+
void resetSpoofDetectorOutput(const std::shared_ptr<margelo::nitro::camera::HybridCameraOutputSpec>& output) override;
|
|
58
59
|
|
|
59
60
|
private:
|
|
60
61
|
jni::global_ref<JHybridSpoofDetectorFactorySpec::JavaPart> _javaPart;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JSpoofDetectedFace.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include "SpoofDetectedFace.hpp"
|
|
12
|
+
|
|
13
|
+
#include "JSpoofDetectedFaceBounds.hpp"
|
|
14
|
+
#include "SpoofDetectedFaceBounds.hpp"
|
|
15
|
+
|
|
16
|
+
namespace margelo::nitro::camera::spoofdetector {
|
|
17
|
+
|
|
18
|
+
using namespace facebook;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The C++ JNI bridge between the C++ struct "SpoofDetectedFace" and the Kotlin data class "SpoofDetectedFace".
|
|
22
|
+
*/
|
|
23
|
+
struct JSpoofDetectedFace final: public jni::JavaClass<JSpoofDetectedFace> {
|
|
24
|
+
public:
|
|
25
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/SpoofDetectedFace;";
|
|
26
|
+
|
|
27
|
+
public:
|
|
28
|
+
/**
|
|
29
|
+
* Convert this Java/Kotlin-based struct to the C++ struct SpoofDetectedFace by copying all values to C++.
|
|
30
|
+
*/
|
|
31
|
+
[[maybe_unused]]
|
|
32
|
+
[[nodiscard]]
|
|
33
|
+
SpoofDetectedFace toCpp() const {
|
|
34
|
+
static const auto clazz = javaClassStatic();
|
|
35
|
+
static const auto fieldBounds = clazz->getField<JSpoofDetectedFaceBounds>("bounds");
|
|
36
|
+
jni::local_ref<JSpoofDetectedFaceBounds> bounds = this->getFieldValue(fieldBounds);
|
|
37
|
+
return SpoofDetectedFace(
|
|
38
|
+
bounds->toCpp()
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public:
|
|
43
|
+
/**
|
|
44
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
45
|
+
*/
|
|
46
|
+
[[maybe_unused]]
|
|
47
|
+
static jni::local_ref<JSpoofDetectedFace::javaobject> fromCpp(const SpoofDetectedFace& value) {
|
|
48
|
+
using JSignature = JSpoofDetectedFace(jni::alias_ref<JSpoofDetectedFaceBounds>);
|
|
49
|
+
static const auto clazz = javaClassStatic();
|
|
50
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
51
|
+
return create(
|
|
52
|
+
clazz,
|
|
53
|
+
JSpoofDetectedFaceBounds::fromCpp(value.bounds)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
} // namespace margelo::nitro::camera::spoofdetector
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JSpoofDetectedFaceBounds.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include "SpoofDetectedFaceBounds.hpp"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
namespace margelo::nitro::camera::spoofdetector {
|
|
16
|
+
|
|
17
|
+
using namespace facebook;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The C++ JNI bridge between the C++ struct "SpoofDetectedFaceBounds" and the Kotlin data class "SpoofDetectedFaceBounds".
|
|
21
|
+
*/
|
|
22
|
+
struct JSpoofDetectedFaceBounds final: public jni::JavaClass<JSpoofDetectedFaceBounds> {
|
|
23
|
+
public:
|
|
24
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/spoofdetector/SpoofDetectedFaceBounds;";
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
/**
|
|
28
|
+
* Convert this Java/Kotlin-based struct to the C++ struct SpoofDetectedFaceBounds by copying all values to C++.
|
|
29
|
+
*/
|
|
30
|
+
[[maybe_unused]]
|
|
31
|
+
[[nodiscard]]
|
|
32
|
+
SpoofDetectedFaceBounds toCpp() const {
|
|
33
|
+
static const auto clazz = javaClassStatic();
|
|
34
|
+
static const auto fieldX = clazz->getField<double>("x");
|
|
35
|
+
double x = this->getFieldValue(fieldX);
|
|
36
|
+
static const auto fieldY = clazz->getField<double>("y");
|
|
37
|
+
double y = this->getFieldValue(fieldY);
|
|
38
|
+
static const auto fieldWidth = clazz->getField<double>("width");
|
|
39
|
+
double width = this->getFieldValue(fieldWidth);
|
|
40
|
+
static const auto fieldHeight = clazz->getField<double>("height");
|
|
41
|
+
double height = this->getFieldValue(fieldHeight);
|
|
42
|
+
return SpoofDetectedFaceBounds(
|
|
43
|
+
x,
|
|
44
|
+
y,
|
|
45
|
+
width,
|
|
46
|
+
height
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public:
|
|
51
|
+
/**
|
|
52
|
+
* Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
|
|
53
|
+
*/
|
|
54
|
+
[[maybe_unused]]
|
|
55
|
+
static jni::local_ref<JSpoofDetectedFaceBounds::javaobject> fromCpp(const SpoofDetectedFaceBounds& value) {
|
|
56
|
+
using JSignature = JSpoofDetectedFaceBounds(double, double, double, double);
|
|
57
|
+
static const auto clazz = javaClassStatic();
|
|
58
|
+
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
59
|
+
return create(
|
|
60
|
+
clazz,
|
|
61
|
+
value.x,
|
|
62
|
+
value.y,
|
|
63
|
+
value.width,
|
|
64
|
+
value.height
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
} // namespace margelo::nitro::camera::spoofdetector
|