Add Initialize function to PP-OCR (#326)

* Imporve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Improve OCR Readme

* Add Initialize function to PP-OCR

* Add Initialize function to PP-OCR

* Add Initialize function to PP-OCR

Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
yunyaoXYY
2022-10-09 17:31:37 +08:00
committed by GitHub
parent 56730a6517
commit ff5e798b7f
5 changed files with 65 additions and 0 deletions

View File

@@ -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();

View File

@@ -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();

View File

@@ -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]|

View File

@@ -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))) {

View File

@@ -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;