react-native-vision-camera-spoof-detector 1.1.24 → 1.1.25

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.
@@ -1,5 +1,4 @@
1
1
  import AVFoundation
2
- import CoreImage
3
2
  import Foundation
4
3
  import NitroModules
5
4
  import VisionCamera
@@ -8,11 +7,12 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
8
7
  private let queue = DispatchQueue(label: "com.faceantispoof.output", qos: .userInitiated)
9
8
  private let options: SpoofDetectorOutputOptions
10
9
  private let delegate: SpoofDetectorDelegate
11
- private let ciContext = CIContext(options: nil)
12
10
  private let stateLock = NSLock()
13
11
  private let targetSize = 256
14
12
  private var isBusy = false
15
13
  private var isDisposed = false
14
+ private var frameCounter = 0
15
+ private var lastFrameBrightness: Float = 0
16
16
 
17
17
  let output = AVCaptureVideoDataOutput()
18
18
  let requiresAudioInput = false
@@ -27,7 +27,10 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
27
27
  }
28
28
 
29
29
  var targetResolution: ResolutionRule {
30
- return .closestTo(margelo.nitro.camera.Size(width: 720.0, height: 1280.0))
30
+ let resolution = options.targetResolution.map {
31
+ margelo.nitro.camera.Size(width: $0.width, height: $0.height)
32
+ } ?? margelo.nitro.camera.Size(width: 720.0, height: 1280.0)
33
+ return .closestTo(resolution)
31
34
  }
32
35
 
