AutoJs6 icon indicating copy to clipboard operation
AutoJs6 copied to clipboard

【Bug反馈】OCR功能中 ocr.mlkit.detect 与 ocr.paddle.detect 返回完全相同的结果

Open zhu855 opened this issue 1 month ago • 2 comments

  1. 环境信息
  • 设备型号: (红米k80pro)
  • Android 版本: (例如: os版本:3.0.3.0)
  • Auto.js 6 版本: 6.6.4
  1. 问题描述 根据官方文档,ocr.mlkit.detect() 和 ocr.paddle.detect() 应该分别调用 MLKit 和 PaddleOCR 两个不同的识别引擎,并产生不同的识别结果。

然而,在我的环境中,无论调用 ocr.mlkit.detect(img) 还是 ocr.paddle.detect(img),两者返回的识别结果(包括所有文本块的 label, confidence 和 bounds)都完全一致,精确到每一个细节。这表明底层实际上可能只调用了同一个 OCR 引擎,或者其中一个引擎静默失败并回退到了另一个。

  1. 预期行为 调用 ocr.mlkit.detect(img) 和 ocr.paddle.detect(img) 时,应该分别激活两个独立的 OCR 引擎,并针对同一张图片返回两组具有明显差异(尤其是在置信度 confidence 上)的识别结果。

  2. 实际行为 两个函数返回了完全相同的 OcrResult[] 数组,使我无法对两个引擎进行独立的测试和使用。

zhu855 avatar Nov 19 '25 13:11 zhu855

感谢反馈, 这是一个相对难以发现的 bug. 问题在于 ocr.xxx.detect 方法对 ocr.mode 的判断出现了条件遗漏, 而 ocr.xxx.recognizeText 是正常的.

源码体现如下:

val results = when (scriptRuntime.ocr.mode) {
    OcrMode.MLKIT -> OcrMLKit.detectInternal(scriptRuntime, image, options)
    OcrMode.PADDLE -> OcrPaddle.detectInternal(scriptRuntime, image, options)
    OcrMode.RAPID -> OcrRapid.detectInternal(scriptRuntime, image, options)
    else -> throw WrappedIllegalArgumentException("Cannot call ocr.detect with an unknown mode")
}
val results = when (opt.prop("mode").takeIf { it is OcrMode } ?: scriptRuntime.ocr.mode) {
    OcrMode.MLKIT -> OcrMLKit.recognizeTextInternal(scriptRuntime, image, options)
    OcrMode.PADDLE -> OcrPaddle.recognizeTextInternal(scriptRuntime, image, options)
    OcrMode.RAPID -> OcrRapid.recognizeTextInternal(scriptRuntime, image, options)
    else -> throw WrappedIllegalArgumentException("Cannot call ocr.recognizeText with an unknown mode")
}

可以看到 recognizeText 的判断条件是全面的, 而 detect 是存在遗漏的.

目前在脚本代码层面可以替代的方案, 是使用 ocr.mode 的 setter 形式变更 OCR 模式, 再使用通用的 ocr.detect 进行文本识别 (暂时避免使用 ocr.xxx.detect 这种特定方法).

一个简单的示例:

ocr.mode = 'paddle';
console.log(ocr.detect( /* ... */ ));
ocr.mode = 'rapid';
console.log(ocr.detect( /* ... */ ));
ocr.mode = 'mlkit';
console.log(ocr.detect( /* ... */ ));

SuperMonster003 avatar Nov 19 '25 16:11 SuperMonster003

谢谢,按照建议修改后已经正常工作了

zhu855 avatar Nov 20 '25 14:11 zhu855