-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathRNMLKitFaceDetectionModule.swift
More file actions
42 lines (33 loc) · 1.64 KB
/
RNMLKitFaceDetectionModule.swift
File metadata and controls
42 lines (33 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import ExpoModulesCore
import MLKitFaceDetection
import RNMLKitCore
let ERROR_DOMAIN: String = "red.infinite.reactnativemlkit.FaceDetectorErrorDomain"
public class RNMLKitFaceDetectionModule: Module {
var faceDetector: RNMLKitFaceDetector? = nil
public func definition() -> ModuleDefinition {
Name("RNMLKitFaceDetection")
AsyncFunction("initialize") { (record: RNMLKitFaceDetectorOptionsRecord, promise: Promise) in
let options = RNMLKitFaceDetectorOptions(record: record)
self.faceDetector = RNMLKitFaceDetector(options: options)
promise.resolve()
}
AsyncFunction("detectFaces") { (imagePath: String, promise: Promise) in
let logger = Logger(logHandlers: [createOSLogHandler(category: Logger.EXPO_LOG_CATEGORY)])
Task {
do {
guard let faceDetector = self.faceDetector else {
rejectPromiseWithMessage(promise: promise, message: "[RNMLKitFaceDetection.detectFace] Face detector not initialized", domain: ERROR_DOMAIN)
return
}
let image = try RNMLKitImage(imagePath: imagePath)
let result = try await faceDetector.detectFaces(image: image)
logger.debug(result)
// Use result to resolve promise
promise.resolve(result.record)
} catch {
rejectPromiseWithMessage(promise: promise, message: "[RNMLKitFaceDetection.detectFace] Error Detecting Faces: \(error)", domain: ERROR_DOMAIN)
}
}
}
}
}