[Doc] Rename PPOCRSystem to PPOCR and update comments. (#395)

* 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

* Make all the model links come from PaddleOCR

* Improve OCR readme

* Improve OCR readme

* Improve OCR readme

* Improve OCR readme

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add Readme for vision results

* Add check for label file in postprocess of Rec model

* Add check for label file in postprocess of Rec model

* Add check for label file in postprocess of Rec model

* Add check for label file in postprocess of Rec model

* Add check for label file in postprocess of Rec model

* Add check for label file in postprocess of Rec model

* Add comments to create API docs

* Improve OCR comments

* Rename OCR and add comments

* Make sure previous python example works

* Make sure previous python example works

Co-authored-by: Jason <jiangjiajun@baidu.com>
This commit is contained in:
yunyaoXYY
2022-10-19 17:21:48 +08:00
committed by GitHub
parent 85e1c647f6
commit 24317e1a14
23 changed files with 380 additions and 186 deletions

View File

@@ -1,3 +1,41 @@
# OCR API # OCR API
comming soon... ## fastdeploy.vision.ocr.DBDetector
```{eval-rst}
.. autoclass:: fastdeploy.vision.ocr.DBDetector
:members:
:inherited-members:
```
## fastdeploy.vision.ocr.Classifier
```{eval-rst}
.. autoclass:: fastdeploy.vision.ocr.Classifier
:members:
:inherited-members:
```
## fastdeploy.vision.ocr.Recognizer
```{eval-rst}
.. autoclass:: fastdeploy.vision.ocr.Recognizer
:members:
:inherited-members:
```
## fastdeploy.vision.ocr.PPOCRv2
```{eval-rst}
.. autoclass:: fastdeploy.vision.ocr.PPOCRv2
:members:
:inherited-members:
```
## fastdeploy.vision.ocr.PPOCRv3
```{eval-rst}
.. autoclass:: fastdeploy.vision.ocr.PPOCRv3
:members:
:inherited-members:
```

View File

@@ -3,3 +3,4 @@ recommonmark
sphinx_markdown_tables sphinx_markdown_tables
sphinx_rtd_theme sphinx_rtd_theme
furo furo
myst_parser

View File

@@ -1,6 +1,6 @@
# PPOCRSystemv2 C++部署示例 # PPOCRv2 C++部署示例
本目录下提供`infer.cc`快速完成PPOCRSystemv2在CPU/GPU以及GPU上通过TensorRT加速部署的示例。 本目录下提供`infer.cc`快速完成PPOCRv2在CPU/GPU以及GPU上通过TensorRT加速部署的示例。
在部署前,需确认以下两个步骤 在部署前,需确认以下两个步骤
@@ -19,14 +19,14 @@ make -j
# 下载模型,图片和字典文件 # 下载模型,图片和字典文件
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv2_det_infer.tar.gz wget https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_infer.tar
tar -xvf ch_PP-OCRv2_det_infer.tar.gz tar -xvf ch_PP-OCRv2_det_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar.gz tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv2_rec_infer.tar.gz wgethttps://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_infer.tar
tar -xvf ch_PP-OCRv2_rec_infer.tar.gz tar -xvf ch_PP-OCRv2_rec_infer.tar
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
@@ -48,17 +48,17 @@ wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg"> <img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
## PPOCRSystemv2 C++接口 ## PPOCRv2 C++接口
### PPOCRSystemv2类 ### PPOCRv2类
``` ```
fastdeploy::application::ocrsystem::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, fastdeploy::pipeline::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model, fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
``` ```
PPOCRSystemv2 的初始化,由检测,分类和识别模型串联构成 PPOCRv2 的初始化,由检测,分类和识别模型串联构成
**参数** **参数**
@@ -67,10 +67,10 @@ PPOCRSystemv2 的初始化,由检测,分类和识别模型串联构成
> * **Recognizer**(model): OCR中的识别模型 > * **Recognizer**(model): OCR中的识别模型
``` ```
fastdeploy::application::ocrsystem::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, fastdeploy::pipeline::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
``` ```
PPOCRSystemv2 的初始化,由检测,识别模型串联构成(无分类器) PPOCRv2 的初始化,由检测,识别模型串联构成(无分类器)
**参数** **参数**

View File

@@ -37,12 +37,12 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
assert(cls_model.Initialized()); assert(cls_model.Initialized());
assert(rec_model.Initialized()); assert(rec_model.Initialized());
// The classification model is optional, so the OCR system can also be connected in series as follows // The classification model is optional, so the PP-OCR can also be connected in series as follows
// auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &rec_model); // auto ppocr_v2 = fastdeploy::pipeline::PPOCRv2(&det_model, &rec_model);
auto ocr_system_v2 = fastdeploy::application::ocrsystem::PPOCRSystemv2(&det_model, &cls_model, &rec_model); auto ppocr_v2 = fastdeploy::pipeline::PPOCRv2(&det_model, &cls_model, &rec_model);
if(!ocr_system_v2.Initialized()){ if(!ppocr_v2.Initialized()){
std::cerr << "Failed to initialize OCR system." << std::endl; std::cerr << "Failed to initialize PP-OCR." << std::endl;
return; return;
} }
@@ -50,14 +50,14 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
auto im_bak = im.clone(); auto im_bak = im.clone();
fastdeploy::vision::OCRResult result; fastdeploy::vision::OCRResult result;
if (!ocr_system_v2.Predict(&im, &result)) { if (!ppocr_v2.Predict(&im, &result)) {
std::cerr << "Failed to predict." << std::endl; std::cerr << "Failed to predict." << std::endl;
return; return;
} }
std::cout << result.Str() << std::endl; std::cout << result.Str() << std::endl;
auto vis_im = fastdeploy::vision::Visualize::VisOcr(im_bak, result); auto vis_im = fastdeploy::vision::VisOcr(im_bak, result);
cv::imwrite("vis_result.jpg", vis_im); cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
} }

View File

