package expo.modules.facedetector import android.content.Context import android.os.Bundle import expo.modules.core.ExportedModule import expo.modules.core.interfaces.ExpoMethod import expo.modules.core.ModuleRegistry import expo.modules.core.ModuleRegistryDelegate import expo.modules.core.Promise import expo.modules.interfaces.facedetector.FaceDetectorInterface import expo.modules.interfaces.facedetector.FaceDetectorProviderInterface import expo.modules.facedetector.tasks.FileFaceDetectionTask import expo.modules.facedetector.tasks.FileFaceDetectionCompletionListener import java.util.* private const val TAG = "ExpoFaceDetector" private const val MODE_OPTION_KEY = "Mode" private const val DETECT_LANDMARKS_OPTION_KEY = "Landmarks" private const val RUN_CLASSIFICATIONS_OPTION_KEY = "Classifications" class FaceDetectorModule( context: Context?, private val moduleRegistryDelegate: ModuleRegistryDelegate = ModuleRegistryDelegate() ) : ExportedModule(context) { private inline fun moduleRegistry() = moduleRegistryDelegate.getFromModuleRegistry() override fun getName() = TAG override fun getConstants() = mapOf( MODE_OPTION_KEY to faceDetectionModeConstants, DETECT_LANDMARKS_OPTION_KEY to faceDetectionLandmarksConstants, RUN_CLASSIFICATIONS_OPTION_KEY to faceDetectionClassificationsConstants ) override fun onCreate(moduleRegistry: ModuleRegistry) { moduleRegistryDelegate.onCreate(moduleRegistry) } @ExpoMethod fun detectFaces(options: HashMap, promise: Promise) { // TODO: Check file scope FileFaceDetectionTask( detectorForOptions(options, context), options, object : FileFaceDetectionCompletionListener { override fun resolve(result: Bundle) = promise.resolve(result) override fun reject(tag: String, message: String) = promise.reject(tag, message, null) } ).execute() } private fun detectorForOptions(options: HashMap, context: Context): FaceDetectorInterface { val faceDetectorProvider: FaceDetectorProviderInterface by moduleRegistry() val faceDetector = faceDetectorProvider.createFaceDetectorWithContext(context) faceDetector.setSettings(options) return faceDetector } }