I am implementing the new MlKitAnalalyzer based on the description and links of CameraX 1.2 found here.
Following the example I made an analyzer for Barcode scanning, and this works very well. But if I try to implement a text-scanner with MlKitAnalyzer the processing gets very slow. It takes over 4 seconds for each frame to be processed. Same code but with a manual implementation of ImageAnalysis.Analyzer processes a frame in about 600ms. Any help as to what is making MlKitAnalyzer so slow with text-recognition would be nice!
My code for creating the analyzer. Time for each frame: >4000ms:
private fun clearAndSetAnalyzer() {
cameraController?.clearImageAnalysisAnalyzer()
val startMs = SystemClock.elapsedRealtime()
val scanner = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
val analyzer = MlKitAnalyzer(
listOf(scanner),
ImageAnalysis.COORDINATE_SYSTEM_ORIGINAL,
mainThreadExecutor()
) { result ->
result.getValue(scanner)?.let { resultText ->
Log.i(TAG, "Scan complete net. Time: ${SystemClock.elapsedRealtime() - startMs}ms.")
}
}
cameraController?.setImageAnalysisAnalyzer(mainThreadExecutor(), analyzer)
}
The old code for creating imageanalyzer myself is here. Initializing the scanner for every frame takes no time but is just here for illustrating what the code does. Time for each frame: 600ms:
class AnalyzerOld() : ImageAnalysis.Analyzer {
override fun analyze(imageProxy: ImageProxy) {
val mediaImage = imageProxy.image ?: return
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
val tekstScanner = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
tekstScanner.process(image)
.addOnSuccessListener { resultText ->
//Handling result
}
.addOnFailureListener { LogUtils.e(TAG, "Fail", it) }
.addOnCompleteListener {
tekstScanner.close()
imageProxy.close()
}
}
}