react-native-face-match-sdk 0.0.0-stage → 1.0.3

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.
Files changed (59) hide show
  1. package/FaceMatch.podspec +55 -0
  2. package/LICENSE +21 -0
  3. package/README.md +190 -2
  4. package/android/build.gradle +59 -0
  5. package/android/consumer-rules.pro +5 -0
  6. package/android/src/main/AndroidManifest.xml +11 -0
  7. package/android/src/main/java/com/facematch/sdk/AntiSpoofHelper.kt +216 -0
  8. package/android/src/main/java/com/facematch/sdk/BitmapUtils.kt +138 -0
  9. package/android/src/main/java/com/facematch/sdk/CameraFrameView.kt +75 -0
  10. package/android/src/main/java/com/facematch/sdk/FaceAlignmentHelper.kt +156 -0
  11. package/android/src/main/java/com/facematch/sdk/FaceCameraView.kt +628 -0
  12. package/android/src/main/java/com/facematch/sdk/FaceCameraViewManager.kt +79 -0
  13. package/android/src/main/java/com/facematch/sdk/FaceCandidateFilter.kt +65 -0
  14. package/android/src/main/java/com/facematch/sdk/FaceDetectionHelper.kt +225 -0
  15. package/android/src/main/java/com/facematch/sdk/FaceEmbeddingHelper.kt +242 -0
  16. package/android/src/main/java/com/facematch/sdk/FaceFrameAnalyzer.kt +228 -0
  17. package/android/src/main/java/com/facematch/sdk/FaceMatchModule.kt +249 -0
  18. package/android/src/main/java/com/facematch/sdk/FaceMatchPackage.kt +14 -0
  19. package/android/src/main/java/com/facematch/sdk/FaceMatchRuntime.kt +138 -0
  20. package/android/src/main/java/com/facematch/sdk/FaceMatcherHelper.kt +92 -0
  21. package/android/src/main/java/com/facematch/sdk/FaceOverlayView.kt +236 -0
  22. package/android/src/main/java/com/facematch/sdk/LivenessDetector.kt +483 -0
  23. package/android/src/main/java/com/facematch/sdk/LivenessPrompts.kt +137 -0
  24. package/android/src/main/java/com/facematch/sdk/LivenessVoice.kt +270 -0
  25. package/android/src/main/java/com/facematch/sdk/PrivateCaptureStore.kt +50 -0
  26. package/ios/Sources/AntiSpoofHelper.swift +239 -0
  27. package/ios/Sources/FaceAlignmentHelper.swift +169 -0
  28. package/ios/Sources/FaceCameraView.swift +1161 -0
  29. package/ios/Sources/FaceCameraViewManager.m +28 -0
  30. package/ios/Sources/FaceCameraViewManager.swift +57 -0
  31. package/ios/Sources/FaceCandidateFilter.swift +62 -0
  32. package/ios/Sources/FaceDetectionHelper.swift +283 -0
  33. package/ios/Sources/FaceEmbeddingHelper.swift +188 -0
  34. package/ios/Sources/FaceFrameAnalyzer.swift +179 -0
  35. package/ios/Sources/FaceFrameResult.swift +96 -0
  36. package/ios/Sources/FaceMatchBundle.swift +26 -0
  37. package/ios/Sources/FaceMatchEngine.swift +136 -0
  38. package/ios/Sources/FaceMatchError.swift +40 -0
  39. package/ios/Sources/FaceMatchModule.m +55 -0
  40. package/ios/Sources/FaceMatchModule.swift +288 -0
  41. package/ios/Sources/FaceMatcherHelper.swift +107 -0
  42. package/ios/Sources/FaceOverlayView.swift +285 -0
  43. package/ios/Sources/ImageUtils.swift +322 -0
  44. package/ios/Sources/LivenessDetector.swift +505 -0
  45. package/ios/Sources/LivenessPrompts.swift +121 -0
  46. package/ios/Sources/LivenessVoice.swift +229 -0
  47. package/ios/Sources/OnnxRuntime/ORTShim.h +91 -0
  48. package/ios/Sources/OnnxRuntime/ORTShim.mm +260 -0
  49. package/ios/Sources/PrivateCaptureStore.swift +71 -0
  50. package/ios/Sources/VisionFaceFallback.swift +95 -0
  51. package/ios/onnxruntime-c.podspec +47 -0
  52. package/models/anti_spoof_minifasnet_v2.onnx +0 -0
  53. package/models/w600k_mbf.onnx +0 -0
  54. package/package.json +47 -4
  55. package/react-native.config.js +16 -0
  56. package/src/FaceCameraView.tsx +75 -0
  57. package/src/FaceMatch.ts +66 -0
  58. package/src/index.ts +4 -0
  59. package/src/types.ts +87 -0
