簡體   English   中英

ImageAnalysis 用例 CameraX 人臉檢測

[英]ImageAnalysis usecase CameraX Facedetection

我正在嘗試開發一個應用程序,使用CameraXMLKit Facedetecion API分析相機的實時幀。 它在預覽用例中工作正常,但圖像分析用例根本不起作用。

我不明白為什么,因為我正在關注最新的官方文檔。 我的代碼:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previewView = findViewById(R.id.previewView);
    if (previewView == null) {
        Toast.makeText(this,
            "PreviewView not found",
            Toast.LENGTH_SHORT).show();
    }
    iv = findViewById(R.id.face_image_view);
    if (iv == null) {
        Toast.makeText(this,
            "ImageView not found",
            Toast.LENGTH_SHORT).show();
    }

    cameraProviderFuture = ProcessCameraProvider.getInstance(this);

    cameraProviderFuture.addListener(() - > {
        try {
            ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
            if (allPermissionsGranted()) {
                bindPreview(cameraProvider);
                Log.i(TAG, "here1");
            } else {
                ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSION);
            }


        } catch (ExecutionException | InterruptedException e) {
            // No errors need to be handled for this Future.
            // This should never be reached.
        }
    }, ContextCompat.getMainExecutor(this));
}

void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
    cameraProvider.unbindAll();

    //bind preview
    Preview preview = new Preview.Builder()
        .build();

    CameraSelector cameraSelector = new CameraSelector.Builder()
        .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
        .build();

    preview.setSurfaceProvider(previewView.getSurfaceProvider());

    //bind image analysis
    ImageAnalysis imageAnalysis =
        new ImageAnalysis.Builder()
        .setTargetResolution(new Size(previewView.getWidth(), previewView.getHeight()))
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
        .build();

    imageAnalysis.setAnalyzer(executor, new ImageAnalysis.Analyzer() {
        @Override
        public void analyze(@NonNull ImageProxy imageProxy) {

            int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();

            Log.i(TAG, "here analyzer");
            @SuppressLint("UnsafeOptInUsageError") Image mediaImage = imageProxy.getImage();
            if (mediaImage != null) {
                InputImage image =
                    InputImage.fromMediaImage(mediaImage, rotationDegrees);

                initDrawingUtils(image);

                //START configuration of the facedetector
                FaceDetectorOptions realTimeOpts =
                    new FaceDetectorOptions.Builder()
                    .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL)
                    .build();

                FaceDetector detector = FaceDetection.getClient(realTimeOpts);
                //END of configuration of face detector

                // START detector
                detector.process(image)
                    .addOnSuccessListener(
                        new OnSuccessListener < List < Face >> () {
                            @Override
                            public void onSuccess(List < Face > faces) {
                                if (!faces.isEmpty()) {
                                    //Get information about detected faces
                                    processFaces(faces);
                                } else {
                                    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.MULTIPLY);
                                    Log.i(TAG, "vuoto");
                                }
                            }
                        })
                    .addOnFailureListener(
                        e - > {
                            e.printStackTrace();
                        });
            }
        }
    });

    Log.i(TAG, "here4");

    //add preview and analysis to the lifecycle
    cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageAnalysis);

    Log.i(TAG, "here5");
}

代碼永遠不會到達分析器,因此不會處理實時幀。 有人可以幫我嗎?

不確定這是否是主要問題,但看起來您在處理圖像后忘記關閉 ImageProxy。 嘗試將此偵聽器添加到從process方法返回的值中:

.addOnCompleteListener(task -> imageProxy.close());

從文檔中:

如果您使用的是CameraX API,請確保在使用完畢后關閉ImageProxy,例如,通過將OnCompleteListener 添加到從process 方法返回的Task 中。 有關示例,請參閱快速入門示例應用程序中的 VisionProcessorBase class。

遲到的答案,無論如何我今天遇到並解決了同樣的問題。

您應該在analyze方法結束時調用ImageProxy.close() ,這將允許傳送下一張圖像進行分析:

imageAnalysis.setAnalyzer(executor, new ImageAnalysis.Analyzer() {
    @Override
    public void analyze(@NonNull ImageProxy imageProxy) {
        int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();

        @SuppressLint("UnsafeOptInUsageError") 
        Image mediaImage = imageProxy.getImage();
        if (mediaImage != null) {
            ...
        }

        imageProxy.close() // <-- Add this line
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM