react-native-vision-camera-spoof-detector 1.4.1 → 1.5.1
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 +42 -46
- package/VisionCameraSpoofDetector.podspec +12 -2
- package/android/build.gradle +0 -13
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofPackage.kt +1 -3
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofingAdvanced.kt +78 -925
- package/android/src/main/java/com/lengdev/livenessdetector/LivenessDetector.kt +228 -0
- package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +17 -31
- package/android/src/main/jniLibs/arm64-v8a/libliveness_detector.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libliveness_detector.so +0 -0
- package/index.d.ts +2 -9
- package/index.js +6 -6
- package/ios/FaceAntiSpoofModule.m +3 -2
- package/ios/HybridSpoofDetectorOutput.swift +73 -158
- package/ios/NcnnLivenessDetector.h +24 -0
- package/ios/NcnnLivenessDetector.mm +248 -0
- package/ios/ncnn.xcframework/Info.plist +27 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/ncnn +0 -0
- package/ios/openmp.xcframework/Info.plist +27 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/openmp +0 -0
- package/lib/assets/live/config.json +1 -0
- package/lib/assets/live/model_1.bin +0 -0
- package/lib/assets/live/model_1.param +214 -0
- package/lib/assets/live/model_2.bin +0 -0
- package/lib/assets/live/model_2.param +238 -0
- package/nitrogen/generated/android/c++/JSpoofDetectorOutputOptions.hpp +1 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectorOutputOptions.kt +4 -14
- package/nitrogen/generated/ios/swift/SpoofDetectorOutputOptions.swift +1 -37
- package/nitrogen/generated/shared/c++/SpoofDetectorOutputOptions.hpp +1 -9
- package/package.json +1 -2
- package/src/specs/SpoofDetectorFactory.nitro.ts +0 -2
- package/android/fix-prefab.gradle +0 -4
- package/android/src/main/java/com/faceantispoof/AcceleratorConfig.kt +0 -97
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofModule.kt +0 -242
- package/android/src/main/res/values/strings.xml +0 -3
- package/lib/assets/FaceAntiSpoofing.tflite +0 -0
- package/lib/assets/model_spec.json +0 -16
|
@@ -9,8 +9,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
9
9
|
private struct DetectedFace {
|
|
10
10
|
let observation: VNFaceObservation
|
|
11
11
|
let face: SpoofDetectedFace
|
|
12
|
-
let leftEye: CGPoint
|
|
13
|
-
let rightEye: CGPoint
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
private let queue = DispatchQueue(label: "com.faceantispoof.output", qos: .userInitiated)
|
|
@@ -18,8 +16,8 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
18
16
|
private let delegate: SpoofDetectorDelegate
|
|
19
17
|
private let faceDetectionRequest = VNDetectFaceLandmarksRequest()
|
|
20
18
|
private let stateLock = NSLock()
|
|
21
|
-
private
|
|
22
|
-
private var
|
|
19
|
+
private lazy var livenessDetector = NcnnLivenessDetector()
|
|
20
|
+
private var ncnnModelsLoaded = false
|
|
23
21
|
private var isBusy = false
|
|
24
22
|
private var isDisposed = false
|
|
25
23
|
private var captured = false
|
|
@@ -29,11 +27,9 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
29
27
|
private var consecutiveLiveFrames = 0
|
|
30
28
|
private var lastSpoofResult: AntiSpoofingResult?
|
|
31
29
|
|
|
32
|
-
private var antispoofLevel: Double { options.antispoofLevel ?? 0.
|
|
30
|
+
private var antispoofLevel: Double { options.antispoofLevel ?? 0.5 }
|
|
33
31
|
private var stableFrames: Int { max(1, Int(options.stableFrames ?? 3)) }
|
|
34
32
|
private var requiredLiveFrames: Int { max(1, Int(options.requiredLiveFrames ?? 3)) }
|
|
35
|
-
private var laplacianThreshold: Double { options.laplacianThreshold ?? 1100 }
|
|
36
|
-
private var maxNeuralSpoofScore: Double { options.maxNeuralSpoofScore ?? 0.1 }
|
|
37
33
|
let output = AVCaptureVideoDataOutput()
|
|
38
34
|
let requiresAudioInput = false
|
|
39
35
|
let requiresDepthFormat = false
|
|
@@ -113,7 +109,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
113
109
|
}
|
|
114
110
|
|
|
115
111
|
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
|
|
116
|
-
options.onError?(RuntimeError.error(withMessage: "Failed to
|
|
112
|
+
options.onError?(RuntimeError.error(withMessage: "Failed to access the camera frame buffer."))
|
|
117
113
|
return
|
|
118
114
|
}
|
|
119
115
|
|
|
@@ -141,25 +137,39 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
141
137
|
invalidateLiveEvidence()
|
|
142
138
|
return
|
|
143
139
|
}
|
|
144
|
-
guard makeRGBInput(from: pixelBuffer, face: faces[0], into: &modelInput) else {
|
|
145
|
-
invalidateLiveEvidence()
|
|
146
|
-
options.onError?(RuntimeError.error(withMessage: "Failed to prepare the face image for inference."))
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
|
|
150
140
|
do {
|
|
151
|
-
|
|
152
|
-
|
|
141
|
+
if !ncnnModelsLoaded {
|
|
142
|
+
guard livenessDetector.loadModels(atPath: try findModelDirectory()) else {
|
|
143
|
+
throw RuntimeError.error(withMessage: livenessDetector.lastError)
|
|
144
|
+
}
|
|
145
|
+
ncnnModelsLoaded = true
|
|
153
146
|
}
|
|
154
|
-
guard
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
147
|
+
guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
|
|
148
|
+
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else {
|
|
149
|
+
throw RuntimeError.error(withMessage: "Camera frame is not available in BGRA format.")
|
|
150
|
+
}
|
|
151
|
+
let bounds = faces[0].face.bounds
|
|
152
|
+
let left = max(0, Int(bounds.x))
|
|
153
|
+
let top = max(0, Int(bounds.y))
|
|
154
|
+
let right = min(CVPixelBufferGetWidth(pixelBuffer), Int(bounds.x + bounds.width))
|
|
155
|
+
let bottom = min(CVPixelBufferGetHeight(pixelBuffer), Int(bounds.y + bounds.height))
|
|
156
|
+
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
157
|
+
let livenessScore = livenessDetector.score(
|
|
158
|
+
forBGRABytes: baseAddress,
|
|
159
|
+
byteCount: bytesPerRow * CVPixelBufferGetHeight(pixelBuffer),
|
|
160
|
+
width: CVPixelBufferGetWidth(pixelBuffer),
|
|
161
|
+
height: CVPixelBufferGetHeight(pixelBuffer),
|
|
162
|
+
bytesPerRow: bytesPerRow,
|
|
163
|
+
left: left,
|
|
164
|
+
top: top,
|
|
165
|
+
right: right,
|
|
166
|
+
bottom: bottom
|
|
162
167
|
)
|
|
168
|
+
guard livenessScore.isFinite, (0...1).contains(livenessScore) else {
|
|
169
|
+
throw RuntimeError.error(withMessage: livenessDetector.lastError)
|
|
170
|
+
}
|
|
171
|
+
guard isActive() else { return }
|
|
172
|
+
processAntiSpoofResult(livenessScore)
|
|
163
173
|
} catch {
|
|
164
174
|
invalidateLiveEvidence()
|
|
165
175
|
options.onError?(error)
|
|
@@ -184,31 +194,15 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
184
194
|
|
|
185
195
|
let width = CVPixelBufferGetWidth(pixelBuffer)
|
|
186
196
|
let height = CVPixelBufferGetHeight(pixelBuffer)
|
|
187
|
-
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
188
|
-
let orientedWidth = isQuarterTurn ? height : width
|
|
189
|
-
let orientedHeight = isQuarterTurn ? width : height
|
|
190
197
|
return (faceDetectionRequest.results ?? []).compactMap { observation in
|
|
191
|
-
guard isEligibleFace(observation, width:
|
|
192
|
-
let landmarks = observation.landmarks,
|
|
193
|
-
let leftEye = landmarkCenter(landmarks.leftEye, in: observation.boundingBox),
|
|
194
|
-
let rightEye = landmarkCenter(landmarks.rightEye, in: observation.boundingBox) else {
|
|
198
|
+
guard isEligibleFace(observation, width: width, height: height) else {
|
|
195
199
|
return nil
|
|
196
200
|
}
|
|
197
|
-
let leftEyePoint = CGPoint(
|
|
198
|
-
x: leftEye.x * CGFloat(orientedWidth),
|
|
199
|
-
y: leftEye.y * CGFloat(orientedHeight)
|
|
200
|
-
)
|
|
201
|
-
let rightEyePoint = CGPoint(
|
|
202
|
-
x: rightEye.x * CGFloat(orientedWidth),
|
|
203
|
-
y: rightEye.y * CGFloat(orientedHeight)
|
|
204
|
-
)
|
|
205
201
|
return DetectedFace(
|
|
206
202
|
observation: observation,
|
|
207
203
|
face: SpoofDetectedFace(
|
|
208
204
|
bounds: makeBufferBounds(from: observation.boundingBox, width: width, height: height)
|
|
209
|
-
)
|
|
210
|
-
leftEye: leftEyePoint,
|
|
211
|
-
rightEye: rightEyePoint
|
|
205
|
+
)
|
|
212
206
|
)
|
|
213
207
|
}
|
|
214
208
|
}
|
|
@@ -257,18 +251,19 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
257
251
|
return centerDistance <= max(previousDiagonal, currentDiagonal) * 0.5
|
|
258
252
|
}
|
|
259
253
|
|
|
260
|
-
private func processAntiSpoofResult(_
|
|
254
|
+
private func processAntiSpoofResult(_ livenessScore: Float) {
|
|
255
|
+
let score = Double(livenessScore)
|
|
256
|
+
let result = AntiSpoofingResult(
|
|
257
|
+
neuralNetworkScore: 1 - score,
|
|
258
|
+
laplacianScore: 0,
|
|
259
|
+
combinedScore: score,
|
|
260
|
+
confidence: score
|
|
261
|
+
)
|
|
261
262
|
lastSpoofResult = result
|
|
262
|
-
if
|
|
263
|
-
consecutiveLiveFrames =
|
|
263
|
+
if score >= antispoofLevel {
|
|
264
|
+
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
264
265
|
} else {
|
|
265
|
-
|
|
266
|
-
let meetsLaplacian = result.laplacianScore >= laplacianThreshold
|
|
267
|
-
if meetsConfidence && meetsLaplacian {
|
|
268
|
-
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
269
|
-
} else {
|
|
270
|
-
consecutiveLiveFrames = 0
|
|
271
|
-
}
|
|
266
|
+
consecutiveLiveFrames = 0
|
|
272
267
|
}
|
|
273
268
|
|
|
274
269
|
options.onResult(result)
|
|
@@ -343,105 +338,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
343
338
|
return average >= 24 && average <= 220
|
|
344
339
|
}
|
|
345
340
|
|
|
346
|
-
private func makeRGBInput(
|
|
347
|
-
from pixelBuffer: CVPixelBuffer,
|
|
348
|
-
face: DetectedFace,
|
|
349
|
-
into result: inout [Float]
|
|
350
|
-
) -> Bool {
|
|
351
|
-
guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
|
|
352
|
-
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer),
|
|
353
|
-
result.count == targetSize * targetSize * 3 else { return false }
|
|
354
|
-
|
|
355
|
-
let sourceWidth = CVPixelBufferGetWidth(pixelBuffer)
|
|
356
|
-
let sourceHeight = CVPixelBufferGetHeight(pixelBuffer)
|
|
357
|
-
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
358
|
-
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
359
|
-
let orientedWidth = isQuarterTurn ? sourceHeight : sourceWidth
|
|
360
|
-
let orientedHeight = isQuarterTurn ? sourceWidth : sourceHeight
|
|
361
|
-
let box = face.observation.boundingBox
|
|
362
|
-
let faceWidth = box.width * CGFloat(orientedWidth)
|
|
363
|
-
let faceHeight = box.height * CGFloat(orientedHeight)
|
|
364
|
-
let cropSide = min(
|
|
365
|
-
min(orientedWidth, orientedHeight),
|
|
366
|
-
max(1, Int(max(faceWidth, faceHeight) * 1.4))
|
|
367
|
-
)
|
|
368
|
-
let halfSide = CGFloat(cropSide) / 2
|
|
369
|
-
let centerX = min(max(box.midX * CGFloat(orientedWidth), halfSide), CGFloat(orientedWidth) - halfSide)
|
|
370
|
-
let centerY = min(max((1 - box.midY) * CGFloat(orientedHeight), halfSide), CGFloat(orientedHeight) - halfSide)
|
|
371
|
-
let cropLeft = min(orientedWidth - cropSide, max(0, Int(centerX - halfSide)))
|
|
372
|
-
let cropTop = min(orientedHeight - cropSide, max(0, Int(centerY - halfSide)))
|
|
373
|
-
let cropRight = cropLeft + cropSide - 1
|
|
374
|
-
let cropBottom = cropTop + cropSide - 1
|
|
375
|
-
let cropCenter = CGFloat(cropSide - 1) / 2
|
|
376
|
-
let rawEyeAngle = atan2(face.rightEye.y - face.leftEye.y, face.rightEye.x - face.leftEye.x)
|
|
377
|
-
let eyeAngle = abs(rawEyeAngle * 180 / .pi) >= 0.5 ? rawEyeAngle : 0
|
|
378
|
-
let cosine = Float(cos(eyeAngle))
|
|
379
|
-
let sine = Float(sin(eyeAngle))
|
|
380
|
-
let bytes = baseAddress.assumingMemoryBound(to: UInt8.self)
|
|
381
|
-
let cropCenterFloat = Float(cropCenter)
|
|
382
|
-
let cropLeftFloat = Float(cropLeft)
|
|
383
|
-
let cropTopFloat = Float(cropTop)
|
|
384
|
-
let cropRightFloat = Float(cropRight)
|
|
385
|
-
let cropBottomFloat = Float(cropBottom)
|
|
386
|
-
let sourceMaxX = Float(sourceWidth - 1)
|
|
387
|
-
let sourceMaxY = Float(sourceHeight - 1)
|
|
388
|
-
let cropOriginX = cropLeftFloat + cropCenterFloat
|
|
389
|
-
let cropOriginY = cropTopFloat + cropCenterFloat
|
|
390
|
-
let sampleScale = Float(cropSide) / Float(targetSize)
|
|
391
|
-
|
|
392
|
-
for targetY in 0..<targetSize {
|
|
393
|
-
let relativeY = (Float(targetY) + 0.5) * sampleScale - 0.5 - cropCenterFloat
|
|
394
|
-
let rowBaseX = cropOriginX - sine * relativeY
|
|
395
|
-
let rowBaseY = cropOriginY + cosine * relativeY
|
|
396
|
-
for targetX in 0..<targetSize {
|
|
397
|
-
let relativeX = (Float(targetX) + 0.5) * sampleScale - 0.5 - cropCenterFloat
|
|
398
|
-
let orientedX = min(cropRightFloat, max(cropLeftFloat, rowBaseX + cosine * relativeX))
|
|
399
|
-
let orientedY = min(cropBottomFloat, max(cropTopFloat, rowBaseY + sine * relativeX))
|
|
400
|
-
let sourceX: Float
|
|
401
|
-
let sourceY: Float
|
|
402
|
-
switch outputOrientation {
|
|
403
|
-
case .right:
|
|
404
|
-
sourceX = orientedY
|
|
405
|
-
sourceY = Float(sourceHeight - 1) - orientedX
|
|
406
|
-
case .down:
|
|
407
|
-
sourceX = sourceMaxX - orientedX
|
|
408
|
-
sourceY = sourceMaxY - orientedY
|
|
409
|
-
case .left:
|
|
410
|
-
sourceX = sourceMaxX - orientedY
|
|
411
|
-
sourceY = orientedX
|
|
412
|
-
default:
|
|
413
|
-
sourceX = orientedX
|
|
414
|
-
sourceY = orientedY
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
let destination = (targetY * targetSize + targetX) * 3
|
|
418
|
-
let boundedX = min(sourceMaxX, max(0, sourceX))
|
|
419
|
-
let boundedY = min(sourceMaxY, max(0, sourceY))
|
|
420
|
-
let x0 = Int(boundedX)
|
|
421
|
-
let y0 = Int(boundedY)
|
|
422
|
-
let x1 = min(x0 + 1, sourceWidth - 1)
|
|
423
|
-
let y1 = min(y0 + 1, sourceHeight - 1)
|
|
424
|
-
let xWeight = boundedX - Float(x0)
|
|
425
|
-
let yWeight = boundedY - Float(y0)
|
|
426
|
-
let topLeft = y0 * bytesPerRow + x0 * 4
|
|
427
|
-
let topRight = y0 * bytesPerRow + x1 * 4
|
|
428
|
-
let bottomLeft = y1 * bytesPerRow + x0 * 4
|
|
429
|
-
let bottomRight = y1 * bytesPerRow + x1 * 4
|
|
430
|
-
|
|
431
|
-
let topBlue = Float(bytes[topLeft]) * (1 - xWeight) + Float(bytes[topRight]) * xWeight
|
|
432
|
-
let bottomBlue = Float(bytes[bottomLeft]) * (1 - xWeight) + Float(bytes[bottomRight]) * xWeight
|
|
433
|
-
let topGreen = Float(bytes[topLeft + 1]) * (1 - xWeight) + Float(bytes[topRight + 1]) * xWeight
|
|
434
|
-
let bottomGreen = Float(bytes[bottomLeft + 1]) * (1 - xWeight) + Float(bytes[bottomRight + 1]) * xWeight
|
|
435
|
-
let topRed = Float(bytes[topLeft + 2]) * (1 - xWeight) + Float(bytes[topRight + 2]) * xWeight
|
|
436
|
-
let bottomRed = Float(bytes[bottomLeft + 2]) * (1 - xWeight) + Float(bytes[bottomRight + 2]) * xWeight
|
|
437
|
-
result[destination] = (topRed * (1 - yWeight) + bottomRed * yWeight) / 255
|
|
438
|
-
result[destination + 1] = (topGreen * (1 - yWeight) + bottomGreen * yWeight) / 255
|
|
439
|
-
result[destination + 2] = (topBlue * (1 - yWeight) + bottomBlue * yWeight) / 255
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
return true
|
|
443
|
-
}
|
|
444
|
-
|
|
445
341
|
private func isEligibleFace(_ face: VNFaceObservation, width: Int, height: Int) -> Bool {
|
|
446
342
|
guard width > 0, height > 0 else { return false }
|
|
447
343
|
let box = face.boundingBox
|
|
@@ -460,14 +356,33 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
460
356
|
return true
|
|
461
357
|
}
|
|
462
358
|
|
|
463
|
-
private func
|
|
464
|
-
|
|
465
|
-
let
|
|
466
|
-
|
|
359
|
+
private func findModelDirectory() throws -> String {
|
|
360
|
+
let bundles = [Bundle.main, Bundle(for: NcnnLivenessDetector.self)]
|
|
361
|
+
let subdirectories: [String?] = ["lib/assets/live", "live", nil]
|
|
362
|
+
for bundle in bundles {
|
|
363
|
+
for subdirectory in subdirectories {
|
|
364
|
+
guard let configPath = bundle.path(
|
|
365
|
+
forResource: "config",
|
|
366
|
+
ofType: "json",
|
|
367
|
+
inDirectory: subdirectory
|
|
368
|
+
) else {
|
|
369
|
+
continue
|
|
370
|
+
}
|
|
371
|
+
let directory = (configPath as NSString).deletingLastPathComponent
|
|
372
|
+
let requiredFiles = [
|
|
373
|
+
"model_1.param",
|
|
374
|
+
"model_1.bin",
|
|
375
|
+
"model_2.param",
|
|
376
|
+
"model_2.bin",
|
|
377
|
+
]
|
|
378
|
+
if requiredFiles.allSatisfy({
|
|
379
|
+
FileManager.default.fileExists(atPath: (directory as NSString).appendingPathComponent($0))
|
|
380
|
+
}) {
|
|
381
|
+
return directory
|
|
382
|
+
}
|
|
383
|
+
}
|
|
467
384
|
}
|
|
468
|
-
|
|
469
|
-
let normalizedY = 1 - (box.minY + average.y / CGFloat(points.count) * box.height)
|
|
470
|
-
return CGPoint(x: normalizedX, y: normalizedY)
|
|
385
|
+
throw RuntimeError.error(withMessage: "NCNN model assets were not found in the application or package bundle.")
|
|
471
386
|
}
|
|
472
387
|
|
|
473
388
|
private func invalidateLiveEvidence() {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
+
|
|
5
|
+
@interface NcnnLivenessDetector : NSObject
|
|
6
|
+
|
|
7
|
+
@property(nonatomic, copy, readonly) NSString *lastError;
|
|
8
|
+
|
|
9
|
+
- (BOOL)loadModelsAtPath:(NSString *)modelPath NS_SWIFT_NAME(loadModels(atPath:));
|
|
10
|
+
|
|
11
|
+
- (float)scoreForBGRABytes:(const void *)bytes
|
|
12
|
+
byteCount:(NSUInteger)byteCount
|
|
13
|
+
width:(NSInteger)width
|
|
14
|
+
height:(NSInteger)height
|
|
15
|
+
bytesPerRow:(NSInteger)bytesPerRow
|
|
16
|
+
left:(NSInteger)left
|
|
17
|
+
top:(NSInteger)top
|
|
18
|
+
right:(NSInteger)right
|
|
19
|
+
bottom:(NSInteger)bottom
|
|
20
|
+
NS_SWIFT_NAME(score(forBGRABytes:byteCount:width:height:bytesPerRow:left:top:right:bottom:));
|
|
21
|
+
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
#import "NcnnLivenessDetector.h"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#if HAS_NCNN
|
|
10
|
+
#include <ncnn/net.h>
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
struct ModelConfig {
|
|
16
|
+
float scale;
|
|
17
|
+
float shiftX;
|
|
18
|
+
float shiftY;
|
|
19
|
+
int height;
|
|
20
|
+
int width;
|
|
21
|
+
std::string name;
|
|
22
|
+
bool originalResize;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
NSError *DetectorError(NSInteger code, NSString *message) {
|
|
26
|
+
return [NSError errorWithDomain:@"NcnnLivenessDetector"
|
|
27
|
+
code:code
|
|
28
|
+
userInfo:@{NSLocalizedDescriptionKey : message}];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
bool ReadModelConfigs(NSString *configPath, std::vector<ModelConfig> &configs, NSError **error) {
|
|
32
|
+
NSData *data = [NSData dataWithContentsOfFile:configPath];
|
|
33
|
+
if (data == nil) {
|
|
34
|
+
if (error != nullptr) {
|
|
35
|
+
*error = DetectorError(1, [NSString stringWithFormat:@"Could not read model config at %@", configPath]);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
NSError *jsonError = nil;
|
|
41
|
+
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
|
|
42
|
+
if (![json isKindOfClass:[NSArray class]] || [(NSArray *)json count] == 0) {
|
|
43
|
+
if (error != nullptr) {
|
|
44
|
+
*error = jsonError ?: DetectorError(2, @"Model config must contain a non-empty JSON array");
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (id item in (NSArray *)json) {
|
|
50
|
+
if (![item isKindOfClass:[NSDictionary class]]) {
|
|
51
|
+
if (error != nullptr) {
|
|
52
|
+
*error = DetectorError(3, @"Each model config entry must be a JSON object");
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
NSDictionary *entry = (NSDictionary *)item;
|
|
58
|
+
NSString *name = entry[@"name"];
|
|
59
|
+
NSNumber *width = entry[@"width"];
|
|
60
|
+
NSNumber *height = entry[@"height"];
|
|
61
|
+
NSNumber *scale = entry[@"scale"];
|
|
62
|
+
if (![name isKindOfClass:[NSString class]] || name.length == 0 ||
|
|
63
|
+
![width isKindOfClass:[NSNumber class]] || width.intValue <= 0 ||
|
|
64
|
+
![height isKindOfClass:[NSNumber class]] || height.intValue <= 0 ||
|
|
65
|
+
![scale isKindOfClass:[NSNumber class]] || scale.floatValue <= 0) {
|
|
66
|
+
if (error != nullptr) {
|
|
67
|
+
*error = DetectorError(4, @"Model config entries require a name, positive dimensions, and positive scale");
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
configs.push_back({
|
|
73
|
+
scale.floatValue,
|
|
74
|
+
[entry[@"shift_x"] respondsToSelector:@selector(floatValue)] ? [entry[@"shift_x"] floatValue] : 0.0f,
|
|
75
|
+
[entry[@"shift_y"] respondsToSelector:@selector(floatValue)] ? [entry[@"shift_y"] floatValue] : 0.0f,
|
|
76
|
+
height.intValue,
|
|
77
|
+
width.intValue,
|
|
78
|
+
name.UTF8String ?: "",
|
|
79
|
+
[entry[@"org_resize"] respondsToSelector:@selector(boolValue)] ? [entry[@"org_resize"] boolValue] : false,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
} // namespace
|
|
87
|
+
|
|
88
|
+
@interface NcnnLivenessDetector () {
|
|
89
|
+
void *_implementation;
|
|
90
|
+
}
|
|
91
|
+
@property(nonatomic, copy, readwrite) NSString *lastError;
|
|
92
|
+
@end
|
|
93
|
+
|
|
94
|
+
class NcnnLivenessDetectorImpl {
|
|
95
|
+
public:
|
|
96
|
+
#if HAS_NCNN
|
|
97
|
+
std::vector<std::unique_ptr<ncnn::Net>> networks;
|
|
98
|
+
#endif
|
|
99
|
+
std::vector<ModelConfig> configs;
|
|
100
|
+
bool loaded = false;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
@implementation NcnnLivenessDetector
|
|
104
|
+
|
|
105
|
+
- (instancetype)init {
|
|
106
|
+
self = [super init];
|
|
107
|
+
if (self) {
|
|
108
|
+
_implementation = new NcnnLivenessDetectorImpl();
|
|
109
|
+
_lastError = @"";
|
|
110
|
+
}
|
|
111
|
+
return self;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
- (BOOL)loadModelsAtPath:(NSString *)modelPath {
|
|
115
|
+
auto *implementation = static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
116
|
+
if (implementation->loaded) {
|
|
117
|
+
return YES;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#if HAS_NCNN
|
|
121
|
+
NSError *error = nil;
|
|
122
|
+
NSString *configPath = [modelPath stringByAppendingPathComponent:@"config.json"];
|
|
123
|
+
if (!ReadModelConfigs(configPath, implementation->configs, &error)) {
|
|
124
|
+
self.lastError = error.localizedDescription;
|
|
125
|
+
return NO;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const ModelConfig &config : implementation->configs) {
|
|
129
|
+
auto network = std::make_unique<ncnn::Net>();
|
|
130
|
+
network->opt.use_vulkan_compute = false;
|
|
131
|
+
network->opt.use_fp16_storage = true;
|
|
132
|
+
|
|
133
|
+
NSString *parameterPath = [modelPath stringByAppendingPathComponent:
|
|
134
|
+
[NSString stringWithUTF8String:(config.name + ".param").c_str()]];
|
|
135
|
+
NSString *weightsPath = [modelPath stringByAppendingPathComponent:
|
|
136
|
+
[NSString stringWithUTF8String:(config.name + ".bin").c_str()]];
|
|
137
|
+
if (network->load_param(parameterPath.UTF8String) != 0 ||
|
|
138
|
+
network->load_model(weightsPath.UTF8String) != 0) {
|
|
139
|
+
self.lastError = [NSString stringWithFormat:@"Failed to load NCNN model %@", [NSString stringWithUTF8String:config.name.c_str()]];
|
|
140
|
+
implementation->networks.clear();
|
|
141
|
+
implementation->configs.clear();
|
|
142
|
+
return NO;
|
|
143
|
+
}
|
|
144
|
+
implementation->networks.push_back(std::move(network));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
implementation->loaded = true;
|
|
148
|
+
self.lastError = @"";
|
|
149
|
+
return YES;
|
|
150
|
+
#else
|
|
151
|
+
self.lastError = @"The bundled NCNN runtime is available only for physical arm64 iOS devices";
|
|
152
|
+
return NO;
|
|
153
|
+
#endif
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
- (float)scoreForBGRABytes:(const void *)bytes
|
|
157
|
+
byteCount:(NSUInteger)byteCount
|
|
158
|
+
width:(NSInteger)width
|
|
159
|
+
height:(NSInteger)height
|
|
160
|
+
bytesPerRow:(NSInteger)bytesPerRow
|
|
161
|
+
left:(NSInteger)left
|
|
162
|
+
top:(NSInteger)top
|
|
163
|
+
right:(NSInteger)right
|
|
164
|
+
bottom:(NSInteger)bottom {
|
|
165
|
+
auto *implementation = static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
166
|
+
if (!implementation->loaded) {
|
|
167
|
+
self.lastError = @"NCNN models are not loaded";
|
|
168
|
+
return NAN;
|
|
169
|
+
}
|
|
170
|
+
if (width <= 0 || height <= 0 || bytesPerRow < width * 4 ||
|
|
171
|
+
bytes == nullptr || byteCount < (NSUInteger)(bytesPerRow * height) ||
|
|
172
|
+
left < 0 || top < 0 || right <= left || bottom <= top ||
|
|
173
|
+
right > width || bottom > height) {
|
|
174
|
+
self.lastError = @"Invalid BGRA frame dimensions, buffer size, or face bounds";
|
|
175
|
+
return NAN;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#if HAS_NCNN
|
|
179
|
+
ncnn::Mat image = ncnn::Mat::from_pixels(
|
|
180
|
+
static_cast<const unsigned char *>(bytes),
|
|
181
|
+
ncnn::Mat::PIXEL_BGRA2RGB,
|
|
182
|
+
static_cast<int>(width),
|
|
183
|
+
static_cast<int>(height),
|
|
184
|
+
static_cast<int>(bytesPerRow));
|
|
185
|
+
if (image.empty()) {
|
|
186
|
+
self.lastError = @"Could not convert the BGRA camera frame to NCNN input";
|
|
187
|
+
return NAN;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const int boxWidth = static_cast<int>(right - left);
|
|
191
|
+
const int boxHeight = static_cast<int>(bottom - top);
|
|
192
|
+
float totalScore = 0.0f;
|
|
193
|
+
|
|
194
|
+
for (size_t index = 0; index < implementation->networks.size(); index++) {
|
|
195
|
+
const ModelConfig &config = implementation->configs[index];
|
|
196
|
+
ncnn::Net *network = implementation->networks[index].get();
|
|
197
|
+
const float scale = std::min(
|
|
198
|
+
static_cast<float>(height) / boxHeight,
|
|
199
|
+
std::min(static_cast<float>(width) / boxWidth, config.scale));
|
|
200
|
+
const int cropWidth = static_cast<int>(boxWidth * scale);
|
|
201
|
+
const int cropHeight = static_cast<int>(boxHeight * scale);
|
|
202
|
+
const int cropLeft = std::max(0, static_cast<int>(left - (cropWidth - boxWidth) / 2.0f));
|
|
203
|
+
const int cropTop = std::max(0, static_cast<int>(top - (cropHeight - boxHeight) / 2.0f));
|
|
204
|
+
const int boundedWidth = std::min(static_cast<int>(width) - cropLeft, cropWidth);
|
|
205
|
+
const int boundedHeight = std::min(static_cast<int>(height) - cropTop, cropHeight);
|
|
206
|
+
if (boundedWidth <= 0 || boundedHeight <= 0) {
|
|
207
|
+
self.lastError = @"The expanded face crop is empty";
|
|
208
|
+
return NAN;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
ncnn::Mat face;
|
|
212
|
+
ncnn::copy_cut_border(
|
|
213
|
+
image,
|
|
214
|
+
face,
|
|
215
|
+
cropTop,
|
|
216
|
+
static_cast<int>(height) - cropTop - boundedHeight,
|
|
217
|
+
cropLeft,
|
|
218
|
+
static_cast<int>(width) - cropLeft - boundedWidth);
|
|
219
|
+
ncnn::Mat input;
|
|
220
|
+
ncnn::resize_bilinear(face, input, config.width, config.height);
|
|
221
|
+
|
|
222
|
+
ncnn::Extractor extractor = network->create_extractor();
|
|
223
|
+
ncnn::Mat output;
|
|
224
|
+
if (input.empty() || extractor.input("data", input) != 0 ||
|
|
225
|
+
extractor.extract("softmax", output) != 0 || output.empty()) {
|
|
226
|
+
self.lastError = [NSString stringWithFormat:@"NCNN inference failed for model %@", [NSString stringWithUTF8String:config.name.c_str()]];
|
|
227
|
+
return NAN;
|
|
228
|
+
}
|
|
229
|
+
totalScore += output.w >= 2 ? output[1] : output[0];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (implementation->networks.empty()) {
|
|
233
|
+
self.lastError = @"No NCNN models are loaded";
|
|
234
|
+
return NAN;
|
|
235
|
+
}
|
|
236
|
+
self.lastError = @"";
|
|
237
|
+
return totalScore / static_cast<float>(implementation->networks.size());
|
|
238
|
+
#else
|
|
239
|
+
self.lastError = @"NCNN inference is unavailable for this build target";
|
|
240
|
+
return NAN;
|
|
241
|
+
#endif
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
- (void)dealloc {
|
|
245
|
+
delete static_cast<NcnnLivenessDetectorImpl *>(_implementation);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
@end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>AvailableLibraries</key>
|
|
6
|
+
<array>
|
|
7
|
+
<dict>
|
|
8
|
+
<key>BinaryPath</key>
|
|
9
|
+
<string>ncnn.framework/Versions/A/ncnn</string>
|
|
10
|
+
<key>LibraryIdentifier</key>
|
|
11
|
+
<string>ios-arm64</string>
|
|
12
|
+
<key>LibraryPath</key>
|
|
13
|
+
<string>ncnn.framework</string>
|
|
14
|
+
<key>SupportedArchitectures</key>
|
|
15
|
+
<array>
|
|
16
|
+
<string>arm64</string>
|
|
17
|
+
</array>
|
|
18
|
+
<key>SupportedPlatform</key>
|
|
19
|
+
<string>ios</string>
|
|
20
|
+
</dict>
|
|
21
|
+
</array>
|
|
22
|
+
<key>CFBundlePackageType</key>
|
|
23
|
+
<string>XFWK</string>
|
|
24
|
+
<key>XCFrameworkFormatVersion</key>
|
|
25
|
+
<string>1.0</string>
|
|
26
|
+
</dict>
|
|
27
|
+
</plist>
|