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

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.
@@ -34,6 +34,7 @@ Pod::Spec.new do |s|
34
34
  s.resources = ['lib/assets/*']
35
35
 
36
36
  s.dependency 'TensorFlowLiteSwift'
37
+ s.dependency 'TensorFlowLiteC/Metal'
37
38
  s.dependency 'VisionCamera'
38
39
  s.dependency 'React-jsi'
39
40
  s.dependency 'React-callinvoker'
@@ -3,6 +3,27 @@ import Foundation
3
3
  #if canImport(TensorFlowLite)
4
4
  import TensorFlowLite
5
5
  #endif
6
+ #if canImport(TensorFlowLiteCMetal)
7
+ import TensorFlowLiteCMetal
8
+ #endif
9
+
10
+ #if canImport(TensorFlowLite) && canImport(TensorFlowLiteCMetal)
11
+ private final class IOSMetalDelegate: Delegate {
12
+ let cDelegate: Delegate.CDelegate
13
+
14
+ init() {
15
+ var options = TFLGpuDelegateOptions()
16
+ options.allow_precision_loss = false
17
+ options.wait_type = TFLGpuDelegateWaitTypePassive
18
+ options.enable_quantization = true
19
+ cDelegate = TFLGpuDelegateCreate(&options)
20
+ }
21
+
22
+ deinit {
23
+ TFLGpuDelegateDelete(cDelegate)
24
+ }
25
+ }
26
+ #endif
6
27
 
7
28
  @objcMembers
