0

我想在我的应用程序中同时使用图像标记和文本识别。我有以下内容:

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()
                        })
                    })
        }
    }

我遇到的问题是,首先,这样我并没有真正同时使用它们,其次,当我输入图像检测器时出现错误:

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

1 回答 1

0

我回答自己,我已经用布尔值解决了:

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()
                            })
                        }
                    })
        }
    }

它对我有用,因为它会分析直到找到“适合它”的东西并使用它,如果你能想到更好的方法,我在听。

于 2021-02-10T12:37:06.490 回答