microg/GmsCore · critical · RuntimeException

faceDetectorYN initialization failed

Error message

faceDetectorYN initialization failed

What it means

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.

Source

Thrown at play-services-vision/core/src/main/kotlin/org/microg/gms/vision/face/FaceDetectorHelper.kt:50

const val TAG = "FaceDetection"

class FaceDetectorHelper(context: Context) {

    private var faceDetectorYN: FaceDetectorYN? = null
    private var inputSize = Size(320.0, 320.0)

    init {
        try {
            val buffer: ByteArray
            context.assets.open("face_detection_yunet_2023mar.onnx").use {
                val size = it.available()
                buffer = ByteArray(size)
                it.read(buffer)
            }
            faceDetectorYN = FaceDetectorYN.create("onnx", MatOfByte(*buffer), MatOfByte(), inputSize, 0.7f, 0.3f, 5000)
        } catch (e: Exception) {
            throw RuntimeException("faceDetectorYN initialization failed")
        }
    }

    fun detectFaces(bitmap: Bitmap, rotation: Int): List<FaceParcel> {
        Log.d(TAG, "detectFaces: source is bitmap")
        val rootMat = bitmapToMat(bitmap) ?: return emptyList()
        return processMat(rootMat, rotation)
    }

    fun detectFaces(nv21ByteArray: ByteArray, width: Int, height: Int, rotation: Int): List<FaceParcel> {
        Log.d(TAG, "detectFaces: source is nv21Buffer")
        val rootMat = nv21ToMat(nv21ByteArray, width, height) ?: return emptyList()
        return processMat(rootMat, rotation)
    }

    fun detectFaces(image: Image, rotation: Int): List<FaceParcel> {
        Log.d(TAG, "detectFaces: source is image")
        val rootMat = imageToMat(image) ?: return emptyList()

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Verify the onnx model file exists in src/main/assets (or wherever `assets.open` reads it) and is not truncated.
  2. Ensure your OpenCV dependency is >= 4.5.1 and includes the dnn module.
  3. Log the original exception (`throw RuntimeException("faceDetectorYN initialization failed", e)`) to see the root cause before blaming config.
  4. Reduce/validate inputSize and try the default 300x300; test FaceDetectorYN.create in isolation.

Example fix

// before
} catch (e: Exception) {
    throw RuntimeException("faceDetectorYN initialization failed")
}
// after
} catch (e: Exception) {
    throw RuntimeException("faceDetectorYN initialization failed", e)
}
Defensive patterns

Strategy: validation

Validate before calling

val model = assets.list("")?.contains("onnx") ?: false
if (!model || !OpenCVLoader.initDebug()) failFast("OpenCV/model unavailable")

Try / catch

try {
    val detector = FaceDetector(context)
} catch (e: RuntimeException) {
    Log.e(TAG, "Face detection unavailable: ${e.message}", e)
    disableFeature()
}

Prevention

When it happens

Trigger: 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.

Common situations: OpenCV dependency without dnn/onnx support; corrupt or truncated asset file; too-large inputSize for available memory; mismatched OpenCV version lacking FaceDetectorYN.

Related errors


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/997e25fd9723cf41. Report an issue: GitHub.