@@ -1,23 +1,23 @@
# PPOCRSystemv2 Python部署示例 # PPOCRv2 Python部署示例
在部署前,需确认以下两个步骤 在部署前,需确认以下两个步骤
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. FastDeploy Python whl包安装参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. FastDeploy Python whl包安装参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
本目录下提供`infer.py`快速完成PPOCRSystemv2在CPU/GPU以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 本目录下提供`infer.py`快速完成PPOCRv2在CPU/GPU以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成
``` ```
# 下载模型,图片和字典文件 # 下载模型,图片和字典文件
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv2_det_infer.tar.gz wget https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_infer.tar
tar -xvf ch_PP-OCRv2_det_infer.tar.gz tar -xvf ch_PP-OCRv2_det_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar.gz tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv2_rec_infer.tar.gz wgethttps://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_infer.tar
tar -xvf ch_PP-OCRv2_rec_infer.tar.gz tar -xvf ch_PP-OCRv2_rec_infer.tar
wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/doc/imgs/12.jpg
@@ -39,12 +39,12 @@ python infer.py --det_model ch_PP-OCRv2_det_infer --cls_model ch_ppocr_mobile_v2
运行完成可视化结果如下图所示 运行完成可视化结果如下图所示
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg"> <img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
## PPOCRSystemv2 Python接口 ## PPOCRv2 Python接口
``` ```
fd.vision.ocr.PPOCRSystemv2(det_model=det_model, cls_model=cls_model, rec_model=rec_model) fd.vision.ocr.PPOCRv2(det_model=det_model, cls_model=cls_model, rec_model=rec_model)
``` ```
PPOCRSystemv2的初始化,输入的参数是检测模型分类模型和识别模型其中cls_model可选如无需求可设置为None PPOCRv2的初始化,输入的参数是检测模型分类模型和识别模型其中cls_model可选如无需求可设置为None
**参数** **参数**
@@ -55,7 +55,7 @@ PPOCRSystemv2的初始化,输入的参数是检测模型,分类模型和识别
### predict函数 ### predict函数
> ``` > ```
> result = ocr_system.predict(im) > result = ppocr_v2.predict(im)
> ``` > ```
> >
> 模型预测接口,输入是一张图片 > 模型预测接口,输入是一张图片

View File

@@ -110,15 +110,15 @@ rec_model = fd.vision.ocr.Recognizer(
rec_label_file, rec_label_file,
runtime_option=runtime_option) runtime_option=runtime_option)
# 创建OCR系统串联3个模型其中cls_model可选如无需求可设置为None # 创建PP-OCR串联3个模型其中cls_model可选如无需求可设置为None
ocr_system = fd.vision.ocr.PPOCRSystemv2( ppocr_v2 = fd.vision.ocr.PPOCRv2(
det_model=det_model, cls_model=cls_model, rec_model=rec_model) det_model=det_model, cls_model=cls_model, rec_model=rec_model)
# 预测图片准备 # 预测图片准备
im = cv2.imread(args.image) im = cv2.imread(args.image)
#预测并打印结果 #预测并打印结果
result = ocr_system.predict(im) result = ppocr_v2.predict(im)
print(result) print(result)

View File

@@ -1,6 +1,6 @@
# PPOCRSystemv3 C++部署示例 # PPOCRv3 C++部署示例
本目录下提供`infer.cc`快速完成PPOCRSystemv3在CPU/GPU以及GPU上通过TensorRT加速部署的示例。 本目录下提供`infer.cc`快速完成PPOCRv3在CPU/GPU以及GPU上通过TensorRT加速部署的示例。
在部署前,需确认以下两个步骤 在部署前,需确认以下两个步骤
@@ -22,8 +22,8 @@ make -j
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
tar -xvf ch_PP-OCRv3_det_infer.tar tar -xvf ch_PP-OCRv3_det_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar.gz tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
tar -xvf ch_PP-OCRv3_rec_infer.tar tar -xvf ch_PP-OCRv3_rec_infer.tar
@@ -48,17 +48,17 @@ wget https://gitee.com/paddlepaddle/PaddleOCR/raw/release/2.6/ppocr/utils/ppocr_
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg"> <img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
## PPOCRSystemv3 C++接口 ## PPOCRv3 C++接口
### PPOCRSystemv3类 ### PPOCRv3类
``` ```
fastdeploy::application::ocrsystem::PPOCRSystemv3(fastdeploy::vision::ocr::DBDetector* det_model, fastdeploy::pipeline::PPOCRv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model, fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
``` ```
PPOCRSystemv3 的初始化,由检测,分类和识别模型串联构成 PPOCRv3 的初始化,由检测,分类和识别模型串联构成
**参数** **参数**
@@ -67,10 +67,10 @@ PPOCRSystemv3 的初始化,由检测,分类和识别模型串联构成
> * **Recognizer**(model): OCR中的识别模型 > * **Recognizer**(model): OCR中的识别模型
``` ```
fastdeploy::application::ocrsystem::PPOCRSystemv3(fastdeploy::vision::ocr::DBDetector* det_model, fastdeploy::pipeline::PPOCRv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
``` ```
PPOCRSystemv3 的初始化,由检测,识别模型串联构成(无分类器) PPOCRv3 的初始化,由检测,识别模型串联构成(无分类器)
**参数** **参数**

View File

@@ -37,12 +37,12 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
assert(cls_model.Initialized()); assert(cls_model.Initialized());
assert(rec_model.Initialized()); assert(rec_model.Initialized());
// The classification model is optional, so the OCR system can also be connected in series as follows // The classification model is optional, so the PP-OCR can also be connected in series as follows
// auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &rec_model); // auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &rec_model);
auto ocr_system_v3 = fastdeploy::application::ocrsystem::PPOCRSystemv3(&det_model, &cls_model, &rec_model); auto ppocr_v3 = fastdeploy::pipeline::PPOCRv3(&det_model, &cls_model, &rec_model);
if(!ocr_system_v3.Initialized()){ if(!ppocr_v3.Initialized()){
std::cerr << "Failed to initialize OCR system." << std::endl; std::cerr << "Failed to initialize PP-OCR." << std::endl;
return; return;
} }
@@ -50,14 +50,14 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
auto im_bak = im.clone(); auto im_bak = im.clone();
fastdeploy::vision::OCRResult result; fastdeploy::vision::OCRResult result;
if (!ocr_system_v3.Predict(&im, &result)) { if (!ppocr_v3.Predict(&im, &result)) {
std::cerr << "Failed to predict." << std::endl; std::cerr << "Failed to predict." << std::endl;
return; return;
} }
std::cout << result.Str() << std::endl; std::cout << result.Str() << std::endl;
auto vis_im = fastdeploy::vision::Visualize::VisOcr(im_bak, result); auto vis_im = fastdeploy::vision::VisOcr(im_bak, result);
cv::imwrite("vis_result.jpg", vis_im); cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl; std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
} }

