android - 为什么 MLKIT 中的“检测人脸”会导致内存泄漏?
问题描述
我编写了一个关于相机的应用程序,使用来自相机的每张图像,我将其传递给班级BarCode
或DetectFace
检测条形码或面部。但是当我使用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()
}
}
解决方案
主要是因为发送到 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
推荐阅读
- sql - 如何计算数组GBQ中的重复值?
- mysql - MySQL master pushes old bin log after Heartbeat event
- python - 在 Python 中使用 SAX 解析器的标签之间的文本
- python - 确定 datetime 对象是 date 还是 datetime
- spring-boot - junit http get 请求与 csv 列表比较
- http - 试图了解如何使用 403 响应 CORS OPTIONS 请求以及何时响应
- openapi - 从 Open API Generator 生成 API 和数据模型 java 文件
- python - matplotlib 第三轴标签移位
- javascript - 表单中的单选按钮可以用于选择同一表单中的另一个单选按钮吗?
- php - 使用 PHP SDK 标记为已付款后,有什么方法可以编辑 xero 发票?