Sunday, 2 June 2019

Cant detect Faces with ML Kit on Android using CameraView

I am trying to run this example of face detection with the firebase MLkit on Android. Instead of the built in CameraView I use the library CameraView. Running the code the logs says: Faces: [], so no faces were found from the camera.

the code:

    camera = findViewById<CameraView>(R.id.camera)
    camera.setLifecycleOwner(this)

    val realTimeOpts = FirebaseVisionFaceDetectorOptions.Builder()
        .setContourMode(FirebaseVisionFaceDetectorOptions.ALL_CONTOURS)
        .build()

    FirebaseApp.initializeApp(this);


    camera.addFrameProcessor { frame ->
        val data = frame.data
        val rotation = frame.rotation
        val metadata = FirebaseVisionImageMetadata.Builder()
            .setWidth(480) // 480x360 is typically sufficient for
            .setHeight(360) // image recognition
            .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_YV12)
            .setRotation(3)
            .build()
        val image = FirebaseVisionImage.fromByteArray(data, metadata)
        val detector = FirebaseVision.getInstance()
            .getVisionFaceDetector(realTimeOpts)

        val result = detector.detectInImage(image)
            .addOnSuccessListener { faces ->
                // Task completed successfully
                // ...
                Log.e("Faces",faces.toString())
                for (face in faces) {
                    val bounds = face.boundingBox
                    val rotY = face.headEulerAngleY // Head is rotated to the right rotY degrees
                    val rotZ = face.headEulerAngleZ // Head is tilted sideways rotZ degrees

                    // If landmark detection was enabled (mouth, ears, eyes, cheeks, and
                    // nose available):
                    val leftEar = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EAR)
                    leftEar?.let {
                        val leftEarPos = leftEar.position
                    }

                    // If contour detection was enabled:
                    val leftEyeContour = face.getContour(FirebaseVisionFaceContour.LEFT_EYE).points
                    val upperLipBottomContour = face.getContour(FirebaseVisionFaceContour.UPPER_LIP_BOTTOM).points

                    // If classification was enabled:
                    if (face.smilingProbability != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
                        val smileProb = face.smilingProbability
                    }
                    if (face.rightEyeOpenProbability != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
                        val rightEyeOpenProb = face.rightEyeOpenProbability
                    }

                    // If face tracking was enabled:
                    if (face.trackingId != FirebaseVisionFace.INVALID_ID) {
                        val id = face.trackingId
                    }
                }
    }.addOnFailureListener(
                object : OnFailureListener {
                    override fun onFailure(e: Exception) {
                        // Task failed with an exception
                        // ...
                    }
                })
}}

in activity_main.xml:

     <com.otaliastudios.cameraview.CameraView
        android:id="@+id/camera"
        app:cameraFacing="front"
        android:keepScreenOn="true"
        android:layout_width="280dp"
        android:layout_height="280dp" />

Tried the same also with Fotoapparat, same result: No faces found. What am I missing here?



from Cant detect Faces with ML Kit on Android using CameraView

No comments:

Post a Comment