equationl / paddleocr4android

A Paddle OCR sample for Android by Paddle-Lite from https://github.com/PaddlePaddle/PaddleOCR 对 PaddleOCR 的简单封装,使其能够快速上手使用而不需繁琐的配置。
120 stars 22 forks source link

模型初始化时出现问题,OcrInitCallback()没有执行 #20

Closed liudapang000 closed 1 year ago

liudapang000 commented 1 year ago

上一次模型文件复制的问题,我通过手动复制到手机中解决,但是在模型初始化时又出现了新的问题,模型开始识别时,app闪退,报错提示

FATAL EXCEPTION: DefaultDispatcher-worker-3
Process: com.equipmentnameplateocr, PID: 31025
kotlin.UninitializedPropertyAccessException: lateinit property predictor has not been initialized
        at com.equationl.fastdeployocr.OCR.runSync-IoAF18A(OCR.kt:124)
        at com.equationl.fastdeployocr.OCR$run$1.invokeSuspend(OCR.kt:141)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@dd9605a, Dispatchers.IO]

代码如下:

       public void ppocr(String imagePath){
        //String path = mContext.getCacheDir().getPath();
        String path = mContext.getExternalCacheDir() + "/OCRModels/";
        Log.d("debug", "mypath"+path);
        // 手动复制模型文件
        copyAssetFileToDirectory("models/rec.pdiparams",path);
        copyAssetFileToDirectory("models/rec.pdmodel",path);
        copyAssetFileToDirectory("models/ppocr_keys_v1.txt",path);
        ocr = new OCR(mContext);
        OcrConfig config = new OcrConfig();

        //config.setModelPath("../../../assets/models");
        config.setModelPath("models");
        config.setClsModelFileName("cls");
        config.setRecModelFileName("rec");
        config.setDetModelFileName("det");
        // 运行全部模型
        config.setRunType(RunType.All);
        // 使用所有核心运行
        config.setCpuPowerMode(LitePowerMode.LITE_POWER_FULL);
        // 绘制文本位置
        config.setDrwwTextPositionBox(true);
        // 如果是原始模型,则使用 FP16 精度
        config.setRecRunPrecision(RunPrecision.LiteFp16);
        config.setDetRunPrecision(RunPrecision.LiteFp16);
        config.setClsRunPrecision(RunPrecision.LiteFp16);
        Log.d("debug", "开始初始化v3");
        //Log.d("debug", "model path"+imagePath);
        ocr.initModel(config, new OcrInitCallback() {
            @Override
            public void onSuccess() {
                Log.d("debug", "onSuccess: 初始化成功");
            }

            @Override
            public void onFail(Throwable e) {
                Log.d("debug", "onFail: 初始化失败", e);
            }
        });

        try {
            Log.d("debug", "v3 imagePath: "+imagePath);
            File file = new File(imagePath);
            if (file.exists()) {
                Bitmap bitmap3 = BitmapFactory.decodeFile(imagePath);
                Log.d("debug", "v3开始识别");
                ocr.run(bitmap3, new OcrRunCallback() {
                    @Override
                    public void onSuccess(OcrResult result) {
                        String simpleText = result.getSimpleText();
                        Bitmap imgWithBox = result.getImgWithBox();
                        long inferenceTime = result.getInferenceTime();
                        ArrayList<OcrResultModel> outputRawResult = result.getOutputRawResult();

                        String text = "识别文字=\n" + simpleText + "\n识别时间=" + inferenceTime + " ms\n更多信息=\n";
                        Log.d("debug", "v3结果:"+text);
                    }
                    @Override
                    public void onFail(Throwable e) {
                        Log.d("debug", "onFail: 识别失败!", e);
                    }
                });
            } else {
                //promise.reject("debug", "Image file not found.");
                Log.d("debug", "Image file not found.");
            }
        } catch (Exception e) {
            //promise.reject("debug", e);
            Log.d("debug", "v3失败");
        }

        ocr.releaseModel();
        Log.d("debug", "onSuccess: 模型释放成功v3");

    }

怀疑是模型初始化出现问题,因为模型初始化时,OcrInitCallback()似乎并没有执行,没有提示成功或失败的信息

equationl commented 1 year ago

你的怀疑是对,因为初始化压根没有完成你就调用 ocr.run() 了。

ocr.initModel() 是异步初始化,你要等初始化完成才能调用 ocr.run() ;或者你也可以自己处理线程问题,调用 ocr.initModelSync() 同步初始化。