7

How to decrease the frame rate to 1 fps in image analysis so that I don't detect barcode multiple times. In my use-case, scanning the same barcode multiple times with a 1-second interval should increment a counter. So I want it to work correctly. (Similar to product scanner at shop tills)

cameraX version : 1.0.0-beta02

Similar questions :

Current implementation :

https://proandroiddev.com/update-android-camerax-4a44c3e4cdcc
Following this doc, to throttle image analysis.

override fun analyze(image: ImageProxy) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.SECONDS.toMillis(1)) {
        // Image analysis code
    }
    image.close()
}

A better solution would be helpful.

2
  • Maybe it makes more sense to use an Image Capture use case in which you are in control of when the image is taken? I'm wondering the same for my project under a similar need atm.
    – topher217
    Apr 2, 2021 at 6:01
  • Just wanted to update and say that I tried it, and found that the imageProxy returned by the Image Capture use case is JPEG rather than YUV_420_888 that you get from Image Analysis. Maybe this is a good thing for some, but I didn't find any documentation as to the structure of the planes or buffer in this Android JPEG format, so I didn't know how to handle things from there so switching back to Image Analysis and just ignoring new images.
    – topher217
    Apr 2, 2021 at 7:22

4 Answers 4

5

Tried bmdelacruz's solution. Had issues with closing the image.
Was getting an error similar to this.
Couldn't get it working.

Using delay worked well for me.

Code

CoroutineScope(Dispatchers.IO).launch {
    delay(1000 - (System.currentTimeMillis() - currentTimestamp))
    imageProxy.close()
}

Complete BarcodeAnalyser code

class BarcodeAnalyser(
    private val onBarcodesDetected: (barcodes: List<Barcode>) -> Unit,
) : ImageAnalysis.Analyzer {
    private val barcodeScannerOptions = BarcodeScannerOptions.Builder()
        .setBarcodeFormats(Barcode.FORMAT_ALL_FORMATS)
        .build()
    private val barcodeScanner = BarcodeScanning.getClient(barcodeScannerOptions)
    var currentTimestamp: Long = 0

    override fun analyze(
        imageProxy: ImageProxy,
    ) {
        currentTimestamp = System.currentTimeMillis()
        imageProxy.image?.let { imageToAnalyze ->
            val imageToProcess =
                InputImage.fromMediaImage(imageToAnalyze, imageProxy.imageInfo.rotationDegrees)
            barcodeScanner.process(imageToProcess)
                .addOnSuccessListener { barcodes ->
                    // Success handling
                }
                .addOnFailureListener { exception ->
                    // Failure handling
                }
                .addOnCompleteListener {
                    CoroutineScope(Dispatchers.IO).launch {
                        delay(1000 - (System.currentTimeMillis() - currentTimestamp))
                        imageProxy.close()
                    }
                }
        }
    }
}
2

You can take advantage of the fact that the next analysis will not begin until you close the provided ImageProxy.

In my case, I just put the thread to sleep since my analyzer's executor is a single thread executor.

class MyAnalyzer : ImageAnalysis.Analyzer {
    override fun analyze(image: ImageProxy) {
        val elapsedAnalysisTime = measureTimeMillis {
            // do your stuff here
        }
        image.use {
            if (elapsedAnalysisTime < 1000) {
                Thread.sleep(1000 - elapsedAnalysisTime)
            }
        }
    }
}
3
  • Great answer that highlights the strengths of the Kotlin language.
    – Greelings
    Sep 28, 2021 at 10:13
  • This is still lag in Emulator
    – Aadam
    May 22, 2023 at 22:42
  • Try to use STRATEGY_KEEP_ONLY_LATEST as the backpressure strategy for the image analyzer.
    – bmdelacruz
    May 23, 2023 at 5:17
0

Not certain if anyone's still on the hunt for a solution to the aforementioned issue. I encountered a similar problem, and my workaround for it goes something like this:

After invoking the method .addOnCompleteListener in my analyzer class, I introduced a pause. Leveraging the fact that the subsequent image analysis won't kick off until you've close the imageProxy

   .addOnCompleteListener{
                        CoroutineScope(Dispatchers.IO).launch {
                            delay(2000)
                            imageProxy.close()
                        }
                    }
-2

private var firstDetected = true

for (barcode in barcodes) {
            if (barcodes.size > 0 && firstDetected) {
                LoggingUtility.writeLog("Analyzer",
                        "MLKitBarcode Result",
                        "Barcode is ${barcode.rawValue!!}")
                firstDetected = false
       }
}

This might help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.