View File

@@ -1,11 +1,11 @@
# PPOCRSystemv3 Python部署示例 # PPOCRv3 Python部署示例
在部署前,需确认以下两个步骤 在部署前,需确认以下两个步骤
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. FastDeploy Python whl包安装参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) - 2. FastDeploy Python whl包安装参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
本目录下提供`infer.py`快速完成PPOCRSystemv3在CPU/GPU以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成 本目录下提供`infer.py`快速完成PPOCRv3在CPU/GPU以及GPU上通过TensorRT加速部署的示例。执行如下脚本即可完成
``` ```
@@ -13,8 +13,8 @@
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
tar xvf ch_PP-OCRv3_det_infer.tar tar xvf ch_PP-OCRv3_det_infer.tar
wget https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tar.gz https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar.gz tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar wget https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
tar xvf ch_PP-OCRv3_rec_infer.tar tar xvf ch_PP-OCRv3_rec_infer.tar
@@ -38,12 +38,12 @@ python infer.py --det_model ch_PP-OCRv3_det_infer --cls_model ch_ppocr_mobile_v2
运行完成可视化结果如下图所示 运行完成可视化结果如下图所示
<img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg"> <img width="640" src="https://user-images.githubusercontent.com/109218879/185826024-f7593a0c-1bd2-4a60-b76c-15588484fa08.jpg">
## PPOCRSystemv3 Python接口 ## PPOCRv3 Python接口
``` ```
fd.vision.ocr.PPOCRSystemv3(det_model=det_model, cls_model=cls_model, rec_model=rec_model) fd.vision.ocr.PPOCRv3(det_model=det_model, cls_model=cls_model, rec_model=rec_model)
``` ```
PPOCRSystemv3的初始化,输入的参数是检测模型分类模型和识别模型其中cls_model可选如无需求可设置为None PPOCRv3的初始化,输入的参数是检测模型分类模型和识别模型其中cls_model可选如无需求可设置为None
**参数** **参数**
@@ -54,7 +54,7 @@ PPOCRSystemv3的初始化,输入的参数是检测模型,分类模型和识别
### predict函数 ### predict函数
> ``` > ```
> result = ocr_system.predict(im) > result = ppocr_v3.predict(im)
> ``` > ```
> >
> 模型预测接口,输入是一张图片 > 模型预测接口,输入是一张图片

View File

@@ -110,15 +110,15 @@ rec_model = fd.vision.ocr.Recognizer(
rec_label_file, rec_label_file,
runtime_option=runtime_option) runtime_option=runtime_option)
# 创建OCR系统串联3个模型其中cls_model可选如无需求可设置为None # 创建PP-OCR串联3个模型其中cls_model可选如无需求可设置为None
ocr_system = fd.vision.ocr.PPOCRSystemv3( ppocr_v3 = fd.vision.ocr.PPOCRv3(
det_model=det_model, cls_model=cls_model, rec_model=rec_model) det_model=det_model, cls_model=cls_model, rec_model=rec_model)
# 预测图片准备 # 预测图片准备
im = cv2.imread(args.image) im = cv2.imread(args.image)
#预测并打印结果 #预测并打印结果
result = ocr_system.predict(im) result = ppocr_v3.predict(im)
print(result) print(result)

View File

@@ -42,8 +42,8 @@
#include "fastdeploy/vision/matting/ppmatting/ppmatting.h" #include "fastdeploy/vision/matting/ppmatting/ppmatting.h"
#include "fastdeploy/vision/ocr/ppocr/classifier.h" #include "fastdeploy/vision/ocr/ppocr/classifier.h"
#include "fastdeploy/vision/ocr/ppocr/dbdetector.h" #include "fastdeploy/vision/ocr/ppocr/dbdetector.h"
#include "fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h" #include "fastdeploy/vision/ocr/ppocr/ppocr_v2.h"
#include "fastdeploy/vision/ocr/ppocr/ppocr_system_v3.h" #include "fastdeploy/vision/ocr/ppocr/ppocr_v3.h"
#include "fastdeploy/vision/ocr/ppocr/recognizer.h" #include "fastdeploy/vision/ocr/ppocr/recognizer.h"
#include "fastdeploy/vision/segmentation/ppseg/model.h" #include "fastdeploy/vision/segmentation/ppseg/model.h"
#endif #endif

View File

