react-native-vision-camera-spoof-detector 1.2.6 → 1.2.7

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.
@@ -56,7 +56,9 @@ class HybridSpoofDetectorOutput(
56
56
  if (!enabled) return@let null
57
57
  FaceDetection.getClient(
58
58
  FaceDetectorOptions.Builder()
59
- .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
59
+ .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)
60
+ .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)
61
+ .setMinFaceSize(MIN_FACE_WIDTH_RATIO)
60
62
  .enableTracking()
61
63
  .build()
62
64
  )
@@ -84,6 +86,10 @@ class HybridSpoofDetectorOutput(
84
86
  private var lastResultUpdateAtMs = 0L
85
87
  private var imageAnalysis: ImageAnalysis? = null
86
88
  private companion object {
89
+ private const val MIN_FACE_WIDTH_RATIO = 0.18f
90
+ private const val MIN_FACE_HEIGHT_RATIO = 0.18f
91
+ private const val FACE_FRAME_MARGIN_RATIO = 0.02f
92
+ private const val MAX_FACE_POSE_ANGLE_DEGREES = 25f
87
93
  private const val MIN_FRAME_BRIGHTNESS = 24f
88
94
  private const val MAX_FRAME_BRIGHTNESS = 220f
89
95
  private const val MIN_VARIANCE_FOR_ANALYSIS = 1500f
@@ -149,11 +155,14 @@ class HybridSpoofDetectorOutput(
149
155
  val mediaImage = image.image ?: throw IllegalStateException("ImageProxy has no media image")
150
156
  val inputImage = InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees)
151
157
  val transform = createMLKitToCameraTransform(image)
158
+ val rotation = ((image.imageInfo.rotationDegrees % 360) + 360) % 360
159
+ val detectorFrameWidth = if (rotation == 90 || rotation == 270) image.height else image.width
160
+ val detectorFrameHeight = if (rotation == 90 || rotation == 270) image.width else image.height
152
161
  val task = faceDetector.process(inputImage)
153
162
  faceDetectorTask.set(task)
154
163
  task.addOnSuccessListener { faces ->
155
164
  try {
156
- processDetectedFaces(faces, transform)
165
+ processDetectedFaces(faces, transform, detectorFrameWidth, detectorFrameHeight)
157
166
  } catch (error: Throwable) {
158
167
  options.onError?.invoke(error)
159
168
  }
@@ -203,8 +212,16 @@ class HybridSpoofDetectorOutput(
203
212
  }
204
213
  }
205
214
 
206
- private fun processDetectedFaces(faces: List<Face>, transform: Matrix) {
207
- val detectedFaces = faces.map { it.toSpoofDetectedFace(transform) }.toTypedArray()
215
+ private fun processDetectedFaces(
216
+ faces: List<Face>,
217
+ transform: Matrix,
218
+ frameWidth: Int,
219
+ frameHeight: Int,
220
+ ) {
221
+ val detectedFaces = faces
222
+ .filter { it.isFullFace(frameWidth, frameHeight) }
223
+ .map { it.toSpoofDetectedFace(transform) }
224
+ .toTypedArray()
208
225
  val now = SystemClock.elapsedRealtime()
209
226
  var shouldNotify = false
210
227
  var capture: Pair<SpoofDetectedFaceBounds, AntiSpoofingResult>? = null
@@ -379,6 +396,36 @@ class HybridSpoofDetectorOutput(
379
396
  )
380
397
  }
381
398
 
399
+ private fun Face.isFullFace(frameWidth: Int, frameHeight: Int): Boolean {
400
+ if (frameWidth <= 0 || frameHeight <= 0) return false
401
+
402
+ val bounds = boundingBox
403
+ val marginX = frameWidth * FACE_FRAME_MARGIN_RATIO
404
+ val marginY = frameHeight * FACE_FRAME_MARGIN_RATIO
405
+ if (bounds.left < marginX || bounds.top < marginY ||
406
+ bounds.right > frameWidth - marginX || bounds.bottom > frameHeight - marginY
407
+ ) return false
408
+ if (bounds.width() < frameWidth * MIN_FACE_WIDTH_RATIO ||
409
+ bounds.height() < frameHeight * MIN_FACE_HEIGHT_RATIO
410
+ ) return false
411
+ if (abs(headEulerAngleY) > MAX_FACE_POSE_ANGLE_DEGREES ||
412
+ abs(headEulerAngleX) > MAX_FACE_POSE_ANGLE_DEGREES
413
+ ) return false
414
+
415
+ val requiredLandmarks = intArrayOf(
416
+ com.google.mlkit.vision.face.FaceLandmark.LEFT_EYE,
417
+ com.google.mlkit.vision.face.FaceLandmark.RIGHT_EYE,
418
+ com.google.mlkit.vision.face.FaceLandmark.NOSE_BASE,
419
+ com.google.mlkit.vision.face.FaceLandmark.MOUTH_LEFT,
420
+ com.google.mlkit.vision.face.FaceLandmark.MOUTH_RIGHT,
421
+ )
422
+ return requiredLandmarks.all { landmarkType ->
423
+ val point = getLandmark(landmarkType)?.position ?: return@all false
424
+ point.x >= bounds.left && point.x <= bounds.right &&
425
+ point.y >= bounds.top && point.y <= bounds.bottom
426
+ }
427
+ }
428
+
382
429
  override fun dispose() {
383
430
  disposed.set(true)
384
431
  imageAnalysis?.clearAnalyzer()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
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": {