ML-KIT Face Detector无法检测人脸问题求助
ML-Kit + CameraX 人脸检测返回0人脸的解决方法
1. 修正图像旋转角度
CameraX的ImageProxy自带传感器旋转信息,硬编码90度旋转会导致部分设备上人脸方向偏移/颠倒,ML-Kit无法识别。应使用imageProxy.imageInfo.rotationDegrees获取正确旋转值:
override fun onCaptureSuccess(imageProxy: ImageProxy) { val bitmap = imageProxy.image?.let { BitmapUtils().convertProxyImageInBitMap(it) } bitmap?.let { val rotationDegrees = imageProxy.imageInfo.rotationDegrees val photo = BitmapUtils().rotateBitmap(it, rotationDegrees.toFloat()) detectFaces(photo, returnIntent) } // 必须关闭ImageProxy,避免CameraX资源泄漏 imageProxy.close() }
2. 跳过Bitmap转换,直接使用MediaImage
Bitmap转换过程可能导致图像格式失真或信息丢失,推荐直接用ImageProxy创建InputImage,效率更高且检测更准确:
override fun onCaptureSuccess(imageProxy: ImageProxy) { val inputImage = InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees) detectFaces(inputImage, returnIntent) imageProxy.close() } // 调整detectFaces函数参数 private fun detectFaces(inputImage: InputImage, intent: Intent){ if (::faceDetector.isInitialized) { faceDetector.process(inputImage) .addOnSuccessListener { faces -> if (faces.size >= 1) { // 处理人脸检测成功逻辑 } } .addOnFailureListener { it.printStackTrace() } } }
3. 检查Bitmap转换工具类
如果必须使用Bitmap,确保convertProxyImageInBitMap正确处理CameraX默认输出的YUV_420_888格式,示例实现:
fun convertProxyImageInBitMap(image: Image): Bitmap { val yBuffer = image.planes[0].buffer val uBuffer = image.planes[1].buffer val vBuffer = image.planes[2].buffer val ySize = yBuffer.remaining() val uSize = uBuffer.remaining() val vSize = vBuffer.remaining() val nv21 = ByteArray(ySize + uSize + vSize) yBuffer.get(nv21, 0, ySize) vBuffer.get(nv21, ySize, vSize) uBuffer.get(nv21, ySize + vSize, uSize) val yuvImage = YuvImage(nv21, ImageFormat.NV21, image.width, image.height, null) val outputStream = ByteArrayOutputStream() yuvImage.compressToJpeg(Rect(0, 0, image.width, image.height), 100, outputStream) val imageBytes = outputStream.toByteArray() return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size) }
4. 优化FaceDetector配置
调整检测器参数,确保不会过滤掉小尺寸人脸:
faceDetectorOptions = FaceDetectorOptions.Builder() .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE) .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL) .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL) .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL) .setMinFaceSize(0.15f) // 人脸占图像比例至少15%,可根据需求调整 .build()
内容的提问来源于stack exchange,提问作者Zannardy A. Oliveira
相关产品推荐
相关产品推荐