@@ -17,13 +17,13 @@
namespace fastdeploy { namespace fastdeploy {
void BindPPOCRModel(pybind11::module& m); void BindPPOCRModel(pybind11::module& m);
void BindPPOCRSystemv3(pybind11::module& m); void BindPPOCRv3(pybind11::module& m);
void BindPPOCRSystemv2(pybind11::module& m); void BindPPOCRv2(pybind11::module& m);
void BindOcr(pybind11::module& m) { void BindOcr(pybind11::module& m) {
auto ocr_module = m.def_submodule("ocr", "Module to deploy OCR models"); auto ocr_module = m.def_submodule("ocr", "Module to deploy OCR models");
BindPPOCRModel(ocr_module); BindPPOCRModel(ocr_module);
BindPPOCRSystemv3(ocr_module); BindPPOCRv3(ocr_module);
BindPPOCRSystemv2(ocr_module); BindPPOCRv2(ocr_module);
} }
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -20,20 +20,36 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
/** \brief All OCR series model APIs are defined inside this namespace
*
*/
namespace ocr { namespace ocr {
/*! @brief Classifier object is used to load the classification model provided by PaddleOCR.
*/
class FASTDEPLOY_DECL Classifier : public FastDeployModel { class FASTDEPLOY_DECL Classifier : public FastDeployModel {
public: public:
Classifier(); Classifier();
/** \brief Set path of model file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g ./ch_ppocr_mobile_v2.0_cls_infer/model.pdmodel.
* \param[in] params_file Path of parameter file, e.g ./ch_ppocr_mobile_v2.0_cls_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
* \param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends`.
* \param[in] model_format Model format of the loaded model, default is Paddle format.
*/
Classifier(const std::string& model_file, const std::string& params_file = "", Classifier(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(), const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::PADDLE); const ModelFormat& model_format = ModelFormat::PADDLE);
/// Get model's name
std::string ModelName() const { return "ppocr/ocr_cls"; } std::string ModelName() const { return "ppocr/ocr_cls"; }
/** \brief Predict the input image and get OCR classification model result.
*
* \param[in] im The input image data, comes from cv::imread().
* \param[in] result The output of OCR classification model result will be writen to this structure.
* \return true if the prediction is successed, otherwise false.
*/
virtual bool Predict(cv::Mat* img, std::tuple<int, float>* result); virtual bool Predict(cv::Mat* img, std::tuple<int, float>* result);
// pre & post parameters // Pre & Post parameters
float cls_thresh; float cls_thresh;
std::vector<int> cls_image_shape; std::vector<int> cls_image_shape;
int cls_batch_num; int cls_batch_num;
@@ -44,9 +60,9 @@ class FASTDEPLOY_DECL Classifier : public FastDeployModel {
private: private:
bool Initialize(); bool Initialize();
/// Preprocess the input data, and set the preprocessed results to `outputs`
bool Preprocess(Mat* img, FDTensor* output); bool Preprocess(Mat* img, FDTensor* output);
/// Postprocess the inferenced results, and set the final result to `result`
bool Postprocess(FDTensor& infer_result, std::tuple<int, float>* result); bool Postprocess(FDTensor& infer_result, std::tuple<int, float>* result);
}; };

View File

@@ -20,22 +20,38 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
/** \brief All OCR series model APIs are defined inside this namespace
*
*/
namespace ocr { namespace ocr {
/*! @brief DBDetector object is used to load the detection model provided by PaddleOCR.
*/
class FASTDEPLOY_DECL DBDetector : public FastDeployModel { class FASTDEPLOY_DECL DBDetector : public FastDeployModel {
public: public:
DBDetector(); DBDetector();
/** \brief Set path of model file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g ./ch_PP-OCRv3_det_infer/model.pdmodel.
* \param[in] params_file Path of parameter file, e.g ./ch_PP-OCRv3_det_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
* \param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends`.
* \param[in] model_format Model format of the loaded model, default is Paddle format.
*/
DBDetector(const std::string& model_file, const std::string& params_file = "", DBDetector(const std::string& model_file, const std::string& params_file = "",
const RuntimeOption& custom_option = RuntimeOption(), const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::PADDLE); const ModelFormat& model_format = ModelFormat::PADDLE);
/// Get model's name
std::string ModelName() const { return "ppocr/ocr_det"; } std::string ModelName() const { return "ppocr/ocr_det"; }
/** \brief Predict the input image and get OCR detection model result.
*
* \param[in] im The input image data, comes from cv::imread().
* \param[in] boxes_result The output of OCR detection model result will be writen to this structure.
* \return true if the prediction is successed, otherwise false.
*/
virtual bool Predict(cv::Mat* im, virtual bool Predict(cv::Mat* im,
std::vector<std::array<int, 8>>* boxes_result); std::vector<std::array<int, 8>>* boxes_result);
// pre&post process parameters // Pre & Post process parameters
int max_side_len; int max_side_len;
float ratio_h{}; float ratio_h{};
@@ -53,14 +69,14 @@ class FASTDEPLOY_DECL DBDetector : public FastDeployModel {
private: private:
bool Initialize(); bool Initialize();
/// Preprocess the input data, and set the preprocessed results to `outputs`
bool Preprocess(Mat* mat, FDTensor* outputs, bool Preprocess(Mat* mat, FDTensor* outputs,
std::map<std::string, std::array<float, 2>>* im_info); std::map<std::string, std::array<float, 2>>* im_info);
/*! @brief Postprocess the inferenced results, and set the final result to `boxes_result`
*/
bool Postprocess(FDTensor& infer_result, bool Postprocess(FDTensor& infer_result,
std::vector<std::array<int, 8>>* boxes_result, std::vector<std::array<int, 8>>* boxes_result,
const std::map<std::string, std::array<float, 2>>& im_info); const std::map<std::string, std::array<float, 2>>& im_info);
PostProcessor post_processor_; PostProcessor post_processor_;
}; };

View File

@@ -15,17 +15,17 @@
#include "fastdeploy/pybind/main.h" #include "fastdeploy/pybind/main.h"
namespace fastdeploy { namespace fastdeploy {
void BindPPOCRSystemv3(pybind11::module& m) { void BindPPOCRv3(pybind11::module& m) {
// OCRSys // PPOCRv3
pybind11::class_<application::ocrsystem::PPOCRSystemv3, FastDeployModel>( pybind11::class_<pipeline::PPOCRv3, FastDeployModel>(
m, "PPOCRSystemv3") m, "PPOCRv3")
.def(pybind11::init<fastdeploy::vision::ocr::DBDetector*, .def(pybind11::init<fastdeploy::vision::ocr::DBDetector*,
fastdeploy::vision::ocr::Classifier*, fastdeploy::vision::ocr::Classifier*,
fastdeploy::vision::ocr::Recognizer*>()) fastdeploy::vision::ocr::Recognizer*>())
.def(pybind11::init<fastdeploy::vision::ocr::DBDetector*, .def(pybind11::init<fastdeploy::vision::ocr::DBDetector*,
fastdeploy::vision::ocr::Recognizer*>()) fastdeploy::vision::ocr::Recognizer*>())
.def("predict", [](application::ocrsystem::PPOCRSystemv3& self, .def("predict", [](pipeline::PPOCRv3& self,
pybind11::array& data) { pybind11::array& data) {
auto mat = PyArrayToCvMat(data); auto mat = PyArrayToCvMat(data);
vision::OCRResult res; vision::OCRResult res;
@@ -34,16 +34,16 @@ void BindPPOCRSystemv3(pybind11::module& m) {
}); });
} }
void BindPPOCRSystemv2(pybind11::module& m) { void BindPPOCRv2(pybind11::module& m) {
// OCRSys // PPOCRv2
pybind11::class_<application::ocrsystem::PPOCRSystemv2, FastDeployModel>( pybind11::class_<pipeline::PPOCRv2, FastDeployModel>(
m, "PPOCRSystemv2") m, "PPOCRv2")
.def(pybind11::init<fastdeploy::vision::ocr::DBDetector*, .def(pybind11::init<fastdeploy::vision::ocr::DBDetector*,
fastdeploy::vision::ocr::Classifier*, fastdeploy::vision::ocr::Classifier*,
fastdeploy::vision::ocr::Recognizer*>()) fastdeploy::vision::ocr::Recognizer*>())
.def(pybind11::init<fastdeploy::vision::ocr::DBDetector*, .def(pybind11::init<fastdeploy::vision::ocr::DBDetector*,
fastdeploy::vision::ocr::Recognizer*>()) fastdeploy::vision::ocr::Recognizer*>())
.def("predict", [](application::ocrsystem::PPOCRSystemv2& self, .def("predict", [](pipeline::PPOCRv2& self,
pybind11::array& data) { pybind11::array& data) {
auto mat = PyArrayToCvMat(data); auto mat = PyArrayToCvMat(data);
vision::OCRResult res; vision::OCRResult res;

View File

@@ -1,40 +0,0 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h"
namespace fastdeploy {
namespace application {
namespace ocrsystem {
class FASTDEPLOY_DECL PPOCRSystemv3 : public PPOCRSystemv2 {
public:
PPOCRSystemv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model) : PPOCRSystemv2(det_model, cls_model, rec_model) {
// The only difference between v2 and v3
recognizer_->rec_image_shape[1] = 48;
}
PPOCRSystemv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model) : PPOCRSystemv2(det_model, rec_model) {
recognizer_->rec_image_shape[1] = 48;
}
};
} // namespace ocrsystem
} // namespace application
} // namespace fastdeploy

View File

@@ -12,27 +12,26 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/ocr/ppocr/ppocr_system_v2.h" #include "fastdeploy/vision/ocr/ppocr/ppocr_v2.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h" #include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h"
namespace fastdeploy { namespace fastdeploy {
namespace application { namespace pipeline {
namespace ocrsystem { PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
PPOCRSystemv2::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model, fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model) fastdeploy::vision::ocr::Recognizer* rec_model)
: detector_(det_model), classifier_(cls_model), recognizer_(rec_model) { : detector_(det_model), classifier_(cls_model), recognizer_(rec_model) {
recognizer_->rec_image_shape[1] = 32; recognizer_->rec_image_shape[1] = 32;
} }
PPOCRSystemv2::PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model) fastdeploy::vision::ocr::Recognizer* rec_model)
: detector_(det_model), recognizer_(rec_model) { : detector_(det_model), recognizer_(rec_model) {
recognizer_->rec_image_shape[1] = 32; recognizer_->rec_image_shape[1] = 32;
} }
bool PPOCRSystemv2::Initialized() const { bool PPOCRv2::Initialized() const {
if (detector_ != nullptr && !detector_->Initialized()){ if (detector_ != nullptr && !detector_->Initialized()){
return false; return false;
@@ -48,21 +47,21 @@ bool PPOCRSystemv2::Initialized() const {
return true; return true;
} }
bool PPOCRSystemv2::Detect(cv::Mat* img, bool PPOCRv2::Detect(cv::Mat* img,
fastdeploy::vision::OCRResult* result) { fastdeploy::vision::OCRResult* result) {
if (!detector_->Predict(img, &(result->boxes))) { if (!detector_->Predict(img, &(result->boxes))) {
FDERROR << "There's error while detecting image in PPOCRSystem." << std::endl; FDERROR << "There's error while detecting image in PPOCR." << std::endl;
return false; return false;
} }
vision::ocr::SortBoxes(result); vision::ocr::SortBoxes(result);
return true; return true;
} }
bool PPOCRSystemv2::Recognize(cv::Mat* img, bool PPOCRv2::Recognize(cv::Mat* img,
fastdeploy::vision::OCRResult* result) { fastdeploy::vision::OCRResult* result) {
std::tuple<std::string, float> rec_result; std::tuple<std::string, float> rec_result;
if (!recognizer_->Predict(img, &rec_result)) { if (!recognizer_->Predict(img, &rec_result)) {
FDERROR << "There's error while recognizing image in PPOCRSystem." << std::endl; FDERROR << "There's error while recognizing image in PPOCR." << std::endl;
return false; return false;
} }
@@ -71,12 +70,12 @@ bool PPOCRSystemv2::Recognize(cv::Mat* img,
return true; return true;
} }
bool PPOCRSystemv2::Classify(cv::Mat* img, bool PPOCRv2::Classify(cv::Mat* img,
fastdeploy::vision::OCRResult* result) { fastdeploy::vision::OCRResult* result) {
std::tuple<int, float> cls_result; std::tuple<int, float> cls_result;
if (!classifier_->Predict(img, &cls_result)) { if (!classifier_->Predict(img, &cls_result)) {
FDERROR << "There's error while classifying image in PPOCRSystem." << std::endl; FDERROR << "There's error while classifying image in PPOCR." << std::endl;
return false; return false;
} }
@@ -85,7 +84,7 @@ bool PPOCRSystemv2::Classify(cv::Mat* img,
return true; return true;
} }
bool PPOCRSystemv2::Predict(cv::Mat* img, bool PPOCRv2::Predict(cv::Mat* img,
fastdeploy::vision::OCRResult* result) { fastdeploy::vision::OCRResult* result) {
result->Clear(); result->Clear();
if (nullptr != detector_ && !Detect(img, result)) { if (nullptr != detector_ && !Detect(img, result)) {
@@ -120,6 +119,5 @@ bool PPOCRSystemv2::Predict(cv::Mat* img,
return true; return true;
}; };
} // namesapce ocrsystem } // namesapce pipeline
} // namespace application
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -26,18 +26,38 @@
#include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h" #include "fastdeploy/vision/ocr/ppocr/utils/ocr_postprocess_op.h"
namespace fastdeploy { namespace fastdeploy {
namespace application { /** \brief This pipeline can launch detection model, classification model and recognition model sequentially. All OCR pipeline APIs are defined inside this namespace.
namespace ocrsystem { *
*/
class FASTDEPLOY_DECL PPOCRSystemv2 : public FastDeployModel { namespace pipeline {
/*! @brief PPOCRv2 is used to load PP-OCRv2 series models provided by PaddleOCR.
*/
class FASTDEPLOY_DECL PPOCRv2 : public FastDeployModel {
public: public:
PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, /** \brief Set up the detection model path, classification model path and recognition model path respectively.
*
* \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv2_det_infer
* \param[in] cls_model Path of classification model, e.g ./ch_ppocr_mobile_v2.0_cls_infer
* \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv2_rec_infer
*/
PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model, fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
PPOCRSystemv2(fastdeploy::vision::ocr::DBDetector* det_model, /** \brief Classification model is optional, so this function is set up the detection model path and recognition model path respectively.
*
* \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv2_det_infer
* \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv2_rec_infer
*/
PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model); fastdeploy::vision::ocr::Recognizer* rec_model);
/** \brief Predict the input image and get OCR result.
*
* \param[in] im The input image data, comes from cv::imread().
* \param[in] result The output OCR result will be writen to this structure.
* \return true if the prediction successed, otherwise false.
*/
virtual bool Predict(cv::Mat* img, fastdeploy::vision::OCRResult* result); virtual bool Predict(cv::Mat* img, fastdeploy::vision::OCRResult* result);
bool Initialized() const override; bool Initialized() const override;
@@ -45,12 +65,19 @@ class FASTDEPLOY_DECL PPOCRSystemv2 : public FastDeployModel {
fastdeploy::vision::ocr::DBDetector* detector_ = nullptr; fastdeploy::vision::ocr::DBDetector* detector_ = nullptr;
fastdeploy::vision::ocr::Classifier* classifier_ = nullptr; fastdeploy::vision::ocr::Classifier* classifier_ = nullptr;
fastdeploy::vision::ocr::Recognizer* recognizer_ = nullptr; fastdeploy::vision::ocr::Recognizer* recognizer_ = nullptr;
/// Launch the detection process in OCR.
virtual bool Detect(cv::Mat* img, fastdeploy::vision::OCRResult* result); virtual bool Detect(cv::Mat* img, fastdeploy::vision::OCRResult* result);
/// Launch the recognition process in OCR.
virtual bool Recognize(cv::Mat* img, fastdeploy::vision::OCRResult* result); virtual bool Recognize(cv::Mat* img, fastdeploy::vision::OCRResult* result);
/// Launch the classification process in OCR.
virtual bool Classify(cv::Mat* img, fastdeploy::vision::OCRResult* result); virtual bool Classify(cv::Mat* img, fastdeploy::vision::OCRResult* result);
}; };
namespace application {
namespace ocrsystem {
typedef pipeline::PPOCRv2 PPOCRSystemv2;
} // namespace ocrsystem } // namespace ocrsystem
} // namespace application } // namespace application
} // namespace pipeline
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -0,0 +1,62 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "fastdeploy/vision/ocr/ppocr/ppocr_v2.h"
namespace fastdeploy {
/** \brief This pipeline can launch detection model, classification model and recognition model sequentially. All OCR pipeline APIs are defined inside this namespace.
*
*/
namespace pipeline {
/*! @brief PPOCRv3 is used to load PP-OCRv3 series models provided by PaddleOCR.
*/
class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 {
public:
/** \brief Set up the detection model path, classification model path and recognition model path respectively.
*
* \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv3_det_infer
* \param[in] cls_model Path of classification model, e.g ./ch_ppocr_mobile_v2.0_cls_infer
* \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv3_rec_infer
*/
PPOCRv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Classifier* cls_model,
fastdeploy::vision::ocr::Recognizer* rec_model)
: PPOCRv2(det_model, cls_model, rec_model) {
// The only difference between v2 and v3
recognizer_->rec_image_shape[1] = 48;
}
/** \brief Classification model is optional, so this function is set up the detection model path and recognition model path respectively.
*
* \param[in] det_model Path of detection model, e.g ./ch_PP-OCRv3_det_infer
* \param[in] rec_model Path of recognition model, e.g ./ch_PP-OCRv3_rec_infer
*/
PPOCRv3(fastdeploy::vision::ocr::DBDetector* det_model,
fastdeploy::vision::ocr::Recognizer* rec_model)
: PPOCRv2(det_model, rec_model) {
// The only difference between v2 and v3
recognizer_->rec_image_shape[1] = 48;
}
};
} // namespace pipeline
namespace application {
namespace ocrsystem {
typedef pipeline::PPOCRv3 PPOCRSystemv3;
} // namespace ocrsystem
} // namespace application
} // namespace fastdeploy

View File

@@ -56,9 +56,6 @@ Recognizer::Recognizer(const std::string& model_file,
runtime_option.model_format = model_format; runtime_option.model_format = model_format;
runtime_option.model_file = model_file; runtime_option.model_file = model_file;
runtime_option.params_file = params_file; runtime_option.params_file = params_file;
runtime_option.DeletePaddleBackendPass("matmul_transpose_reshape_fuse_pass");
runtime_option.DeletePaddleBackendPass(
"matmul_transpose_reshape_mkldnn_fuse_pass");
initialized = Initialize(); initialized = Initialize();

View File

@@ -20,22 +20,39 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
/** \brief All OCR series model APIs are defined inside this namespace
*
*/
namespace ocr { namespace ocr {
/*! @brief Recognizer object is used to load the recognition model provided by PaddleOCR.
*/
class FASTDEPLOY_DECL Recognizer : public FastDeployModel { class FASTDEPLOY_DECL Recognizer : public FastDeployModel {
public: public:
Recognizer(); Recognizer();
/** \brief Set path of model file, and the configuration of runtime
*
* \param[in] model_file Path of model file, e.g ./ch_PP-OCRv3_rec_infer/model.pdmodel.
* \param[in] params_file Path of parameter file, e.g ./ch_PP-OCRv3_rec_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
* \param[in] label_path Path of label file used by OCR recognition model. e.g ./ppocr_keys_v1.txt
* \param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends`.
* \param[in] model_format Model format of the loaded model, default is Paddle format.
*/
Recognizer(const std::string& model_file, const std::string& params_file = "", Recognizer(const std::string& model_file, const std::string& params_file = "",
const std::string& label_path = "", const std::string& label_path = "",
const RuntimeOption& custom_option = RuntimeOption(), const RuntimeOption& custom_option = RuntimeOption(),
const ModelFormat& model_format = ModelFormat::PADDLE); const ModelFormat& model_format = ModelFormat::PADDLE);
/// Get model's name
std::string ModelName() const { return "ppocr/ocr_rec"; } std::string ModelName() const { return "ppocr/ocr_rec"; }
/** \brief Predict the input image and get OCR recognition model result.
*
* \param[in] im The input image data, comes from cv::imread().
* \param[in] rec_result The output of OCR recognition model result will be writen to this structure.
* \return true if the prediction is successed, otherwise false.
*/
virtual bool Predict(cv::Mat* img, virtual bool Predict(cv::Mat* img,
std::tuple<std::string, float>* rec_result); std::tuple<std::string, float>* rec_result);
// pre & post parameters // Pre & Post parameters
std::vector<std::string> label_list; std::vector<std::string> label_list;
int rec_batch_num; int rec_batch_num;
int rec_img_h; int rec_img_h;
@@ -48,10 +65,11 @@ class FASTDEPLOY_DECL Recognizer : public FastDeployModel {
private: private:
bool Initialize(); bool Initialize();
/// Preprocess the input data, and set the preprocessed results to `outputs`
bool Preprocess(Mat* img, FDTensor* outputs, bool Preprocess(Mat* img, FDTensor* outputs,
const std::vector<int>& rec_image_shape); const std::vector<int>& rec_image_shape);
/*! @brief Postprocess the inferenced results, and set the final result to `rec_result`
*/
bool Postprocess(FDTensor& infer_result, bool Postprocess(FDTensor& infer_result,
std::tuple<std::string, float>* rec_result); std::tuple<std::string, float>* rec_result);
}; };

View File

@@ -13,6 +13,8 @@
# limitations under the License. # limitations under the License.
from __future__ import absolute_import from __future__ import absolute_import
from .ppocr import PPOCRv3
from .ppocr import PPOCRv2
from .ppocr import PPOCRSystemv3 from .ppocr import PPOCRSystemv3
from .ppocr import PPOCRSystemv2 from .ppocr import PPOCRSystemv2
from .ppocr import DBDetector from .ppocr import DBDetector

View File

@@ -24,8 +24,13 @@ class DBDetector(FastDeployModel):
params_file="", params_file="",
runtime_option=None, runtime_option=None,
model_format=ModelFormat.PADDLE): model_format=ModelFormat.PADDLE):
# 调用基函数进行backend_option的初始化 """Load OCR detection model provided by PaddleOCR.
# 初始化后的option保存在self._runtime_option
:param model_file: (str)Path of model file, e.g ./ch_PP-OCRv3_det_infer/model.pdmodel.
:param params_file: (str)Path of parameter file, e.g ./ch_PP-OCRv3_det_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
:param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU.
:param model_format: (fastdeploy.ModelForamt)Model format of the loaded model.
"""
super(DBDetector, self).__init__(runtime_option) super(DBDetector, self).__init__(runtime_option)
if (len(model_file) == 0): if (len(model_file) == 0):
@@ -33,7 +38,6 @@ class DBDetector(FastDeployModel):
else: else:
self._model = C.vision.ocr.DBDetector( self._model = C.vision.ocr.DBDetector(
model_file, params_file, self._runtime_option, model_format) model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "DBDetector initialize failed." assert self.initialized, "DBDetector initialize failed."
# 一些跟DBDetector模型有关的属性封装 # 一些跟DBDetector模型有关的属性封装
@@ -81,8 +85,8 @@ class DBDetector(FastDeployModel):
@det_db_box_thresh.setter @det_db_box_thresh.setter
def det_db_box_thresh(self, value): def det_db_box_thresh(self, value):
assert isinstance( assert isinstance(
value, value, float
float), "The value to set `det_db_box_thresh` must be type of float." ), "The value to set `det_db_box_thresh` must be type of float."
self._model.det_db_box_thresh = value self._model.det_db_box_thresh = value
@det_db_unclip_ratio.setter @det_db_unclip_ratio.setter
@@ -119,8 +123,13 @@ class Classifier(FastDeployModel):
params_file="", params_file="",
runtime_option=None, runtime_option=None,
model_format=ModelFormat.PADDLE): model_format=ModelFormat.PADDLE):
# 调用基函数进行backend_option的初始化 """Load OCR classification model provided by PaddleOCR.
# 初始化后的option保存在self._runtime_option
:param model_file: (str)Path of model file, e.g ./ch_ppocr_mobile_v2.0_cls_infer/model.pdmodel.
:param params_file: (str)Path of parameter file, e.g ./ch_ppocr_mobile_v2.0_cls_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
:param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU.
:param model_format: (fastdeploy.ModelForamt)Model format of the loaded model.
"""
super(Classifier, self).__init__(runtime_option) super(Classifier, self).__init__(runtime_option)
if (len(model_file) == 0): if (len(model_file) == 0):
@@ -128,7 +137,6 @@ class Classifier(FastDeployModel):
else: else:
self._model = C.vision.ocr.Classifier( self._model = C.vision.ocr.Classifier(
model_file, params_file, self._runtime_option, model_format) model_file, params_file, self._runtime_option, model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "Classifier initialize failed." assert self.initialized, "Classifier initialize failed."
@property @property
@@ -159,7 +167,8 @@ class Classifier(FastDeployModel):
@cls_batch_num.setter @cls_batch_num.setter
def cls_batch_num(self, value): def cls_batch_num(self, value):
assert isinstance( assert isinstance(
value, int), "The value to set `cls_batch_num` must be type of int." value,
int), "The value to set `cls_batch_num` must be type of int."
self._model.cls_batch_num = value self._model.cls_batch_num = value
@@ -170,8 +179,14 @@ class Recognizer(FastDeployModel):
label_path="", label_path="",
runtime_option=None, runtime_option=None,
model_format=ModelFormat.PADDLE): model_format=ModelFormat.PADDLE):
# 调用基函数进行backend_option的初始化 """Load OCR recognition model provided by PaddleOCR
# 初始化后的option保存在self._runtime_option
:param model_file: (str)Path of model file, e.g ./ch_PP-OCRv3_rec_infer/model.pdmodel.
:param params_file: (str)Path of parameter file, e.g ./ch_PP-OCRv3_rec_infer/model.pdiparams, if the model format is ONNX, this parameter will be ignored.
:param label_path: (str)Path of label file used by OCR recognition model. e.g ./ppocr_keys_v1.txt
:param runtime_option: (fastdeploy.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU.
:param model_format: (fastdeploy.ModelForamt)Model format of the loaded model.
"""
super(Recognizer, self).__init__(runtime_option) super(Recognizer, self).__init__(runtime_option)
if (len(model_file) == 0): if (len(model_file) == 0):
@@ -180,7 +195,6 @@ class Recognizer(FastDeployModel):
self._model = C.vision.ocr.Recognizer( self._model = C.vision.ocr.Recognizer(
model_file, params_file, label_path, self._runtime_option, model_file, params_file, label_path, self._runtime_option,
model_format) model_format)
# 通过self.initialized判断整个模型的初始化是否成功
assert self.initialized, "Recognizer initialize failed." assert self.initialized, "Recognizer initialize failed."
@property @property
@@ -210,33 +224,78 @@ class Recognizer(FastDeployModel):
@rec_batch_num.setter @rec_batch_num.setter
def rec_batch_num(self, value): def rec_batch_num(self, value):
assert isinstance( assert isinstance(
value, int), "The value to set `rec_batch_num` must be type of int." value,
int), "The value to set `rec_batch_num` must be type of int."
self._model.rec_batch_num = value self._model.rec_batch_num = value
class PPOCRSystemv3(FastDeployModel): class PPOCRv3(FastDeployModel):
def __init__(self, det_model=None, cls_model=None, rec_model=None): def __init__(self, det_model=None, cls_model=None, rec_model=None):
"""Consruct a pipeline with text detector, direction classifier and text recognizer models
:param det_model: (FastDeployModel) The detection model object created by fastdeploy.vision.ocr.DBDetector.
:param cls_model: (FastDeployModel) The classification model object created by fastdeploy.vision.ocr.Classifier.
:param rec_model: (FastDeployModel) The recognition model object created by fastdeploy.vision.ocr.Recognizer.
"""
assert det_model is not None and rec_model is not None, "The det_model and rec_model cannot be None." assert det_model is not None and rec_model is not None, "The det_model and rec_model cannot be None."
if cls_model is None: if cls_model is None:
self.system = C.vision.ocr.PPOCRSystemv3(det_model._model, self.system = C.vision.ocr.PPOCRv3(det_model._model,
rec_model._model) rec_model._model)
else: else:
self.system = C.vision.ocr.PPOCRSystemv3( self.system = C.vision.ocr.PPOCRv3(
det_model._model, cls_model._model, rec_model._model) det_model._model, cls_model._model, rec_model._model)
def predict(self, input_image): def predict(self, input_image):
"""Predict an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: OCRResult
"""
return self.system.predict(input_image) return self.system.predict(input_image)
class PPOCRSystemv2(FastDeployModel): class PPOCRSystemv3(PPOCRv3):
def __init__(self, det_model=None, cls_model=None, rec_model=None): def __init__(self, det_model=None, cls_model=None, rec_model=None):
logging.warning(
"DEPRECATED: fd.vision.ocr.PPOCRSystemv3 is deprecated, "
"please use fd.vision.ocr.PPOCRv3 instead.")
super(PPOCRSystemv3, self).__init__(det_model, cls_model, rec_model)
def predict(self, input_image):
return super(PPOCRSystemv3, self).predict(input_image)
class PPOCRv2(FastDeployModel):
def __init__(self, det_model=None, cls_model=None, rec_model=None):
"""Consruct a pipeline with text detector, direction classifier and text recognizer models
:param det_model: (FastDeployModel) The detection model object created by fastdeploy.vision.ocr.DBDetector.
:param cls_model: (FastDeployModel) The classification model object created by fastdeploy.vision.ocr.Classifier.
:param rec_model: (FastDeployModel) The recognition model object created by fastdeploy.vision.ocr.Recognizer.
"""
assert det_model is not None and rec_model is not None, "The det_model and rec_model cannot be None." assert det_model is not None and rec_model is not None, "The det_model and rec_model cannot be None."
if cls_model is None: if cls_model is None:
self.system = C.vision.ocr.PPOCRSystemv2(det_model._model, self.system = C.vision.ocr.PPOCRv2(det_model._model,
rec_model._model) rec_model._model)
else: else:
self.system = C.vision.ocr.PPOCRSystemv2( self.system = C.vision.ocr.PPOCRv2(
det_model._model, cls_model._model, rec_model._model) det_model._model, cls_model._model, rec_model._model)
def predict(self, input_image): def predict(self, input_image):
"""Predict an input image
:param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
:return: OCRResult
"""
return self.system.predict(input_image) return self.system.predict(input_image)
class PPOCRSystemv2(PPOCRv2):
def __init__(self, det_model=None, cls_model=None, rec_model=None):
logging.warning(
"DEPRECATED: fd.vision.ocr.PPOCRSystemv2 is deprecated, "
"please use fd.vision.ocr.PPOCRv2 instead.")
super(PPOCRSystemv2, self).__init__(det_model, cls_model, rec_model)
def predict(self, input_image):
return super(PPOCRSystemv2, self).predict(input_image)