无法使用CameraView在Android上使用ML Kit检测人脸

时间:2019-05-26 16:38:16

标签: android android-camera face-detection firebase-mlkit

我正在尝试在Android上运行此example of face detection with the firebase MLkit。我使用库CameraView代替内置的CameraView。 运行代码,日志中将显示:Faces:[],因此从相机中找不到面孔。

代码:

    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
                        // ...
                    }
                })
}}

在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" />

也对Fotoapparat进行了相同的尝试,结果相同:未找到面孔。我在这里想念什么?

3 个答案:

答案 0 :(得分:0)

请尝试以编程方式从frame获取高度和宽度,而不是对其进行硬编码。

答案 1 :(得分:0)

您应使用“ CameraSourcePreview”进行检测检出find Here

答案 2 :(得分:0)

我认为问题是frame.rotation值。也许您可以将FirebaseVisionImageMetadata.ROTATION_90用于FirebaseVisionImageMetadata