How can I use two Ml Kit detectors at the same time

312 Views Asked by At

I want to use image tagging and text recognition at the same time in my application. I have the following:

fun initAnalyzer(cameraExecutor: Executor) {
        imageAnalyzer = ImageAnalysis.Builder().build().also { imageAnalysis ->
            imageAnalysis.setAnalyzer(
                    cameraExecutor,
                    ImageMLAnalyzer { image, imageProxy, machineLearningUtils ->
                        textClassifier?.processImageWithText(image,
                                onSuccess = { result ->
                                    val resultString = processLineText(result)
                                    if (TextRecognitionClassifier.NORESULT !=
                                            resultString &&
                                            !isProcessingImage) {
                                        processAnalyzedResult(resultString)

                                    } else {
                                        imageClassifier?.processImage(image,
                                                onSuccess = { labelProbList ->
                                                    val labelResult = processResult(labelProbList)
                                                    if (ImageClassifier.NORESULT != labelResult &&
                                                            !isProcessingImage) {
                                                        openWebView(labelResult)
                                                    }
                                                    machineLearningUtils.analyzing(false)
                                                    imageProxy.close()

                                                }, onFailure = {
                                            machineLearningUtils.analyzing(false)
                                            imageProxy.close()
                                        })
                                    }
                                    machineLearningUtils.analyzing(false)
                                    imageProxy.close()

                                }, onFailure = {
                            machineLearningUtils.analyzing(false)
                            imageProxy.close()
                        })
                    })
        }
    }

The problem I have is that, firstly, this way I am not really using them at the same time, and secondly that I get an error when I enter the image detector that says:

com.google.firebase.ml.common.FirebaseMLException: No image data found.
1

There are 1 best solutions below

0
On

I answer myself, I have solved it with a Boolean:

fun initAnalyzer(cameraExecutor: Executor) {
        imageAnalyzer = ImageAnalysis.Builder().build().also { imageAnalysis ->
            imageAnalysis.setAnalyzer(
                    cameraExecutor,
                    ImageMLAnalyzer { image, imageProxy, machineLearningUtils ->
                        if (!useNextDetector) {
                            textClassifier?.processImageWithText(image,
                                    onSuccess = { result ->
                                        val resultString = processLineText(result)
                                        if (TextRecognitionClassifier.NORESULT !=
                                                resultString &&
                                                !isProcessingImage) {
                                            processAnalyzedResult(resultString)
                                        }
                                        useNextDetector = true
                                        machineLearningUtils.analyzing(false)
                                        imageProxy.close()

                                    }, onFailure = {
                                machineLearningUtils.analyzing(false)
                                imageProxy.close()
                            })

                        } else {
                            imageClassifier?.processImage(image,
                                    onSuccess = { labelProbList ->
                                        val result = processResult(labelProbList)
                                        if (ImageClassifier.NORESULT != result &&
                                                !isProcessingImage) {
                                            openWebView(result)
                                        }
                                        useNextDetector = false
                                        machineLearningUtils.analyzing(false)
                                        imageProxy.close()

                                    }, onFailure = {
                                machineLearningUtils.analyzing(false)
                                imageProxy.close()
                            })
                        }
                    })
        }
    }

It has worked for me, as it analyses until it finds something that "works for it" and uses it, if you can think of a better way, I'm listening.