首页 > 解决方案 > 为什么 MLKIT 中的“检测人脸”会导致内存泄漏?

问题描述

我编写了一个关于相机的应用程序,使用来自相机的每张图像,我将其传递给班级BarCodeDetectFace检测条形码或面部。但是当我使用DetectFace.

如果我使用它是安全的BarCode

override fun onImageAvailable(reader: ImageReader) {
        imageOnFrame = reader.acquireNextImage()
        barcode = BarCode(this@MainActivity, imageOnFrame, getRotation("0"))
        barcode!!.run()
        barcode = null
        imageOnFrame.close()
    }

但是如果我使用DetectFace,这会导致内存泄漏:

override fun onImageAvailable(reader: ImageReader) {
        imageOnFrame = reader.acquireNextImage()
        detectFace = DetectFace(this@MainActivity, imageOnFrame, getRotation("0"))
        detectFace!!.run()
        detectFace = null  
        imageOnFrame.close()
    }

这是类DetectFace

class DetectFace(private val context: Context, private val image: Image, private val rotation: Int) {

    private val option = FirebaseVisionFaceDetectorOptions.Builder()
            .setModeType(FirebaseVisionFaceDetectorOptions.ACCURATE_MODE)
            .setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
            .setClassificationType(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
            .setTrackingEnabled(true)
            .setMinFaceSize(0.01f)
            .build()

    fun run(){
        val image = FirebaseVisionImage.fromMediaImage(this.image, rotation)
        val detector = FirebaseVision.getInstance()
                .getVisionFaceDetector(option)

        detector.detectInImage(image)
                .addOnSuccessListener {
                    // code ..
                }
                .addOnFailureListener{
                    // code ..
                }
        this.image.close()
    }
}

标签: androidperformancefirebase-mlkit

解决方案


主要是因为发送到 ML Kit 的图片速度太快,无法及时处理,内存占用增加。我们将提供一种在 ML Kit 中正确处理它的方法。同时,您可以限制发送到 ML Kit 的图像。

例如,ML Kit 快速启动示例应用程序在此处执行图像限制: https ://github.com/firebase/quickstart-android/blob/master/mlkit/app/src/main/java/com/google/firebase/样本/应用程序/mlkit/VisionProcessorBase.java#L49


推荐阅读