{"record":{"id":"997e25fd9723cf41","repo":"microg/GmsCore","slug":"facedetectoryn-initialization-failed","errorCode":null,"errorMessage":"faceDetectorYN initialization failed","messagePattern":"faceDetectorYN initialization failed","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"play-services-vision/core/src/main/kotlin/org/microg/gms/vision/face/FaceDetectorHelper.kt","lineNumber":50,"sourceCode":"\nconst val TAG = \"FaceDetection\"\n\nclass FaceDetectorHelper(context: Context) {\n\n    private var faceDetectorYN: FaceDetectorYN? = null\n    private var inputSize = Size(320.0, 320.0)\n\n    init {\n        try {\n            val buffer: ByteArray\n            context.assets.open(\"face_detection_yunet_2023mar.onnx\").use {\n                val size = it.available()\n                buffer = ByteArray(size)\n                it.read(buffer)\n            }\n            faceDetectorYN = FaceDetectorYN.create(\"onnx\", MatOfByte(*buffer), MatOfByte(), inputSize, 0.7f, 0.3f, 5000)\n        } catch (e: Exception) {\n            throw RuntimeException(\"faceDetectorYN initialization failed\")\n        }\n    }\n\n    fun detectFaces(bitmap: Bitmap, rotation: Int): List<FaceParcel> {\n        Log.d(TAG, \"detectFaces: source is bitmap\")\n        val rootMat = bitmapToMat(bitmap) ?: return emptyList()\n        return processMat(rootMat, rotation)\n    }\n\n    fun detectFaces(nv21ByteArray: ByteArray, width: Int, height: Int, rotation: Int): List<FaceParcel> {\n        Log.d(TAG, \"detectFaces: source is nv21Buffer\")\n        val rootMat = nv21ToMat(nv21ByteArray, width, height) ?: return emptyList()\n        return processMat(rootMat, rotation)\n    }\n\n    fun detectFaces(image: Image, rotation: Int): List<FaceParcel> {\n        Log.d(TAG, \"detectFaces: source is image\")\n        val rootMat = imageToMat(image) ?: return emptyList()","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-vision/core/src/main/kotlin/org/microg/gms/vision/face/FaceDetectorHelper.kt#L32-L68","documentation":"FaceDetectorHelper's constructor loads an ONNX face-detection model from assets and calls OpenCV FaceDetectorYN.create; any exception during model load/creation is rethrown as this RuntimeException. It means the bundled DNN model could not be initialized, so face detection is unusable.","triggerScenarios":"Constructing FaceDetector (which instantiates FaceDetectorHelper) when the model asset is missing/corrupt, asset read fails, inputSize is invalid, or OpenCV's DNN module can't parse the onnx model.","commonSituations":"OpenCV dependency without dnn/onnx support; corrupt or truncated asset file; too-large inputSize for available memory; mismatched OpenCV version lacking FaceDetectorYN.","solutions":["Verify the onnx model file exists in src/main/assets (or wherever `assets.open` reads it) and is not truncated.","Ensure your OpenCV dependency is >= 4.5.1 and includes the dnn module.","Log the original exception (`throw RuntimeException(\"faceDetectorYN initialization failed\", e)`) to see the root cause before blaming config.","Reduce/validate inputSize and try the default 300x300; test FaceDetectorYN.create in isolation."],"exampleFix":"// before\n} catch (e: Exception) {\n    throw RuntimeException(\"faceDetectorYN initialization failed\")\n}\n// after\n} catch (e: Exception) {\n    throw RuntimeException(\"faceDetectorYN initialization failed\", e)\n}","handlingStrategy":"validation","validationCode":"val model = assets.list(\"\")?.contains(\"onnx\") ?: false\nif (!model || !OpenCVLoader.initDebug()) failFast(\"OpenCV/model unavailable\")","typeGuard":null,"tryCatchPattern":"try {\n    val detector = FaceDetector(context)\n} catch (e: RuntimeException) {\n    Log.e(TAG, \"Face detection unavailable: ${e.message}\", e)\n    disableFeature()\n}","preventionTips":["Verify the onnx asset ships in the APK (check build output)","Use OpenCV >= 4.5.1 with dnn module","Fail fast at app start, not mid-frame","Log root causes (wrap the original exception)"],"tags":["android","opencv","onnx","model-loading","microg"],"backgroundTag":"module-init-failed","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}