0

我正在尝试在我的应用程序中使用 MLKit 进行人脸检测。我正在使用 Camera 2 API 从图像读取器获取图像并将其传递给面部检测器进行处理。这在摩托罗拉、Readmi、Realme 以及大多数三星设备上运行良好,但在华硕上却失败了。我检查了日志,发现

setOnImageAvailableListener

只调用一次,而不是调用每一帧。

在下面附上我的代码片段。

imageReader = ImageReader.newInstance(
           480,
            640
            ImageFormat.YUV_420_888,
            2
        )

val previewSurface = Surface(texture)
        mPreviewBuilder!!.addTarget(previewSurface)

val recordingSurface = imageReader?.surface!!
mPreviewBuilder?.addTarget(recordingSurface!!)

mCameraDevice!!.createCaptureSession(
            listOf(previewSurface, recordingSurface),
            object : CameraCaptureSession.StateCallback() {
                override fun onConfigured(@NonNull session: CameraCaptureSession) {
                    mPreviewSession = session
                    updatePreview()

            val realTimeOpts = FaceDetectorOptions.Builder()
            .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
            .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
            .enableTracking()
            .build()

        detector = FaceDetection.getClient(realTimeOpts)

 imageReader?.setOnImageAvailableListener({ reader ->
            if (isProcessing) return@setOnImageAvailableListener
         
            reader.acquireLatestImage().let { image ->
                try {
                    isProcessing = true

                    synchronized(processorLock) {
                        // Real-time contour detection

                        detector?.process(
                            InputImage.fromMediaImage(
                                image,
                                getRotationCompensation()
                            )
                        )?.addOnSuccessListener { faces ->
                            // Task completed successfully
                            // ...
                            for (face in faces) {
                                val faceGraphic = FaceGraphic(graphicOverlay, face)
                                if (faces.isNotEmpty()) {
                                Log.d(TAG, "Face detected!")
                                }
                            }
                     
                            isProcessing = false
                            image?.close()
                        }
                            ?.addOnFailureListener { e ->
                                // Task failed with an exception
                                // ...
                                image?.close()
                                detector?.close()
                                isProcessing = false                          
                                e.printStackTrace()
                            }
                }

                override fun onConfigureFailed(@NonNull session: CameraCaptureSession) {
               
                }
            }, mBackgroundHandler
        )

任何拥有华硕设备的人都可以确认他们是否面临类似的问题。

  • 手机品牌和型号:华硕 Zenfone Max M2
  • 操作系统版本:安卓9
4

0 回答 0