@@ -0,0 +1,169 @@
1
+ import CoreGraphics
2
+ import Foundation
3
+
4
+ /// Port of `FaceAlignmentHelper.kt`.
5
+ ///
6
+ /// Aligns a detected face to the InsightFace standard 112x112 crop using a
7
+ /// 5-point similarity transform (Umeyama algorithm). The reference landmarks
8
+ /// match those used by the Python backend's buffalo_sc model, so embeddings
9
+ /// produced here are directly comparable to server-side and Android ones.
10
+ ///
11
+ /// Landmark order: left_eye, right_eye, nose_tip, left_mouth, right_mouth.
12
+ enum FaceAlignmentHelper {
13
+ static let faceSize = 112
14
+
15
+ /// InsightFace reference 5-point positions for a 112x112 aligned crop,
16
+ /// in top-left-origin pixel coordinates.
17
+ private static let referenceLandmarks: [CGFloat] = [
18
+ 38.2946, 51.6963, // left eye
19
+ 73.5318, 51.5014, // right eye
20
+ 56.0252, 71.7366, // nose tip
21
+ 41.5493, 92.3655, // left mouth corner
22
+ 70.7299, 92.2041 // right mouth corner
23
+ ]
24
+
25
+ /// Tightly crops the face region using the bounding box plus a margin on
26
+ /// each side (default 30%). Giving the Umeyama transform a tight face
27
+ /// region rather than the whole image avoids precision loss and guarantees
28
+ /// the 112x112 output is filled by the face, not by background.
29
+ ///
30
+ /// - Returns: the cropped image and the landmarks translated into its space.
31
+ static func cropFaceRegion(
32
+ _ image: CGImage,
33
+ face: DetectedFace,
34
+ marginFraction: CGFloat = 0.30
35
+ ) -> (image: CGImage, landmarks: [CGFloat]) {
36
+ let box = face.boundingBox
37
+ let marginX = box.width * marginFraction
38
+ let marginY = box.height * marginFraction
39
+
40
+ let x0 = max(box.minX - marginX, 0).rounded(.down)
41
+ let y0 = max(box.minY - marginY, 0).rounded(.down)
42
+ let x1 = min(box.maxX + marginX, CGFloat(image.width)).rounded(.down)
43
+ let y1 = min(box.maxY + marginY, CGFloat(image.height)).rounded(.down)
44
+
45
+ let cropWidth = max(x1 - x0, 1)
46
+ let cropHeight = max(y1 - y0, 1)
47
+ let rect = CGRect(x: x0, y: y0, width: cropWidth, height: cropHeight)
48
+
49
+ guard let cropped = ImageUtils.crop(image, to: rect) else {
50
+ return (image, face.landmarks)
51
+ }
52
+
53
+ var adjusted = face.landmarks
54
+ for index in adjusted.indices {
55
+ adjusted[index] -= (index % 2 == 0) ? x0 : y0
56
+ }
57
+ return (cropped, adjusted)
58
+ }
59
+
60
+ /// Warps `image` so that `srcLandmarks` (flat x,y pairs in top-left-origin
61
+ /// pixel coordinates) align to the reference 112x112 positions.
62
+ static func alignFace(_ image: CGImage, srcLandmarks: [CGFloat]) throws -> CGImage {
63
+ guard srcLandmarks.count == 10 else {
64
+ throw FaceMatchError(
65
+ "E_FACE_MATCH",
66
+ "Need 5 landmarks (10 floats), got \(srcLandmarks.count)"
67
+ )
68
+ }
69
+
70
+ // Android solves this in Canvas space (origin top-left, y down);
71
+ // CoreGraphics draws in origin bottom-left, y up. Reflecting the y axis
72
+ // of BOTH point sets conjugates the similarity transform by that
73
+ // reflection, which is still a reflection-free similarity, so the
74
+ // solver output can be used as a CGAffineTransform directly.
75
+ let sourceHeight = CGFloat(image.height)
76
+ let targetHeight = CGFloat(faceSize)
77
+
78
+ var source = srcLandmarks
79
+ var target = referenceLandmarks
80
+ for index in stride(from: 1, to: source.count, by: 2) {
81
+ source[index] = sourceHeight - source[index]
82
+ target[index] = targetHeight - target[index]
83
+ }
84
+
85
+ let transform = estimateSimilarityTransform(source: source, target: target)
86
+
87
+ guard let context = CGContext(
88
+ data: nil,
89
+ width: faceSize,
90
+ height: faceSize,
91
+ bitsPerComponent: 8,
92
+ bytesPerRow: 0,
93
+ space: CGColorSpaceCreateDeviceRGB(),
94
+ bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
95
+ | CGBitmapInfo.byteOrder32Little.rawValue
96
+ ) else {
97
+ throw FaceMatchError("E_FACE_MATCH", "Unable to allocate the aligned face buffer")
98
+ }
99
+
100
+ // Bilinear filtering for a smoother, higher-quality warped crop.
101
+ context.interpolationQuality = .high
102
+ context.concatenate(transform)
103
+ context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
104
+
105
+ guard let aligned = context.makeImage() else {
106
+ throw FaceMatchError("E_FACE_MATCH", "Unable to render the aligned face")
107
+ }
108
+ return aligned
109
+ }
110
+
111
+ // -----------------------------------------------------------------------
112
+ // Umeyama similarity-transform estimation
113
+ // -----------------------------------------------------------------------
114
+ // For 2D correspondences the optimal similarity transform (rotation,
115
+ // uniform scale, translation - no reflection) has a closed-form solution:
116
+ //
117
+ // a = Sum(sx*dx + sy*dy) / Sum(sx^2 + sy^2)
118
+ // b = Sum(sx*dy - sy*dx) / Sum(sx^2 + sy^2)
119
+ //
120
+ // where (sx,sy) = src point - src centroid, (dx,dy) = dst point - dst
121
+ // centroid. The resulting affine matrix is [ a -b tx ; b a ty ], which
122
+ // encodes scale = sqrt(a^2+b^2) and angle = atan2(b, a).
123
+ // -----------------------------------------------------------------------
124
+ private static func estimateSimilarityTransform(
125
+ source: [CGFloat],
126
+ target: [CGFloat]
127
+ ) -> CGAffineTransform {
128
+ let count = source.count / 2
129
+
130
+ var srcMeanX = 0.0, srcMeanY = 0.0
131
+ var dstMeanX = 0.0, dstMeanY = 0.0
132
+ for index in 0..<count {
133
+ srcMeanX += Double(source[2 * index])
134
+ srcMeanY += Double(source[2 * index + 1])
135
+ dstMeanX += Double(target[2 * index])
136
+ dstMeanY += Double(target[2 * index + 1])
137
+ }
138
+ srcMeanX /= Double(count); srcMeanY /= Double(count)
139
+ dstMeanX /= Double(count); dstMeanY /= Double(count)
140
+
141
+ var a = 0.0, b = 0.0, srcVariance = 0.0
142
+ for index in 0..<count {
143
+ let sx = Double(source[2 * index]) - srcMeanX
144
+ let sy = Double(source[2 * index + 1]) - srcMeanY
145
+ let dx = Double(target[2 * index]) - dstMeanX
146
+ let dy = Double(target[2 * index + 1]) - dstMeanY
147
+ a += sx * dx + sy * dy
148
+ b += sx * dy - sy * dx
149
+ srcVariance += sx * sx + sy * sy
150
+ }
151
+
152
+ // Degenerate case: all source points are identical.
153
+ if srcVariance < 1e-10 { return .identity }
154
+
155
+ a /= srcVariance
156
+ b /= srcVariance
157
+
158
+ let tx = dstMeanX - a * srcMeanX + b * srcMeanY
159
+ let ty = dstMeanY - b * srcMeanX - a * srcMeanY
160
+
161
+ // CGAffineTransform maps (x,y) -> (a*x + c*y + tx, b*x + d*y + ty),
162
+ // so the [a -b tx ; b a ty] matrix becomes (a: a, b: b, c: -b, d: a).
163
+ return CGAffineTransform(
164
+ a: CGFloat(a), b: CGFloat(b),
165
+ c: CGFloat(-b), d: CGFloat(a),
166
+ tx: CGFloat(tx), ty: CGFloat(ty)
167
+ )
168
+ }
169
+ }