I’ve integrated CameraX ImageAnalysis in my application along with the TensorFlow Lite library. The detection works properly, and I’m receiving images from the analyzer as expected. According to the application flow, after detecting an object, the app should navigate to another screen to display it and then return to the main detection screen. However, after returning, the camera sometimes stops detecting, and the image analyzer stops providing images. Additionally, my detection process is running too slowly
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Set up the preview use case
Preview preview = new Preview.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.build();
ImageAnalysis.Builder builder1 = new ImageAnalysis.Builder();
//Images are processed by passing an executor in which the image analysis is run
//
Camera2Interop.Extender ext = new Camera2Interop.Extender<>(builder1);
ext.setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, TotalCaptureResult.CONTROL_AF_MODE_AUTO)
.setCaptureRequestOption(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO)
.setCaptureRequestOption(CaptureRequest.SENSOR_SENSITIVITY, 100)
.setSessionCaptureCallback(sessionCaptureCallback);
// Set up the image analysis use case
imageAnalysis = builder1
.setResolutionSelector(new ResolutionSelector.Builder()
.setAspectRatioStrategy(AspectRatioStrategy.RATIO_4_3_FALLBACK_AUTO_STRATEGY)
.setResolutionStrategy(new ResolutionStrategy(new Size(1920, 1080),
ResolutionStrategy.FALLBACK_RULE_CLOSEST_LOWER)).build())
.setTargetName("ImageAnalysis")
.setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation())
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
initListener();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalyzer(this ,bitmapAnalyzedListener, previewView, borderCamera,afStateMap,belowPreviewTextView,overlayView));
// Select the camera and bind to lifecycle
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder().addUseCase(preview).addUseCase(imageAnalysis).setViewPort(previewView.getViewPort()).build();
camera = cameraProvider.bindToLifecycle(this, cameraSelector, useCaseGroup);
setUpTapToFocus();
} catch (InterruptedException | ExecutionException e) {
Log.e("CameraXApp", "Error binding camera use cases", e);
}
}, ContextCompat.getMainExecutor(this));