Long initialization of USB camera when using Camera X

54 Views Asked by At

I'm making an application for processing frames on orange pi5 from a USB camera using camera and encountered a problem with long initialization during cold start.

Here is my code for Camera init:

 private fun startCameraPreview() {

        cameraProviderFuture = ProcessCameraProvider.getInstance( requireContext() )

        cameraProviderFuture.addListener({
            val cameraProvider = cameraProviderFuture.get()
            bindPreview(cameraProvider) },
            ContextCompat.getMainExecutor(requireContext()) )
    }

     @SuppressLint("RestrictedApi")
     private fun bindPreview(cameraProvider : ProcessCameraProvider) {

        previewView = viewBinding.previewView
        previewView.implementationMode = PreviewView.ImplementationMode.COMPATIBLE
        previewView.scaleX = -1f

        val preview : Preview = Preview.Builder()
            .setResolutionSelector(
                ResolutionSelector.Builder().setResolutionStrategy(
                    ResolutionStrategy(Size(1080, 768),
                    ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER)
                ).build())
            .build()

        val selector = selectExternalOrBestCamera(cameraProvider) //For Usb camera Access
        preview.setSurfaceProvider( previewView.surfaceProvider )

        val strategy = ResolutionStrategy(Size(640, 480),
            ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER)

        val imageFrameAnalysis = ImageAnalysis.Builder()
            .setResolutionSelector(ResolutionSelector.Builder().setResolutionStrategy(strategy).build())
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
            .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
            .build()

        imageFrameAnalysis.setAnalyzer(Executors.newSingleThreadExecutor(), frameAnalyser )
        cameraProvider.bindToLifecycle(this as LifecycleOwner, selector!!, preview , imageFrameAnalysis  )
    }

    

Also i use code from official doc for accessing usb camera selector

fun selectExternalOrBestCamera(provider: ProcessCameraProvider):CameraSelector? {

        val cam2Infos = provider.availableCameraInfos.map {
            Camera2CameraInfo.from(it)
        }.sortedByDescending {
            // HARDWARE_LEVEL is Int type, with the order of:
            // LEGACY < LIMITED < FULL < LEVEL_3 < EXTERNAL
            it.getCameraCharacteristic(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
        }

        return when {
            cam2Infos.isNotEmpty() -> {
                CameraSelector.Builder()
                    .addCameraFilter {
                        it.filter { camInfo ->
                            // cam2Infos[0] is either EXTERNAL or best built-in camera
                            val thisCamId = Camera2CameraInfo.from(camInfo).cameraId
                            thisCamId == cam2Infos[0].cameraId
                        }
                    }.build()
            }
            else -> null
        }
    }

When I launch the application for the first time I get an Error 6 times.

2024-03-05 15:58:14.010 14845-14906 CameraRepository                              D  Added camera: 0
2024-03-05 15:58:14.012 14845-14906 Camera2CameraInfo                             I  Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
2024-03-05 15:58:14.012 14845-14906 CameraValidator                               D  Verifying camera lens facing on rk3588s_s, lensFacingInteger: null
2024-03-05 15:58:14.013 14845-14906 CameraValidator                              E  Camera LensFacing verification failed, existing cameras: [Camera@e0778e4[id=0]]
2024-03-05 15:58:14.014 14845-14906 CameraX                                       W  Retry init. Start time 9251708 current time 9253283
                                                                                                    androidx.camera.core.impl.CameraValidator$CameraIdListIncorrectException: Expected camera missing from device.
                                                                                                        at androidx.camera.core.impl.CameraValidator.validateCameras(CameraValidator.java:97)
                                                                                                        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:334)
                                                                                                        at androidx.camera.core.CameraX$$ExternalSyntheticLambda3.run(Unknown Source:10)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                                                                        at java.lang.Thread.run(Thread.java:920)
                                                                                                    Caused by: java.lang.IllegalArgumentException: No available camera can be found
                                                                                                        at androidx.camera.core.CameraSelector.select(CameraSelector.java:94)
                                                                                                        at androidx.camera.core.impl.CameraValidator.validateCameras(CameraValidator.java:83)
                                                                                                        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:334) 
                                                                                                        at androidx.camera.core.CameraX$$ExternalSyntheticLambda3.run(Unknown Source:10) 
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
                                                                                                        at java.lang.Thread.run(Thread.java:920) 

After this camera is initialized with this message:

The device might underreport the amount of the cameras. Finish the initialize task since we are already reaching the maximum number of retries.

Any help for this?

Added camera: 0
2024-03-05 15:58:15.030 14845-14906 Camera2CameraInfo                             I  Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
2024-03-05 15:58:15.031 14845-14906 CameraValidator                               D  Verifying camera lens facing on rk3588s_s, lensFacingInteger: null
2024-03-05 15:58:15.032 14845-14906 CameraValidator                              E  Camera LensFacing verification failed, existing cameras: [Camera@10741b2[id=0]]
2024-03-05 15:58:15.032 14845-14906 CameraX                                      E  The device might underreport the amount of the cameras. Finish the initialize task since we are already reaching the maximum number of retries.
2024-03-05 15:58:15.083 14845-14845 CameraOrientationUtil                        D  getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90
2024-03-05 15:58:15.085 14845-14845 CameraOrientationUtil                         D  getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90
2024-03-05 15:58:15.094 14845-14845 DynamicRangeResolver                          D  Resolved dynamic range for use case androidx.camera.core.Preview-1b649f9e-f19f-43f5-8882-7ace2058df35 to no compatible HDR dynamic ranges.
                                                                                                    DynamicRange@d8ad747{encoding=UNSPECIFIED, bitDepth=0}
                                                                                                    ->
                                                                                                    DynamicRange@c2bc286{encoding=SDR, bitDepth=8}
2024-03-05 15:58:15.099 14845-14845 CameraOrientationUtil                         D  getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90
2024-03-05 15:58:15.101 14845-14845 DeferrableSurface                             D  Surface created[total_surfaces=1, used_surfaces=0](androidx.camera.core.processing.SurfaceEdge$SettableSurface@df18112}
2024-03-05 15:58:15.103 14845-14845 DeferrableSurface                             D  Surface created[total_surfaces=2, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@4e29c5e}
2024-03-05 15:58:15.104 14845-14845 DeferrableSurface                             D  New surface in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@4e29c5e}
2024-03-05 15:58:15.104 14845-14845 DeferrableSurface                            D  use count+1, useCount=1 androidx.camera.core.SurfaceRequest$2@4e29c5e

My gradle dependencies for CameraX:

// CameraX
    def camerax_version = "1.3.1"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-video:${camerax_version}"

    implementation "androidx.camera:camera-view:${camerax_version}"
    implementation "androidx.camera:camera-extensions:${camerax_version}"

I tried adding @OptIn(ExperimentalCamera2Interop::class) annotations to the experimental method, I tried explicitly specifying lens type for CameraSelector .requireLensFacing(LENS_FACING_FRONT/BACK/EXTERNAL) - no result, only crashes. My thoughts on this case: CameraValidator cannot accept the lens value from the USB camera. Any help for this?

0

There are 0 best solutions below