[英]FirebaseFaceDetection crash with CameraX
將 FirebaseFaceDetetion 與 CameraX 集成可以很好地與“setContourMode(FirebaseVisionFaceDetectorOptions.ALL_CONTOURS)”配合使用。 但是,當我將它與“.setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)”一起使用時,會出現 OutOfMemory 異常('java.lang.OutOfMemoryError:無法分配 518412 字節分配,其中 231440 字節和 226KB 直到 OOM')。
public void analyze(ImageProxy imageProxy, int degrees) {
if (imageProxy == null || imageProxy.getImage() == null) {
return;
}
Image mediaImage = imageProxy.getImage();
int rotation = degreesToFirebaseRotation(degrees);
FirebaseVisionImage image =
FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
FirebaseVisionFaceDetectorOptions configs = new FirebaseVisionFaceDetectorOptions.Builder()
.setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
.build();
FirebaseVisionFaceDetector detector = FirebaseVision.getInstance()
.getVisionFaceDetector(configs);
detector.detectInImage(image).addOnSuccessListener(...) });
}
我希望在沒有任何 memory 問題的情況下工作,但一段時間后它會崩潰。
OOM 可能是由於輸入圖像太大而引發的; 嘗試在檢測之前縮小規模。 您還可以為 Android 仿真器分配大量 RAM,但這不是可移植的解決方案,因為硬件設備有一定的硬限制。
CameraX API 處於 alpha 階段( BackpressureStrategy , setImageQueueDepth ):所以我使用了以下解決方法:
解決方法:僅在人臉檢測未進行時進行分析。
public class MainActivity extends AppCompatActivity {
volatile boolean detecting = false;
private void startCamera() {
//.....
//.....
imageAnalysis.setAnalyzer(new MainActivity.DirectExecutor(),
(image, rotationDegrees) -> {
//Face Detection analysis start
//only after previous detection completed
if (!detecting)
analyzeImage(image, rotationDegrees);
});
CameraX.bindToLifecycle((LifecycleOwner) this, imageAnalysis, preview);
}
private void analyzeImage(ImageProxy imageProxy, int degrees) {
//...
//...
detecting = true; //detection true on detection starts
//.. detectio code goes here
detector.detectInImage(image)
.addOnSuccessListener(
new OnSuccessListener<List<FirebaseVisionFace>>() {
@Override
public void onSuccess(List<FirebaseVisionFace> faces) {
if (faces.size() > 0) {
//do come business logics here...
detecting = false;
}
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
detecting = false;
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.