diff --git a/examples/vision/ocr/PP-OCRv2/cpp/infer.cc b/examples/vision/ocr/PP-OCRv2/cpp/infer.cc index 64cacd24c..bf0ff5f27 100644 --- a/examples/vision/ocr/PP-OCRv2/cpp/infer.cc +++ b/examples/vision/ocr/PP-OCRv2/cpp/infer.cc @@ -41,6 +41,11 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model // auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &rec_model); auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &cls_model, &rec_model); + if(!ocr_system_v2.Initialized()){ + std::cerr << "Failed to initialize OCR system." << std::endl; + return; + } + auto im = cv::imread(image_file); auto im_bak = im.clone(); diff --git a/examples/vision/ocr/PP-OCRv3/cpp/infer.cc b/examples/vision/ocr/PP-OCRv3/cpp/infer.cc index 4cdd8bdec..a48fb6bc0 100644 --- a/examples/vision/ocr/PP-OCRv3/cpp/infer.cc +++ b/examples/vision/ocr/PP-OCRv3/cpp/infer.cc @@ -41,6 +41,11 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model // auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &rec_model); auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &cls_model, &rec_model); + if(!ocr_system_v3.Initialized()){ + std::cerr << "Failed to initialize OCR system." << std::endl; + return; + } + auto im = cv::imread(image_file); auto im_bak = im.clone(); diff --git a/examples/vision/ocr/README.md b/examples/vision/ocr/README.md index a8f55d9e9..18d4458d6 100644 --- a/examples/vision/ocr/README.md +++ b/examples/vision/ocr/README.md @@ -17,3 +17,41 @@ | ch_PP-OCRv2_mobile |[ch_ppocr_mobile_v2.0_det](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_det_infer.tar.gz) | [ch_ppocr_mobile_v2.0_cls](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz) | [ch_ppocr_mobile_v2.0_rec](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_rec_infer.tar.gz) | [ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2系列原始超轻量模型,支持中英文、多语种文本检测,比PPOCRv2更加轻量 | | ch_PP-OCRv2_server |[ch_ppocr_server_v2.0_det](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_server_v2.0_det_infer.tar.gz) | [ch_ppocr_mobile_v2.0_cls](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz) | [ch_ppocr_server_v2.0_rec](https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_server_v2.0_rec_infer.tar.gz) |[ppocr_keys_v1.txt](https://bj.bcebos.com/paddlehub/fastdeploy/ppocr_keys_v1.txt) | OCRv2服务器系列模型, 支持中英文、多语种文本检测,比超轻量模型更大,但效果更好| +### OCR 模型的处理说明 + +为了让OCR系列模型在FastDeploy多个推理后端上正确推理,以上表格中的部分模型的输入shape,和PaddleOCR套件提供的模型有差异. +例如,由PaddleOCR套件库提供的英文版PP-OCRv3_det模型,输入的shape是`[-1,3,960,960]`, 而FastDeploy提供的此模型输入shape为`[-1,3,-1,-1]`. + +**差异存在的原因**: 当我们在ORT和OpenVINO上部署输入shape固定的模型时(指定了高和宽),由于OCR的输入图片尺寸是变化的,会报例如下面所示的错误,导致无法推理: +``` +Failed to Infer: Got invalid dimensions for input: x for the following indices +index: 3 Got: 608 Expected: 960 +``` +**解决办法**:除了直接下载FastDeploy提供的模型外,用户还可以使用如下工具仓库, 修改模型的输入shape. + +**仓库链接**: https://github.com/jiangjiajun/PaddleUtils + +使用示例如下: +``` +#该用例将en_PP-OCRv3_det_infer模型的输入shape, 改为[-1,3,-1,-1], 并将新模型存放至output文件夹下 +git clone git@github.com:jiangjiajun/PaddleUtils.git +cd paddle +python paddle_infer_shape.py --model_dir en_PP-OCRv3_det_infer/ \ + --model_filename inference.pdmodel \ + --params_filename inference.pdiparams \ + --save_dir output \ + --input_shape_dict="{'x':[-1,3,-1,-1]}" +``` + +#### OCR模型输入shape更改记录 +以下表格记录了FastDeploy修改过的OCR模型的输入`('输入名':[shape])`, 供用户参考. + +| OCR版本 | 模型 | 修改前 | 修改后 | +|:----|:----|:----|:----| +|PPOCRv3 |en_PP-OCRv3_det|'x':[-1,3,960,960]|'x':[-1,3,-1,-1]| +|PPOCRv2 |ch_PP-OCRv2_det|'x':[-1,3,960,960]|'x':[-1,3,-1,-1]| +|PPOCRv2 |ch_PP-OCRv2_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]| +|PPOCRv2_mobile |ch_ppocr_mobile_v2.0_det|'x':[-1,3,640,640]|'x':[-1,3,-1,-1]| +|PPOCRv2_mobile|ch_ppocr_mobile_v2.0_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]| +|PPOCRv2_server|ch_ppocr_server_v2.0_det|'x':[-1,3,640,640]|'x':[-1,3,-1,-1]| +|PPOCRv2_server |ch_ppocr_server_v2.0_rec|'x':[-1,3,32,100]|'x':[-1,3,-1,-1]| diff --git a/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.cc b/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.cc index bb02ad105..728b9f883 100644 --- a/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.cc +++ b/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.cc @@ -32,6 +32,22 @@ PPOCRSystemv2::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, recognizer_->rec_image_shape[1] = 32; } +bool PPOCRSystemv2::Initialized() const { + + if (detector_ != nullptr && !detector_->Initialized()){ + return false; + } + + if (classifier_ != nullptr && !classifier_->Initialized()){ + return false; + } + + if (recognizer_ != nullptr && !recognizer_->Initialized()){ + return false; + } + return true; +} + bool PPOCRSystemv2::Detect(cv::Mat* img, fastdeploy::vision::OCRResult* result) { if (!detector_->Predict(img, &(result->boxes))) { diff --git a/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h b/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h index 34e416946..f2a8ccbed 100644 --- a/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h +++ b/fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h @@ -39,6 +39,7 @@ class FASTDEPLOY_DECL PPOCRSystemv2 : public FastDeployModel { fastdeploy::vision::ocr::Recognizer* rec_model); virtual bool Predict(cv::Mat* img, fastdeploy::vision::OCRResult* result); + bool Initialized() const override; protected: fastdeploy::vision::ocr::DBDetector* detector_ = nullptr;