8
29
  public class FaceAntiSpoofManager: NSObject {
@@ -16,7 +37,16 @@ public class FaceAntiSpoofManager: NSObject {
16
37
 
17
38
  #if canImport(TensorFlowLite)
18
39
  public static var sharedInterpreter: Interpreter?
40
+ public static private(set) var currentAccelerator = "CPU"
41
+ public static var enableGpu = true
42
+ public static var disableGpuDelegate = false
43
+ public static var enableCpuFallback = true
44
+ public static var forceCpuOnly = false
45
+ public static var cpuThreadCount = 4
19
46
  private static let interpreterLock = NSLock()
47
+ #if canImport(TensorFlowLiteCMetal)
48
+ private static var metalDelegate: IOSMetalDelegate?
49
+ #endif
20
50
  #endif
21
51
 
22
52
  @objc public class func initializeModel() -> Bool {
@@ -46,15 +76,48 @@ public class FaceAntiSpoofManager: NSObject {
46
76
  }
47
77
 
48
78
  do {
79
+ let threadCount = max(1, cpuThreadCount)
49
80
  var options = Interpreter.Options()
50
- options.threadCount = 2
51
- sharedInterpreter = try Interpreter(modelPath: path, options: options)
81
+ options.threadCount = threadCount
82
+
83
+ #if canImport(TensorFlowLiteCMetal)
84
+ if enableGpu && !disableGpuDelegate && !forceCpuOnly {
85
+ do {
86
+ let delegate = IOSMetalDelegate()
87
+ sharedInterpreter = try Interpreter(
88
+ modelPath: path,
89
+ options: options,
90
+ delegates: [delegate]
91
+ )
92
+ metalDelegate = delegate
93
+ currentAccelerator = "GPU"
94
+ NSLog("[FaceAntiSpoof] Manager: Metal delegate enabled | Threads: \(threadCount)")
95
+ } catch {
96
+ metalDelegate = nil
97
+ NSLog("[FaceAntiSpoof] Manager: Metal delegate unavailable: \(error)")
98
+ guard enableCpuFallback else { throw error }
99
+ }
100
+ }
101
+ #endif
102
+
103
+ if sharedInterpreter == nil {
104
+ guard enableCpuFallback || forceCpuOnly else {
105
+ throw NSError(domain: "FaceAntiSpoof", code: 4, userInfo: [
106
+ NSLocalizedDescriptionKey: "No enabled TensorFlow Lite accelerator is available"
107
+ ])
108
+ }
109
+ sharedInterpreter = try Interpreter(modelPath: path, options: options)
110
+ currentAccelerator = "CPU"
111
+ NSLog("[FaceAntiSpoof] Manager: CPU fallback enabled | Threads: \(threadCount)")
112
+ }
52
113
  try sharedInterpreter?.allocateTensors()
53
114
  NSLog("[FaceAntiSpoof] Manager: Loaded model at \(path)")
54
115
  return true
55
116
  } catch {
56
117
  NSLog("[FaceAntiSpoof] Manager: Failed to create interpreter: \(error)")
57
118
  sharedInterpreter = nil
119
+ metalDelegate = nil
120
+ currentAccelerator = "CPU"
58
121
  return false
59
122
  }
60
123
  #else
@@ -71,6 +134,14 @@ public class FaceAntiSpoofManager: NSObject {
71
134
  #endif
72
135
  }
73
136
 
137
+ @objc public class func accelerator() -> String {
138
+ #if canImport(TensorFlowLite)
139
+ return currentAccelerator
140
+ #else
141
+ return "UNAVAILABLE"
142
+ #endif
143
+ }
144
+
74
145
  public class func process(input: [Float]) throws -> DetectionResult {
75
146
  #if canImport(TensorFlowLite)
76
147
  if sharedInterpreter == nil && !initializeModel() {
@@ -158,7 +229,11 @@ public class FaceAntiSpoofManager: NSObject {
158
229
 
159
230
  @objc public class func cleanupModel() {
160
231
  #if canImport(TensorFlowLite)
232
+ interpreterLock.lock()
161
233
  sharedInterpreter = nil
234
+ metalDelegate = nil
235
+ currentAccelerator = "CPU"
236
+ interpreterLock.unlock()
162
237
  #endif
163
238
  NSLog("[FaceAntiSpoof] Manager: cleaned up model")
164
239
  }
@@ -110,10 +110,26 @@ RCT_REMAP_METHOD(getModuleInfo,
110
110
  getModuleInfoWithResolver:(RCTPromiseResolveBlock)resolve
111
111
  rejecter:(RCTPromiseRejectBlock)reject)
112
112
  {
113
- NSDictionary *info = @{@"name": @"FaceAntiSpoof", @"methods": @[@"initialize", @"checkModelStatus", @"isAvailable", @"testMethod", @"getModuleInfo", @"install", @"cleanup"]};
113
+ NSDictionary *info = @{ @"name": @"FaceAntiSpoof", @"methods": @[@"initialize", @"checkModelStatus", @"isAvailable", @"testMethod", @"getModuleInfo", @"install", @"cleanup", @"getAccelerator"], @"accelerators": @[@"GPU", @"CPU"] };
114
114
  resolve(info);
115
115
  }
116
116
 
117
+ RCT_REMAP_METHOD(getAccelerator,
118
+ getAcceleratorWithResolver:(RCTPromiseResolveBlock)resolve
119
+ rejecter:(RCTPromiseRejectBlock)reject)
120
+ {
121
+ Class managerClass = NSClassFromString(@"FaceAntiSpoofManager");
122
+ if (managerClass != Nil) {
123
+ SEL sel = NSSelectorFromString(@"accelerator");
124
+ if ([managerClass respondsToSelector:sel]) {
125
+ id (*msgSendObject)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
126
+ resolve(msgSendObject((id)managerClass, sel));
127
+ return;
128
+ }
129
+ }
130
+ resolve(@"UNAVAILABLE");
131
+ }
132
+
117
133
  RCT_REMAP_METHOD(install,
118
134
  installWithResolver:(RCTPromiseResolveBlock)resolve
119
135
  rejecter:(RCTPromiseRejectBlock)reject)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.1.25",
3
+ "version": "1.1.26",
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": {