react-native-vision-camera-spoof-detector 1.2.8 → 1.2.10
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
CHANGED
|
@@ -257,6 +257,7 @@ Result fields:
|
|
|
257
257
|
- Uses an `AVCaptureVideoDataOutput` pipeline with a native sample-buffer delegate
|
|
258
258
|
- Converts BGRA pixel buffers to a normalized RGB input for the detector
|
|
259
259
|
- Filters frames using brightness and variance checks before inference
|
|
260
|
+
- Uses automatic camera-output mirroring and only constrains resolution when `targetResolution` is provided
|
|
260
261
|
- Returns the evaluation result through the same `onResult` callback contract
|
|
261
262
|
|
|
262
263
|
## Troubleshooting
|
|
@@ -301,4 +302,3 @@ MIT. See [LICENSE](./LICENSE).
|
|
|
301
302
|
## Author
|
|
302
303
|
|
|
303
304
|
JESCON TECHNOLOGIES PVT LTD
|
|
304
|
-
|
|
@@ -7,4 +7,8 @@ final class HybridSpoofDetectorFactory: HybridSpoofDetectorFactorySpec {
|
|
|
7
7
|
{
|
|
8
8
|
return HybridSpoofDetectorOutput(options: options)
|
|
9
9
|
}
|
|
10
|
+
|
|
11
|
+
func resetSpoofDetectorOutput(output: any HybridCameraOutputSpec) throws {
|
|
12
|
+
(output as? HybridSpoofDetectorOutput)?.resetState()
|
|
13
|
+
}
|
|
10
14
|
}
|
|
@@ -6,6 +6,13 @@ import Vision
|
|
|
6
6
|
import VisionCamera
|
|
7
7
|
|
|
8
8
|
final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutput {
|
|
9
|
+
private struct DetectedFace {
|
|
10
|
+
let observation: VNFaceObservation
|
|
11
|
+
let face: SpoofDetectedFace
|
|
12
|
+
let leftEye: CGPoint
|
|
13
|
+
let rightEye: CGPoint
|
|
14
|
+
}
|
|
15
|
+
|
|
9
16
|
private let queue = DispatchQueue(label: "com.faceantispoof.output", qos: .userInitiated)
|
|
10
17
|
private let options: SpoofDetectorOutputOptions
|
|
11
18
|
private let delegate: SpoofDetectorDelegate
|
|
@@ -13,8 +20,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
13
20
|
private let targetSize = 256
|
|
14
21
|
private var isBusy = false
|
|
15
22
|
private var isDisposed = false
|
|
16
|
-
private var frameCounter = 0
|
|
17
|
-
private var lastFrameBrightness: Float = 0
|
|
18
23
|
private var captured = false
|
|
19
24
|
private var stableCount = 0
|
|
20
25
|
private var lastFaceBounds: SpoofDetectedFaceBounds?
|
|
@@ -25,12 +30,12 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
25
30
|
private var lastFacesUpdateAt = 0.0
|
|
26
31
|
private var lastResultUpdateAt = 0.0
|
|
27
32
|
|
|
28
|
-
private var antispoofLevel: Double { options.antispoofLevel ?? 0.
|
|
29
|
-
private var stableFrames: Int { max(1, Int(options.stableFrames ??
|
|
30
|
-
private var requiredLiveFrames: Int { max(1, Int(options.requiredLiveFrames ??
|
|
33
|
+
private var antispoofLevel: Double { options.antispoofLevel ?? 0.4 }
|
|
34
|
+
private var stableFrames: Int { max(1, Int(options.stableFrames ?? 3)) }
|
|
35
|
+
private var requiredLiveFrames: Int { max(1, Int(options.requiredLiveFrames ?? 3)) }
|
|
31
36
|
private var laplacianThreshold: Double { options.laplacianThreshold ?? 2000 }
|
|
32
37
|
private var maxNeuralSpoofScore: Double { options.maxNeuralSpoofScore ?? 0.1 }
|
|
33
|
-
private var uiUpdateInterval: Double { max(0, options.uiUpdateIntervalMs ??
|
|
38
|
+
private var uiUpdateInterval: Double { max(0, options.uiUpdateIntervalMs ?? 100) / 1000 }
|
|
34
39
|
private var isLowMemoryDevice: Bool {
|
|
35
40
|
ProcessInfo.processInfo.physicalMemory <= 4 * 1024 * 1024 * 1024
|
|
36
41
|
}
|
|
@@ -48,9 +53,13 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
var targetResolution: ResolutionRule {
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
}
|
|
56
|
+
guard let requestedSize = options.targetResolution else {
|
|
57
|
+
return .any
|
|
58
|
+
}
|
|
59
|
+
let requestedResolution = margelo.nitro.camera.Size(
|
|
60
|
+
width: requestedSize.width,
|
|
61
|
+
height: requestedSize.height
|
|
62
|
+
)
|
|
54
63
|
let resolution: margelo.nitro.camera.Size
|
|
55
64
|
if isLowMemoryDevice && requestedResolution.width > 640 {
|
|
56
65
|
let scale = 640 / requestedResolution.width
|
|
@@ -82,6 +91,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
82
91
|
func configure(config: OutputConfiguration) {
|
|
83
92
|
guard let connection = output.connection(with: .video) else { return }
|
|
84
93
|
connection.preferredVideoStabilizationMode = .off
|
|
94
|
+
connection.automaticallyAdjustsVideoMirroring = true
|
|
85
95
|
}
|
|
86
96
|
|
|
87
97
|
func dispose() {
|
|
@@ -130,12 +140,12 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
130
140
|
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
|
131
141
|
}
|
|
132
142
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
143
|
+
let faces = detectFaces(pixelBuffer)
|
|
144
|
+
processDetectedFaces(faces.map(\.face))
|
|
136
145
|
|
|
137
|
-
guard shouldProcessFrame(pixelBuffer),
|
|
138
|
-
let input = makeRGBInput(from: pixelBuffer) else {
|
|
146
|
+
guard shouldProcessFrame(pixelBuffer), faces.count == 1,
|
|
147
|
+
let input = makeRGBInput(from: pixelBuffer, face: faces[0]) else {
|
|
148
|
+
invalidateLiveEvidence()
|
|
139
149
|
stateLock.lock()
|
|
140
150
|
isBusy = false
|
|
141
151
|
stateLock.unlock()
|
|
@@ -150,10 +160,13 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
150
160
|
|
|
151
161
|
do {
|
|
152
162
|
let result = try FaceAntiSpoofManager.process(input: input)
|
|
163
|
+
let isLive = result.neuralNetworkScore < maxNeuralSpoofScore
|
|
164
|
+
&& result.confidence >= antispoofLevel
|
|
165
|
+
&& result.laplacianScore >= laplacianThreshold
|
|
153
166
|
processAntiSpoofResult(
|
|
154
167
|
AntiSpoofingResult(
|
|
155
|
-
isLive:
|
|
156
|
-
label:
|
|
168
|
+
isLive: isLive,
|
|
169
|
+
label: isLive ? "Live Face" : "Spoof Face",
|
|
157
170
|
neuralNetworkScore: result.neuralNetworkScore,
|
|
158
171
|
laplacianScore: result.laplacianScore,
|
|
159
172
|
combinedScore: result.combinedScore,
|
|
@@ -161,6 +174,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
161
174
|
)
|
|
162
175
|
)
|
|
163
176
|
} catch {
|
|
177
|
+
invalidateLiveEvidence()
|
|
164
178
|
options.onError?(error)
|
|
165
179
|
}
|
|
166
180
|
}
|
|
@@ -174,8 +188,8 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
174
188
|
}
|
|
175
189
|
}
|
|
176
190
|
|
|
177
|
-
private func detectFaces(_ pixelBuffer: CVPixelBuffer) -> [
|
|
178
|
-
let request =
|
|
191
|
+
private func detectFaces(_ pixelBuffer: CVPixelBuffer) -> [DetectedFace] {
|
|
192
|
+
let request = VNDetectFaceLandmarksRequest()
|
|
179
193
|
do {
|
|
180
194
|
try VNImageRequestHandler(
|
|
181
195
|
cvPixelBuffer: pixelBuffer,
|
|
@@ -185,13 +199,31 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
185
199
|
|
|
186
200
|
let width = CVPixelBufferGetWidth(pixelBuffer)
|
|
187
201
|
let height = CVPixelBufferGetHeight(pixelBuffer)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
202
|
+
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
203
|
+
let orientedWidth = isQuarterTurn ? height : width
|
|
204
|
+
let orientedHeight = isQuarterTurn ? width : height
|
|
205
|
+
return (request.results ?? []).compactMap { observation in
|
|
206
|
+
guard isEligibleFace(observation, width: orientedWidth, height: orientedHeight),
|
|
207
|
+
let landmarks = observation.landmarks,
|
|
208
|
+
let leftEye = landmarkCenter(landmarks.leftEye, in: observation.boundingBox),
|
|
209
|
+
let rightEye = landmarkCenter(landmarks.rightEye, in: observation.boundingBox) else {
|
|
210
|
+
return nil
|
|
211
|
+
}
|
|
212
|
+
let leftEyePoint = CGPoint(
|
|
213
|
+
x: leftEye.x * CGFloat(orientedWidth),
|
|
214
|
+
y: leftEye.y * CGFloat(orientedHeight)
|
|
215
|
+
)
|
|
216
|
+
let rightEyePoint = CGPoint(
|
|
217
|
+
x: rightEye.x * CGFloat(orientedWidth),
|
|
218
|
+
y: rightEye.y * CGFloat(orientedHeight)
|
|
219
|
+
)
|
|
220
|
+
return DetectedFace(
|
|
221
|
+
observation: observation,
|
|
222
|
+
face: SpoofDetectedFace(
|
|
223
|
+
bounds: makeBufferBounds(from: observation.boundingBox, width: width, height: height)
|
|
224
|
+
),
|
|
225
|
+
leftEye: leftEyePoint,
|
|
226
|
+
rightEye: rightEyePoint
|
|
195
227
|
)
|
|
196
228
|
}
|
|
197
229
|
} catch {
|
|
@@ -221,7 +253,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
221
253
|
lastFacesUpdateAt = now
|
|
222
254
|
options.onFacesDetected?(faces)
|
|
223
255
|
}
|
|
224
|
-
notifyStableFaceIfReady()
|
|
225
256
|
}
|
|
226
257
|
|
|
227
258
|
private func processAntiSpoofResult(_ result: AntiSpoofingResult) {
|
|
@@ -230,19 +261,12 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
230
261
|
consecutiveLiveFrames = 0
|
|
231
262
|
isLive = false
|
|
232
263
|
} else {
|
|
233
|
-
let
|
|
234
|
-
let
|
|
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
|
|
264
|
+
let meetsConfidence = result.confidence >= antispoofLevel
|
|
265
|
+
let meetsLaplacian = result.laplacianScore >= laplacianThreshold
|
|
242
266
|
if meetsConfidence && meetsLaplacian {
|
|
243
267
|
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
244
268
|
} else {
|
|
245
|
-
consecutiveLiveFrames =
|
|
269
|
+
consecutiveLiveFrames = 0
|
|
246
270
|
}
|
|
247
271
|
isLive = consecutiveLiveFrames >= requiredLiveFrames
|
|
248
272
|
}
|
|
@@ -298,9 +322,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
298
322
|
}
|
|
299
323
|
|
|
300
324
|
private func shouldProcessFrame(_ pixelBuffer: CVPixelBuffer) -> Bool {
|
|
301
|
-
frameCounter += 1
|
|
302
|
-
guard frameCounter % 3 == 0 else { return false }
|
|
303
|
-
|
|
304
325
|
let width = CVPixelBufferGetWidth(pixelBuffer)
|
|
305
326
|
let height = CVPixelBufferGetHeight(pixelBuffer)
|
|
306
327
|
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
@@ -308,7 +329,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
308
329
|
let bytes = baseAddress.assumingMemoryBound(to: UInt8.self)
|
|
309
330
|
let sampleStep = max(1, min(width, height) / 32)
|
|
310
331
|
var brightnessSum: Float = 0
|
|
311
|
-
var brightnessSquaredSum: Float = 0
|
|
312
332
|
var sampleCount: Float = 0
|
|
313
333
|
|
|
314
334
|
for y in stride(from: 0, to: height, by: sampleStep) {
|
|
@@ -319,23 +339,16 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
319
339
|
let red = Float(bytes[offset + 2])
|
|
320
340
|
let luma = 0.299 * red + 0.587 * green + 0.114 * blue
|
|
321
341
|
brightnessSum += luma
|
|
322
|
-
brightnessSquaredSum += luma * luma
|
|
323
342
|
sampleCount += 1
|
|
324
343
|
}
|
|
325
344
|
}
|
|
326
345
|
|
|
327
346
|
guard sampleCount > 0 else { return false }
|
|
328
347
|
let average = brightnessSum / sampleCount
|
|
329
|
-
|
|
330
|
-
let brightnessDelta = abs(average - lastFrameBrightness)
|
|
331
|
-
let isFirstSample = lastFrameBrightness == 0
|
|
332
|
-
lastFrameBrightness = average
|
|
333
|
-
|
|
334
|
-
return average >= 24 && average <= 220 && variance >= 1500 &&
|
|
335
|
-
(isFirstSample || brightnessDelta >= 8)
|
|
348
|
+
return average >= 24 && average <= 220
|
|
336
349
|
}
|
|
337
350
|
|
|
338
|
-
private func makeRGBInput(from pixelBuffer: CVPixelBuffer) -> [Float]? {
|
|
351
|
+
private func makeRGBInput(from pixelBuffer: CVPixelBuffer, face: DetectedFace) -> [Float]? {
|
|
339
352
|
guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
|
|
340
353
|
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else { return nil }
|
|
341
354
|
|
|
@@ -347,11 +360,23 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
347
360
|
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
348
361
|
let orientedWidth = isQuarterTurn ? sourceHeight : sourceWidth
|
|
349
362
|
let orientedHeight = isQuarterTurn ? sourceWidth : sourceHeight
|
|
363
|
+
let box = face.observation.boundingBox
|
|
364
|
+
let faceWidth = box.width * CGFloat(orientedWidth)
|
|
365
|
+
let faceHeight = box.height * CGFloat(orientedHeight)
|
|
366
|
+
let cropSide = min(min(orientedWidth, orientedHeight), max(1, Int((max(faceWidth, faceHeight) * 1.4).rounded())))
|
|
367
|
+
let halfSide = CGFloat(cropSide) / 2
|
|
368
|
+
let centerX = min(max(box.midX * CGFloat(orientedWidth), halfSide), CGFloat(orientedWidth) - halfSide)
|
|
369
|
+
let centerY = min(max((1 - box.midY) * CGFloat(orientedHeight), halfSide), CGFloat(orientedHeight) - halfSide)
|
|
370
|
+
let eyeAngle = atan2(face.rightEye.y - face.leftEye.y, face.rightEye.x - face.leftEye.x)
|
|
371
|
+
let cosine = cos(eyeAngle)
|
|
372
|
+
let sine = sin(eyeAngle)
|
|
350
373
|
|
|
351
374
|
for targetY in 0..<targetSize {
|
|
352
|
-
let
|
|
375
|
+
let cropY = (CGFloat(targetY) + 0.5) * CGFloat(cropSide) / CGFloat(targetSize) - halfSide
|
|
353
376
|
for targetX in 0..<targetSize {
|
|
354
|
-
let
|
|
377
|
+
let cropX = (CGFloat(targetX) + 0.5) * CGFloat(cropSide) / CGFloat(targetSize) - halfSide
|
|
378
|
+
let orientedX = min(orientedWidth - 1, max(0, Int((centerX + cosine * cropX - sine * cropY).rounded())))
|
|
379
|
+
let orientedY = min(orientedHeight - 1, max(0, Int((centerY + sine * cropX + cosine * cropY).rounded())))
|
|
355
380
|
let sourcePoint: (x: Int, y: Int)
|
|
356
381
|
switch outputOrientation {
|
|
357
382
|
case .right:
|
|
@@ -373,6 +398,40 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
373
398
|
}
|
|
374
399
|
return result
|
|
375
400
|
}
|
|
401
|
+
|
|
402
|
+
private func isEligibleFace(_ face: VNFaceObservation, width: Int, height: Int) -> Bool {
|
|
403
|
+
guard width > 0, height > 0 else { return false }
|
|
404
|
+
let box = face.boundingBox
|
|
405
|
+
let marginX: CGFloat = 0.02
|
|
406
|
+
let marginY: CGFloat = 0.02
|
|
407
|
+
guard box.minX >= marginX, box.maxX <= 1 - marginX,
|
|
408
|
+
box.minY >= marginY, box.maxY <= 1 - marginY,
|
|
409
|
+
box.width >= 0.18, box.height >= 0.18,
|
|
410
|
+
abs(face.yaw?.doubleValue ?? 0) <= 25 * .pi / 180,
|
|
411
|
+
abs(face.pitch?.doubleValue ?? 0) <= 25 * .pi / 180,
|
|
412
|
+
let landmarks = face.landmarks,
|
|
413
|
+
landmarks.leftEye != nil, landmarks.rightEye != nil,
|
|
414
|
+
landmarks.nose != nil, landmarks.outerLips != nil else {
|
|
415
|
+
return false
|
|
416
|
+
}
|
|
417
|
+
return true
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private func landmarkCenter(_ landmark: VNFaceLandmarkRegion2D?, in box: CGRect) -> CGPoint? {
|
|
421
|
+
guard let points = landmark?.normalizedPoints, !points.isEmpty else { return nil }
|
|
422
|
+
let average = points.reduce(CGPoint.zero) { partial, point in
|
|
423
|
+
CGPoint(x: partial.x + point.x, y: partial.y + point.y)
|
|
424
|
+
}
|
|
425
|
+
let normalizedX = box.minX + average.x / CGFloat(points.count) * box.width
|
|
426
|
+
let normalizedY = 1 - (box.minY + average.y / CGFloat(points.count) * box.height)
|
|
427
|
+
return CGPoint(x: normalizedX, y: normalizedY)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private func invalidateLiveEvidence() {
|
|
431
|
+
consecutiveLiveFrames = 0
|
|
432
|
+
isLive = false
|
|
433
|
+
lastSpoofResult = nil
|
|
434
|
+
}
|
|
376
435
|
}
|
|
377
436
|
|
|
378
437
|
private final class SpoofDetectorDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-vision-camera-spoof-detector",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"description": "High-performance face anti-spoofing and liveness detection module for React Native Vision Camera. Uses TensorFlow Lite with GPU acceleration and optimized YUV processing.",
|
|
5
5
|
"homepage": "https://github.com/dpraful/react-native-vision-camera-spoof-detector",
|
|
6
6
|
"repository": {
|