33
36
  init(options: SpoofDetectorOutputOptions) {
@@ -35,6 +38,9 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
35
38
  self.delegate = SpoofDetectorDelegate()
36
39
  super.init()
37
40
 
41
+ output.videoSettings = [
42
+ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
43
+ ]
38
44
  delegate.onSampleBuffer = { [weak self] sampleBuffer in
39
45
  self?.process(sampleBuffer)
40
46
  }
@@ -63,7 +69,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
63
69
  isBusy = true
64
70
  stateLock.unlock()
65
71
 
66
- guard let input = makeRGBInput(from: sampleBuffer) else {
72
+ guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
67
73
  stateLock.lock()
68
74
  isBusy = false
69
75
  stateLock.unlock()
@@ -71,69 +77,115 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
71
77
  return
72
78
  }
73
79
 
74
- queue.async { [weak self] in
75
- guard let self else { return }
76
- defer {
77
- self.stateLock.lock()
78
- self.isBusy = false
79
- self.stateLock.unlock()
80
- }
80
+ CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
81
+ defer {
82
+ CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
83
+ }
81
84
 
82
- do {
83
- let result = try FaceAntiSpoofManager.process(input: input)
84
- self.options.onResult(
85
- AntiSpoofingResult(
86
- isLive: result.isLive,
87
- label: result.isLive ? "Live Face" : "Spoof Face",
88
- neuralNetworkScore: result.neuralNetworkScore,
89
- laplacianScore: result.laplacianScore,
90
- combinedScore: result.combinedScore,
91
- confidence: result.confidence
92
- )
93
- )
94
- } catch {
95
- self.options.onError?(error)
96
- }
85
+ guard shouldProcessFrame(pixelBuffer),
86
+ let input = makeRGBInput(from: pixelBuffer) else {
87
+ stateLock.lock()
88
+ isBusy = false
89
+ stateLock.unlock()
90
+ return
97
91
  }
98
- }
99
92
 
100
- private func makeRGBInput(from sampleBuffer: CMSampleBuffer) -> [Float]? {
101
- guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }
93
+ defer {
94
+ stateLock.lock()
95
+ isBusy = false
96
+ stateLock.unlock()
97
+ }
102
98
 
103
- let image = CIImage(cvPixelBuffer: pixelBuffer)
104
- let oriented = image.oriented(forExifOrientation: outputOrientation.exifOrientation)
105
- guard let cgImage = ciContext.createCGImage(oriented, from: oriented.extent) else {
106
- return nil
99
+ do {
100
+ let result = try FaceAntiSpoofManager.process(input: input)
101
+ options.onResult(
102
+ AntiSpoofingResult(
103
+ isLive: result.isLive,
104
+ label: result.isLive ? "Live Face" : "Spoof Face",
105
+ neuralNetworkScore: result.neuralNetworkScore,
106
+ laplacianScore: result.laplacianScore,
107
+ combinedScore: result.combinedScore,
108
+ confidence: result.confidence
109
+ )
110
+ )
111
+ } catch {
112
+ options.onError?(error)
107
113
  }
114
+ }
108
115
 
109
- let width = targetSize
110
- let height = targetSize
111
- let colorSpace = CGColorSpaceCreateDeviceRGB()
112
- var bytes = [UInt8](repeating: 0, count: width * height * 4)
113
- let rendered = bytes.withUnsafeMutableBytes { rawBuffer -> Bool in
114
- guard let baseAddress = rawBuffer.baseAddress,
115
- let context = CGContext(
116
- data: baseAddress,
117
- width: width,
118
- height: height,
119
- bitsPerComponent: 8,
120
- bytesPerRow: width * 4,
121
- space: colorSpace,
122
- bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
123
- ) else { return false }
124
- context.interpolationQuality = .high
125
- context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
126
- return true
116
+ private func shouldProcessFrame(_ pixelBuffer: CVPixelBuffer) -> Bool {
117
+ frameCounter += 1
118
+ guard frameCounter % 3 == 0 else { return false }
119
+
120
+ let width = CVPixelBufferGetWidth(pixelBuffer)
121
+ let height = CVPixelBufferGetHeight(pixelBuffer)
122
+ let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
123
+ guard let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else { return false }
124
+ let bytes = baseAddress.assumingMemoryBound(to: UInt8.self)
125
+ let sampleStep = max(1, min(width, height) / 32)
126
+ var brightnessSum: Float = 0
127
+ var brightnessSquaredSum: Float = 0
128
+ var sampleCount: Float = 0
129
+
130
+ for y in stride(from: 0, to: height, by: sampleStep) {
131
+ for x in stride(from: 0, to: width, by: sampleStep) {
132
+ let offset = y * bytesPerRow + x * 4
133
+ let blue = Float(bytes[offset])
134
+ let green = Float(bytes[offset + 1])
135
+ let red = Float(bytes[offset + 2])
136
+ let luma = 0.299 * red + 0.587 * green + 0.114 * blue
137
+ brightnessSum += luma
138
+ brightnessSquaredSum += luma * luma
139
+ sampleCount += 1
140
+ }
127
141
  }
128
- guard rendered else { return nil }
129
-
130
- var result = [Float](repeating: 0, count: width * height * 3)
131
- for index in 0..<(width * height) {
132
- let source = index * 4
133
- let destination = index * 3
134
- result[destination] = Float(bytes[source]) / 255.0
135
- result[destination + 1] = Float(bytes[source + 1]) / 255.0
136
- result[destination + 2] = Float(bytes[source + 2]) / 255.0
142
+
143
+ guard sampleCount > 0 else { return false }
144
+ let average = brightnessSum / sampleCount
145
+ let variance = (brightnessSquaredSum / sampleCount) - (average * average)
146
+ let brightnessDelta = abs(average - lastFrameBrightness)
147
+ let isFirstSample = lastFrameBrightness == 0
148
+ lastFrameBrightness = average
149
+
150
+ return average >= 24 && average <= 220 && variance >= 1500 &&
151
+ (isFirstSample || brightnessDelta >= 8)
152
+ }
153
+
154
+ private func makeRGBInput(from pixelBuffer: CVPixelBuffer) -> [Float]? {
155
+ guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
156
+ let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else { return nil }
157
+
158
+ let sourceWidth = CVPixelBufferGetWidth(pixelBuffer)
159
+ let sourceHeight = CVPixelBufferGetHeight(pixelBuffer)
160
+ let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
161
+ let bytes = baseAddress.assumingMemoryBound(to: UInt8.self)
162
+ var result = [Float](repeating: 0, count: targetSize * targetSize * 3)
163
+ let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
164
+ let orientedWidth = isQuarterTurn ? sourceHeight : sourceWidth
165
+ let orientedHeight = isQuarterTurn ? sourceWidth : sourceHeight
166
+
167
+ for targetY in 0..<targetSize {
168
+ let orientedY = min(orientedHeight - 1, targetY * orientedHeight / targetSize)
169
+ for targetX in 0..<targetSize {
170
+ let orientedX = min(orientedWidth - 1, targetX * orientedWidth / targetSize)
171
+ let sourcePoint: (x: Int, y: Int)
172
+ switch outputOrientation {
173
+ case .right:
174
+ sourcePoint = (orientedY, sourceHeight - 1 - orientedX)
175
+ case .down:
176
+ sourcePoint = (sourceWidth - 1 - orientedX, sourceHeight - 1 - orientedY)
177
+ case .left:
178
+ sourcePoint = (sourceWidth - 1 - orientedY, orientedX)
179
+ default:
180
+ sourcePoint = (orientedX, orientedY)
181
+ }
182
+
183
+ let source = sourcePoint.y * bytesPerRow + sourcePoint.x * 4
184
+ let destination = (targetY * targetSize + targetX) * 3
185
+ result[destination] = Float(bytes[source + 2]) / 255
186
+ result[destination + 1] = Float(bytes[source + 1]) / 255
187
+ result[destination + 2] = Float(bytes[source]) / 255
188
+ }
137
189
  }
138
190
  return result
139
191
  }
@@ -150,15 +202,3 @@ private final class SpoofDetectorDelegate: NSObject, AVCaptureVideoDataOutputSam
150
202
  onSampleBuffer?(sampleBuffer)
151
203
  }
152
204
  }
153
-
154
- private extension CameraOrientation {
155
- var exifOrientation: Int32 {
156
- switch self {
157
- case .up: return 1
158
- case .right: return 6
159
- case .down: return 3
160
- case .left: return 8
161
- @unknown default: return 1
162
- }
163
- }
164
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
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": {