diff --git a/mxVision/GeneralTextRecognition/C++/CMakeLists.txt b/mxVision/GeneralTextRecognition/C++/CMakeLists.txt index f72bdc8ad36308c15ab66a47eca741e7263ff984..04d8cffd163c61b4d2a32deb455ce48c926da72b 100644 --- a/mxVision/GeneralTextRecognition/C++/CMakeLists.txt +++ b/mxVision/GeneralTextRecognition/C++/CMakeLists.txt @@ -8,6 +8,7 @@ project(Test) set(CMAKE_CXX_STANDARD 11) set(MX_SDK_HOME $ENV{MX_SDK_HOME}) +set(CMAKE_EXE_LINKER_FLAGS "-Wl,--copy-dt-needed-entries") if (NOT DEFINED ENV{MX_SDK_HOME}) string(REGEX REPLACE "(.*)/(.*)/(.*)/(.*)" "\\1" MX_SDK_HOME ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/mxVision/GeneralTextRecognition/C++/mainMultiThread.cpp b/mxVision/GeneralTextRecognition/C++/mainMultiThread.cpp index 5e94950103b1d62624a2d31f2517f7c81bf1e131..855ce70ffd5f2fd17171c4804429e290256a22c5 100644 --- a/mxVision/GeneralTextRecognition/C++/mainMultiThread.cpp +++ b/mxVision/GeneralTextRecognition/C++/mainMultiThread.cpp @@ -84,7 +84,7 @@ APP_ERROR GetCallback(MxStreamManager& mxStreamManager, const std::string& strea return APP_ERR_COMM_FAILURE; } std::string dataStr = std::string((char *)output->dataPtr, output->dataSize); - LogInfo << "[" << streamName << "] GetResult: " << dataStr; + std::cout << "[" << streamName << "] GetResult: " << dataStr << std::endl; } return APP_ERR_OK; } @@ -110,7 +110,7 @@ APP_ERROR SendCallback(MxStreamManager& mxStreamManager, } APP_ERROR TestMultiThread(const std::string& pipelinePath) { - LogDebug << "********case TestMultiThread********" << std::endl; + std::cout << "********case TestMultiThread********" << std::endl; MxStream::MxStreamManager mxStreamManager; APP_ERROR ret = mxStreamManager.InitManager(); diff --git a/mxVision/GeneralTextRecognition/README.md b/mxVision/GeneralTextRecognition/README.md index bf0828a39e40b767fa8c91983ab9eaafad531e49..d8cac07ddd2c0d05da65acc9c206566ce17193c9 100644 --- a/mxVision/GeneralTextRecognition/README.md +++ b/mxVision/GeneralTextRecognition/README.md @@ -1,8 +1,10 @@ # 通用文字识别(中英文) -## 1 简介 +## 1 介绍 -通用文字识别样例基于mxVision SDK进行开发,以昇腾Atlas300卡为主要的硬件平台,主要支持以下功能: +### 1.1 简介 + +通用文字识别样例基于mxVision SDK进行开发,主要支持以下功能: 1. 图片读取解码:本样例支持JPG及PNG格式图片,采用OpenCV进行解码、缩放等预处理。 2. 文本检测:在输入图片中检测出文本框,本样例选用基于DBNet的文本检测模型,能达到快速精准检测。 @@ -11,41 +13,35 @@ 5. 文本方向检测:识别文本小图上文本的方向--[0°,180°],如果为180°,则将文本小图进行180°旋转,本样例选用Mobilenet为方向识别模型。 6. 文字识别:识别文本小图上中英文,本样例采用CRNN模型进行文字识别,能够识别中英文. +### 1.2 支持的产品 -## 2 环境依赖 - -- 支持的硬件形态和操作系统版本 - -| 硬件形态 | 操作系统版本 | -| ----------------------------------- | -------------- | -| x86_64+Atlas 300I 推理卡(型号3010) | Ubuntu 18.04.1 | -| x86_64+Atlas 300I 推理卡 (型号3010)| CentOS 7.6 | -| ARM+Atlas 300I 推理卡 (型号3000) | Ubuntu 18.04.1 | -| ARM+Atlas 300I 推理卡 (型号3000) | CentOS 7.6 | +本项目支持昇腾Atlas 300I pro、Atlas 300V pro。 -- 软件依赖 +### 1.3 支持的版本 -| 软件名称 | 版本 | -| -------- | ------ | -| cmake | 3.5.1+ | -| mxVision | 5.0.0 | -| CANN | 7.0.0 | -| Python | 3.7.5 | +本样例配套的MxVision版本、CANN版本、Driver/Firmware版本如下所示: +| MxVision版本 | CANN版本 | Driver/Firmware版本 | +| --------- | ------------------ | -------------- | +| 6.0.RC3 | 8.0.RC3 | 24.1.RC3 | +### 1.4 三方依赖 -## 3 代码主要目录介绍 +| 软件名称 | 版本 | +|-------------------|--------| +| paddlle2onnx | 1.3.1 | +| paddlepaddle | 2.6.0 | +| onnx | 1.10.0 | -本代码仓名称为mxSdkReferenceApps,工程目录如下图所示: +### 1.4 代码目录结构说明 ``` |-- mxVision -| |-- AllObjectsStructuring | |-- GeneralTextRecognition | | |-- C++ | | | |-- CMakeLists.txt | | | |-- mainMultiThread.cpp -| | | `-- run.sh +| | | |-- run.sh | | |-- License.md | | |-- README.md | | |-- THIRD PARTY OPEN SOURCE SOFTWARE NOTICE.md @@ -55,43 +51,93 @@ | | | |-- config | | | | |-- cls | | | | | |-- cls.cfg -| | | | | `-- ic15.names +| | | | | |-- ic15.names | | | | |-- det -| | | | | `-- det.cfg -| | | | `-- rec -| | | | `-- rec_cfg.txt -| | | `-- model -| | | |-- MODEL.md +| | | | | |-- det.cfg +| | | | |-- rec +| | | | |-- rec_cfg.txt +| | | |-- model | | | |-- cls_aipp.cfg | | | |-- det_aipp.cfg -| | | `-- rec_aipp.cfg +| | | |-- rec_aipp.cfg +| | | |-- run.sh | | |-- main_ocr.py -| | `-- src +| | |-- src | | |-- Clipper -| | | `-- CMakeLists.txt +| | | |-- CMakeLists.txt | | |-- DBPostProcess | | | |-- CMakeLists.txt | | | |-- DBPostProcess.cpp -| | | `-- DBPostProcess.h -| | `-- README.md +| | | |-- DBPostProcess.h ``` -## 4 准备 +### 1.5 相关约束 + +本项目支持通用文字识别,支持图片格式为jpg、jpeg、png。 + +## 2 设置环境变量 -**步骤1:** 参考安装教程《mxVision 用户指南》安装 mxVision SDK。 +```bash +. /usr/local/Ascend/ascend-toolkit/set_env.sh # toolkit默认安装路径,根据实际安装路径修改 +. ${SDK_INSTALL_PATH}/mxVision/set_env.sh # sdk安装路径,根据实际安装路径修改 +``` -**步骤2:** 配置 mxVision SDK 环境变量。 +## 3 准备模型 -`. {sdk_install_path}/mxVision/set_env.sh ` +**步骤1:** 模型下载。 -注:本例中mxVision SDK安装路径为 /root/MindX_SDK。 +本样例采用[PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR)的release/2.1分支作为基准,预训练模型请下载"PP-OCR 2.0 series model list(Update on Dec 15)"->"Chinese and English general OCR model (143.4M)" 对应的三个推理模型: +1. Detection model: [DBNet](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar) +2. Direction classifier model: [Mobilenet](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) +3. Recognition model: [CRNN](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar) -**步骤3:** 编译DBNet模型的后处理动态链接库,请根据./src/README.md, 编译相应的动态链接库。 +**步骤2:** 将下载的3个模型tar包移动至/data/model目录下。其中,Project_Root为样例代码根目录。 -**步骤4:** 准备模型,根据./data/model/MODEL.md文件转换样例需要的模型,并将模型保存到./data/model/目录下。 +**步骤3:** 执行/data/model下的run.sh脚本,等待片刻。 +```bash +bash run.sh +``` +如果执行成功,界面上会显示3段如下内容(非连续显示),表示om模型已经转换完成: +```bash +ATC start working now, please wait for a moment. +..... +ATC run success, welcome to the next use. +``` -**步骤5:** 下载文字识别模型的[字典](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/ppocr/utils/ppocr_keys_v1.txt), 由于样例使用的CRNN模型,对应的字典从1开始,0代表为空,请在下载的字典首行添加一行"blank",并将修改后的字典保存到./data/config/rec/ppocr_keys_v1.txt, : +## 4 编译与运行 +**步骤1:** 编译clipper动态库。 +在[Clipper网站](https://sourceforge.net/projects/polyclipping/files/)下载`clipper_ver6.4.2.zip`压缩包,解压后将路径cpp下的 `clipper.hpp、clipper.cpp` 到/src/Clipper目录下。依次执行如下命令: +```bash +mkdir build +cd build +cmake .. +make -j +make install +``` + +**步骤2:** 编译后处理动态库DBPostProcess。 + +进入到/src/DBPostProcess路径目录下。依次执行如下命令: +```bash +mkdir build +cd build +cmake .. +make -j +make install +``` + +**步骤3:** 权限设置。 + +DB后处理目前支持两种缩放方式:拉伸缩放`Resizer_Stretch`、 等比例缩放`Resizer_KeepAspectRatio_Fit`。 因此,需要确保编译生成的libclipper.so和libDBPostProcess.so文件权限不高于640, 如果文件权限不满足要求, +进入到/lib目录, 执行如下命令修改文件权限: +```bash +chmod 640 libclipper.so libDBPostProcess.so +``` + +**步骤4:** 准备字典数据。 + +下载文字识别模型的[字典](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/ppocr/utils/ppocr_keys_v1.txt), 由于样例使用的CRNN模型,对应的字典从1开始,0代表为空,请在下载的字典首行添加一行"blank",并将修改后的字典保存到/data/config/rec目录,文件命名为ppocr_keys_v1.txt, 修改示例如下: ```bash blank ' @@ -104,60 +150,46 @@ blank . ``` -**步骤6:** 修改配置根目录下的配置文件./data/OCR.pipeline文件: - -1. 将所有“deviceId”字段值替换为实际使用的device的id值,可用的 device id 值可以使用如下命令查看: - - `npu-smi info` - -2. 文本检测使用的DBNet,后处理由步骤三编译得到,默认生成到"./lib/libDBpostprocess.so", 如有修改,请修改./data/OCR.pipeline的对应配置: - - ```bash - "mxpi_textobjectpostprocessor0": { - "props": { - "postProcessConfigPath": "./data/config/det/det.cfg", - "postProcessLibPath": "./lib/libDBpostprocess.so" - }, - "factory": "mxpi_textobjectpostprocessor", - "next": "mxpi_warpperspective0" - }, - ``` - -3. 文本方向检测和文字识别的后处理在mxVision SDK安装目录,本例中mxVision SDK安装路径为 /root/MindX_SDK,如有修改,请修改./data/OCR.pipeline的对应配置: - - ```bash - "mxpi_classpostprocessor0": { - "props": { - "dataSource": "mxpi_tensorinfer1", - "postProcessConfigPath": "./data/configs/cls/cls.cfg", - "labelPath": "./data/configs/cls/ic15.names", - "postProcessLibPath": "/root/MindX_SDK/lib/modelpostprocessors/libresnet50postprocess.so" - }, - "factory": "mxpi_classpostprocessor", - "next": "mxpi_rotation1:1" - }, - . - . - . - "mxpi_textgenerationpostprocessor0": { - "props": { - "dataSource": "crnn_recognition", - "postProcessConfigPath": "./data/config/rec/rec_cfg.txt", - "labelPath": "./data/config/rec/ppocr_keys_v1.txt", - "postProcessLibPath": "/root/MindX_SDK/lib/modelpostprocessors/libcrnnpostprocess.so" - }, - "factory": "mxpi_textgenerationpostprocessor", - "next": "mxpi_dataserialize0" - }, - ``` - -**步骤7:** 准备测试图片,在根目录下创建input_data目录,并将包含中英文的JPG或PNG图片拷贝到input_data目录下: +**步骤5:** 修改配置根目录下的配置文件。 + +将所有`deviceId`字段值替换为实际使用的device的id值,可用的`deviceId`值可以使用如下命令查看: +```bash +npu-smi info +``` +文本检测使用的DBNet后处理由步骤2编译得到,默认生成到"/lib/libDBpostprocess.so", 如有修改,请修改/data/OCR.pipeline和OCR_multi3.pipeline的对应配置,将标识符更改为实际的路径,OCR.pipeline示例如下: +```bash +# 44行 "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" +. +. +# 55行 "postProcessConfigPath": "/data/config/det/det.cfg", +# 56行 "postProcessLibPath": "/lib/libDBPostProcess.so" +. +. +# 199行 "labelPath": "/data/config/rec/ppocr_keys_v1.txt", +``` +OCR_multi3.pipeline示例如下: +```bash +# 44行 "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" +. +# 265行 "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" +. +. +# 641行 "labelPath": "/data/config/rec/ppocr_keys_v1.txt", +``` +最后,请将pipline下的所有**Project_Root**路径更换为实际的项目路径,例如/root/GeneralTextRecognition/data/config/rec/rec_cfg.txt,/root/GeneralTextRecognition为实际项目路径。 + +**步骤6:** 准备测试图片,在根目录下创建input_data目录,并将包含中英文的JPG或PNG图片拷贝到input_data目录下 + +**步骤7:** 运行多线程高性能c++样例 + +多线程高性能c++样例输入与输出解耦,多线程发送与读取数据。运行前确保/data/OCR_multi3.pipeline已完成修改,而后进入到/C++目录,执行如下命令: +```bash +bash run.sh +``` ## 5 运行 -- 简易python运行样例 - - `python3 main_ocr.py` -
-- 多线程高性能c++样例:输入与输出解耦,多线程发送与读取数据; - - 源文件 `./C++/mainMultiThread.cpp`, 配置 `./data/OCR_multi3.pipeline` 和 `input_data` - - 运行 `bash run.sh` +提供简易python运行样例,请参考第4小结中步骤1至步骤6完成配置准备,进入到/python目录,执行如下命令: +```bash +python3 main_ocr.py +``` diff --git a/mxVision/GeneralTextRecognition/data/OCR.pipeline b/mxVision/GeneralTextRecognition/data/OCR.pipeline index 8e50be3875b86e18e32fc5af05a4ec6304187fef..5af8e630ae23f669319642f455c35169916c1d64 100644 --- a/mxVision/GeneralTextRecognition/data/OCR.pipeline +++ b/mxVision/GeneralTextRecognition/data/OCR.pipeline @@ -41,7 +41,7 @@ "mxpi_tensorinfer0": { "props": { "dataSource": "mxpi_imageresizer0", - "modelPath": "./data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" + "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" }, "factory": "mxpi_tensorinfer", "next": "queue3" @@ -52,8 +52,8 @@ }, "mxpi_textobjectpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/det/det.cfg", - "postProcessLibPath": "./lib/libDBPostProcess.so" + "postProcessConfigPath": "/data/config/det/det.cfg", + "postProcessLibPath": "/lib/libDBPostProcess.so" }, "factory": "mxpi_textobjectpostprocessor", "next": "queue4" @@ -124,7 +124,7 @@ "mxpi_tensorinfer1": { "props": { "dataSource": "mxpi_imageresize1", - "modelPath": "./data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", + "modelPath": "/data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", "waitingTime": "50", "singleBatchInfer": "0" }, @@ -137,8 +137,8 @@ }, "mxpi_classpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/cls/cls.cfg", - "labelPath": "./data/config/cls/ic15.names", + "postProcessConfigPath": "/data/config/cls/cls.cfg", + "labelPath": "/data/config/cls/ic15.names", "postProcessLibPath": "libresnet50postprocess.so" }, "factory": "mxpi_classpostprocessor", @@ -182,7 +182,7 @@ "crnn_recognition": { "props": { "dataSource": "mxpi_imageresize2", - "modelPath": "./data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", + "modelPath": "/data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", "waitingTime": "10", "singleBatchInfer": "0" }, @@ -195,8 +195,8 @@ }, "mxpi_textgenerationpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/rec/rec_cfg.txt", - "labelPath": "./data/config/rec/ppocr_keys_v1.txt", + "postProcessConfigPath": "/data/config/rec/rec_cfg.txt", + "labelPath": "/data/config/rec/ppocr_keys_v1.txt", "postProcessLibPath": "libcrnnpostprocess.so" }, "factory": "mxpi_textgenerationpostprocessor", diff --git a/mxVision/GeneralTextRecognition/data/OCR_multi3.pipeline b/mxVision/GeneralTextRecognition/data/OCR_multi3.pipeline index 27c762193de060e63cee17625ff253b5c68bf271..5c0685380a92ca3b2441cbe976058a5c1b5b89c9 100644 --- a/mxVision/GeneralTextRecognition/data/OCR_multi3.pipeline +++ b/mxVision/GeneralTextRecognition/data/OCR_multi3.pipeline @@ -41,7 +41,7 @@ "mxpi_tensorinfer0": { "props": { "dataSource": "mxpi_imageresizer0", - "modelPath": "./data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" + "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" }, "factory": "mxpi_tensorinfer", "next": "queue3" @@ -52,8 +52,8 @@ }, "mxpi_textobjectpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/det/det.cfg", - "postProcessLibPath": "./lib/libDBPostProcess.so" + "postProcessConfigPath": "/data/config/det/det.cfg", + "postProcessLibPath": "/lib/libDBPostProcess.so" }, "factory": "mxpi_textobjectpostprocessor", "next": "queue4" @@ -124,7 +124,7 @@ "mxpi_tensorinfer1": { "props": { "dataSource": "mxpi_imageresize1", - "modelPath": "./data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", + "modelPath": "/data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", "waitingTime": "50", "singleBatchInfer": "0" }, @@ -137,8 +137,8 @@ }, "mxpi_classpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/cls/cls.cfg", - "labelPath": "./data/config/cls/ic15.names", + "postProcessConfigPath": "/data/config/cls/cls.cfg", + "labelPath": "/data/config/cls/ic15.names", "postProcessLibPath": "libresnet50postprocess.so" }, "factory": "mxpi_classpostprocessor", @@ -182,7 +182,7 @@ "crnn_recognition": { "props": { "dataSource": "mxpi_imageresize2", - "modelPath": "./data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", + "modelPath": "/data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", "waitingTime": "10", "singleBatchInfer": "0" }, @@ -195,8 +195,8 @@ }, "mxpi_textgenerationpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/rec/rec_cfg.txt", - "labelPath": "./data/config/rec/ppocr_keys_v1.txt", + "postProcessConfigPath": "/data/config/rec/rec_cfg.txt", + "labelPath": "/data/config/rec/ppocr_keys_v1.txt", "postProcessLibPath": "libcrnnpostprocess.so" }, "factory": "mxpi_textgenerationpostprocessor", @@ -262,7 +262,7 @@ "mxpi_tensorinfer0": { "props": { "dataSource": "mxpi_imageresizer0", - "modelPath": "./data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" + "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" }, "factory": "mxpi_tensorinfer", "next": "queue3" @@ -273,8 +273,8 @@ }, "mxpi_textobjectpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/det/det.cfg", - "postProcessLibPath": "./lib/libDBPostProcess.so" + "postProcessConfigPath": "/data/config/det/det.cfg", + "postProcessLibPath": "/lib/libDBPostProcess.so" }, "factory": "mxpi_textobjectpostprocessor", "next": "queue4" @@ -345,7 +345,7 @@ "mxpi_tensorinfer1": { "props": { "dataSource": "mxpi_imageresize1", - "modelPath": "./data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", + "modelPath": "/data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", "waitingTime": "50", "singleBatchInfer": "0" }, @@ -358,8 +358,8 @@ }, "mxpi_classpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/cls/cls.cfg", - "labelPath": "./data/config/cls/ic15.names", + "postProcessConfigPath": "/data/config/cls/cls.cfg", + "labelPath": "/data/config/cls/ic15.names", "postProcessLibPath": "libresnet50postprocess.so" }, "factory": "mxpi_classpostprocessor", @@ -403,7 +403,7 @@ "crnn_recognition": { "props": { "dataSource": "mxpi_imageresize2", - "modelPath": "./data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", + "modelPath": "/data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", "waitingTime": "10", "singleBatchInfer": "0" }, @@ -416,8 +416,8 @@ }, "mxpi_textgenerationpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/rec/rec_cfg.txt", - "labelPath": "./data/config/rec/ppocr_keys_v1.txt", + "postProcessConfigPath": "/data/config/rec/rec_cfg.txt", + "labelPath": "/data/config/rec/ppocr_keys_v1.txt", "postProcessLibPath": "libcrnnpostprocess.so" }, "factory": "mxpi_textgenerationpostprocessor", @@ -483,7 +483,7 @@ "mxpi_tensorinfer0": { "props": { "dataSource": "mxpi_imageresizer0", - "modelPath": "./data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" + "modelPath": "/data/model/Dynamic24_ch_ppocr_server_v2.0_det_infer.om" }, "factory": "mxpi_tensorinfer", "next": "queue3" @@ -494,8 +494,8 @@ }, "mxpi_textobjectpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/det/det.cfg", - "postProcessLibPath": "./lib/libDBPostProcess.so" + "postProcessConfigPath": "/data/config/det/det.cfg", + "postProcessLibPath": "/lib/libDBPostProcess.so" }, "factory": "mxpi_textobjectpostprocessor", "next": "queue4" @@ -566,7 +566,7 @@ "mxpi_tensorinfer1": { "props": { "dataSource": "mxpi_imageresize1", - "modelPath": "./data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", + "modelPath": "/data/model/ch_ppocr_mobile_v2.0_cls_infer_3_48_192.om", "waitingTime": "50", "singleBatchInfer": "0" }, @@ -579,8 +579,8 @@ }, "mxpi_classpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/cls/cls.cfg", - "labelPath": "./data/config/cls/ic15.names", + "postProcessConfigPath": "/data/config/cls/cls.cfg", + "labelPath": "/data/config/cls/ic15.names", "postProcessLibPath": "libresnet50postprocess.so" }, "factory": "mxpi_classpostprocessor", @@ -624,7 +624,7 @@ "crnn_recognition": { "props": { "dataSource": "mxpi_imageresize2", - "modelPath": "./data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", + "modelPath": "/data/model/ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16.om", "waitingTime": "10", "singleBatchInfer": "0" }, @@ -637,8 +637,8 @@ }, "mxpi_textgenerationpostprocessor0": { "props": { - "postProcessConfigPath": "./data/config/rec/rec_cfg.txt", - "labelPath": "./data/config/rec/ppocr_keys_v1.txt", + "postProcessConfigPath": "/data/config/rec/rec_cfg.txt", + "labelPath": "/data/config/rec/ppocr_keys_v1.txt", "postProcessLibPath": "libcrnnpostprocess.so" }, "factory": "mxpi_textgenerationpostprocessor", diff --git a/mxVision/GeneralTextRecognition/data/model/MODEL.md b/mxVision/GeneralTextRecognition/data/model/MODEL.md deleted file mode 100644 index 4a2fbabf61d73ae886461ec0774476f71093ddea..0000000000000000000000000000000000000000 --- a/mxVision/GeneralTextRecognition/data/model/MODEL.md +++ /dev/null @@ -1,112 +0,0 @@ -# OCR模型转换 -## 一、PaddleOCR的inference模型转onnx模型 -### 环境依赖 -#### 用户环境配置 - python == 3.7.5 - 静态图: paddlepaddle >= 1.8.0 - 动态图: paddlepaddle >= 2.0.0 - onnx >= 1.7.0 -#### 安装 -##### 安装方式1 - pip install paddle2onnx -##### 安装方式2 - git clone https://github.com/PaddlePaddle/paddle2onnx.git - 进入到下载好的paddle2onnx源码根目录, 执行如下命令 - python setup.py install - -#### 静态图模型导出 -##### 下载预训练模型 - -本样例采用[PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR)的release/2.1分支作为基准,预训练模型请下载"PP-OCR 2.0 series model list(Update on Dec 15)"->"Chinese and English general OCR model (143.4M)" 对应的三个推理模型: - Detection model: [DBNet](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar) - Direction classifier model: [Mobilenet](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar) - Recognition model: [CRNN](https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar) - -##### 检测模型转onnx模型 - - paddle2onnx --model_dir ./ch_ppocr_server_v2.0_det_infer/ch_ppocr_server_v2.0_det_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_server_v2.0_det_infer.onnx --opset_version 11 --enable_onnx_checker True - -##### 方向分类模型转onnx模型 - - paddle2onnx --model_dir ./ch_ppocr_server_v2.0_rec_infer/ch_ppocr_server_v2.0_rec_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_server_v2.0_rec_infer.onnx --opset_version 11 --enable_onnx_checker True - -##### 识别模型转onnx模型 - - paddle2onnx --model_dir ./ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_mobile_v2.0_cls_infer.onnx --opset_version 11 --enable_onnx_checker True - -## 二、onnx模型转om模型 - -### 检测模型 -#### AIPP配置文件 - aipp_op { - aipp_mode : static - related_input_rank : 0 - input_format : RGB888_U8 - csc_switch : false - rbuv_swap_switch : false - - mean_chn_0 :0 - mean_chn_1 :0 - mean_chn_2 :0 - - min_chn_0 : 123.675 - min_chn_1 : 116.28 - min_chn_2 : 103.53 - - var_reci_chn_0 : 0.017124753831664 - var_reci_chn_1 : 0.017507002801120 - var_reci_chn_2 : 0.017429193899782 - } -#### 转om模型命令 -##### 转多分辨率档位模型 - /usr/local/Ascend/ascend-toolkit/latest/atc/bin/atc --model=./ch_ppocr_server_v2.0_det_infer.onnx --framework=5 --output_type=FP32 --output=Dynamic24_ch_ppocr_server_v2.0_det_infer --input_format=NCHW --input_shape="x:1,3,-1,-1" --dynamic_image_size="768,1024;1024,768;864,1120;1120,864;960,1216;1216,960;1056,1312;1312,1056;1152,1408;1408,1152;1248,1504;1504,1248;1344,1600;1600,1344;1440,1696;1696,1440;1536,1792;1792,1536;1632,1888;1888,1632;1728,1984;1984,1728;1824,2080;2080,1824" --soc_version=Ascend310 --insert_op_conf=./det_aipp.cfg -注意:可修改`--dynamic_image_size`后的档位列表,来适应数据集图片的尺寸范围,有利于改善精度;该paddle模型的尺寸宽高需设置为32的倍数 - -### 方向分类模型 -#### AIPP配置文件 - aipp_op { - aipp_mode : static - related_input_rank : 0 - input_format : RGB888_U8 - csc_switch : false - rbuv_swap_switch : false - mean_chn_0 :0 - mean_chn_1 :0 - mean_chn_2 :0 - min_chn_0 :127.5 - min_chn_1 :127.5 - min_chn_2 :127.5 - var_reci_chn_0 : 0.00784313725490196 - var_reci_chn_1 : 0.00784313725490196 - var_reci_chn_2 : 0.00784313725490196 - } -#### 转om模型命令 - -##### 转动态batch模型 - /usr/local/Ascend/ascend-toolkit/latest/atc/bin/atc --model=./ch_ppocr_mobile_v2.0_cls_infer.onnx --framework=5 --output_type=FP32 --output=ch_ppocr_mobile_v2.0_cls_infer_3_48_192 --input_format=NCHW --input_shape="x:-1,3,48,192" --dynamic_batch_size="1,2,4,8" --soc_version=Ascend310 --insert_op_conf="cls_aipp.cfg" - -### 识别模型 -#### AIPP配置文件 - aipp_op { - aipp_mode : static - related_input_rank : 0 - input_format : RGB888_U8 - csc_switch : false - rbuv_swap_switch : false - mean_chn_0 :0 - mean_chn_1 :0 - mean_chn_2 :0 - min_chn_0 :127.5 - min_chn_1 :127.5 - min_chn_2 :127.5 - var_reci_chn_0 : 0.00784313725490196 - var_reci_chn_1 : 0.00784313725490196 - var_reci_chn_2 : 0.00784313725490196 - } - -#### 转om模型命令 -##### 转多batch模型 - /usr/local/Ascend/ascend-toolkit/latest/atc/bin/atc --model=./ch_ppocr_server_v2.0_rec_infer.onnx --framework=5 --output_type=FP32 --output=ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16 --input_format=NCHW --input_shape="x:-1,3,32,320" --dynamic_batch_size="1,2,4,8,16" --soc_version=Ascend310 --insert_op_conf="rec_aipp.cfg" -注意:如果转换模型出现 `Prebuild op[LSTM_4/DynamicRnn] failed`,需要使用CANN 5.0.2及更高的版本 -## 相关文档 -- [Paddle2ONNX](https://github.com/PaddlePaddle/Paddle2ONNX/blob/develop/README_zh.md " Paddle2ONNX") diff --git a/mxVision/GeneralTextRecognition/data/model/run.sh b/mxVision/GeneralTextRecognition/data/model/run.sh new file mode 100644 index 0000000000000000000000000000000000000000..c5c48f429fb6033243d2e6c2a63672f9919c713f --- /dev/null +++ b/mxVision/GeneralTextRecognition/data/model/run.sh @@ -0,0 +1,17 @@ +# 解压模型压缩包 +tar -xvf ch_ppocr_server_v2.0_det_infer.tar +tar -xvf ch_ppocr_mobile_v2.0_cls_infer.tar +tar -xvf ch_ppocr_server_v2.0_rec_infer.tar + +# 处理检测模型,1. 转onnx,2. 转多分辨率档位om模型 +paddle2onnx --model_dir ./ch_ppocr_server_v2.0_det_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_server_v2.0_det_infer.onnx --opset_version 11 --enable_onnx_checker True +atc --model=./ch_ppocr_server_v2.0_det_infer.onnx --framework=5 --output_type=FP32 --output=Dynamic24_ch_ppocr_server_v2.0_det_infer --input_format=NCHW --input_shape="x:1,3,-1,-1" --dynamic_image_size="768,1024;1024,768;864,1120;1120,864;960,1216;1216,960;1056,1312;1312,1056;1152,1408;1408,1152;1248,1504;1504,1248;1344,1600;1600,1344;1440,1696;1696,1440;1536,1792;1792,1536;1632,1888;1888,1632;1728,1984;1984,1728;1824,2080;2080,1824" --soc_version=Ascend310P3 --insert_op_conf=./det_aipp.cfg + +# 处理识别模型,1. 转onnx,2. 转动态batch om模型 +paddle2onnx --model_dir ./ch_ppocr_mobile_v2.0_cls_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_mobile_v2.0_cls_infer.onnx --opset_version 11 --enable_onnx_checker True +atc --model=./ch_ppocr_mobile_v2.0_cls_infer.onnx --framework=5 --output_type=FP32 --output=ch_ppocr_mobile_v2.0_cls_infer_3_48_192 --input_format=NCHW --input_shape="x:-1,3,48,192" --dynamic_batch_size="1,2,4,8" --soc_version=Ascend310P3 --insert_op_conf="cls_aipp.cfg" + +# 处理方向分类模型,1. 转onnx,2. 转多batch om模型 +paddle2onnx --model_dir ./ch_ppocr_server_v2.0_rec_infer --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file ./ch_ppocr_server_v2.0_rec_infer.onnx --opset_version 11 --enable_onnx_checker True +atc --model=./ch_ppocr_server_v2.0_rec_infer.onnx --framework=5 --output_type=FP32 --output=ch_ppocr_server_v2.0_rec_infer_3_32_320_bs_1_2_4_8_16 --input_format=NCHW --input_shape="x:-1,3,32,320" --dynamic_batch_size="1,2,4,8,16" --soc_version=Ascend310P3 --insert_op_conf="rec_aipp.cfg" + diff --git a/mxVision/GeneralTextRecognition/main_ocr.py b/mxVision/GeneralTextRecognition/python/main_ocr.py similarity index 96% rename from mxVision/GeneralTextRecognition/main_ocr.py rename to mxVision/GeneralTextRecognition/python/main_ocr.py index 7ea1013aebbab33fb25e4735b2f0943a300eb32f..efdb38ab5a8fc3764d048e851b2f739711635dc3 100644 --- a/mxVision/GeneralTextRecognition/main_ocr.py +++ b/mxVision/GeneralTextRecognition/python/main_ocr.py @@ -33,7 +33,7 @@ if __name__ == '__main__': exit() # create streams by pipeline config file - with open("./data/OCR.pipeline", 'rb') as f: + with open("../data/OCR.pipeline", 'rb') as f: pipelineStr = f.read() ret = streamManagerApi.CreateMultipleStreams(pipelineStr) if ret != 0: @@ -41,12 +41,11 @@ if __name__ == '__main__': exit() # Inputs data to a specified stream based on streamName. - - IN_PLUGIN_ID = 0 + IN_PLUGIN_ID = 0 # Construct the input of the stream dataInput = MxDataInput() - DIR_NAME = './input_data/' + DIR_NAME = '../input_data/' file_list = os.listdir(DIR_NAME) file_list.sort() diff --git a/mxVision/GeneralTextRecognition/src/README.md b/mxVision/GeneralTextRecognition/src/README.md deleted file mode 100644 index eac82d53dd726ea2de79208d8c220c49058c0beb..0000000000000000000000000000000000000000 --- a/mxVision/GeneralTextRecognition/src/README.md +++ /dev/null @@ -1,31 +0,0 @@ -## 第一步,编译clipper动态库 -- 在[网站](https://sourceforge.net/projects/polyclipping/files/)下载`clipper_ver6.4.2.zip`压缩包,解压后将路径cpp下的 `clipper.hpp、clipper.cpp` 到本地路径src/Clipper下 -- 在Clipper目录下新建并进入路径: - - `mkdir build` - - `cd build` -- 编译并安装: - - `cmake ..` - - `make -j` - - `make install` -- 可在根目录下的lib中看到编译生成的文件:`libclipper.so`。将lib的绝对路径加入系统环境变量: - - `vim ~/.bashrc` - - 将 ` export LD_LIBRARY_PATH=/lib:$LD_LIBRARY_PATH ` 加在最后一行(注意修改路径), 保存退出 - - `source ~/.bashrc` 使之生效。 - -## 第二步,编译后处理动态库DBPostProcess -- 进入src/DBPostProcess路径,进行类似第一步的编译流程,创建并进入路径: - - `mkdir build` - - `cd build` -- 编译安装: - - `cmake ..` - - `make -j` - - `make install` -- 可在src同级路径lib中看到 `libDBPostProcess.so`, 确保pipeline中元件`"mxpi_textobjectpostprocessor0"`的参数`"postProcessLibPath"`为该lib所在的绝对路径。 - -## 注意事项 -- DB后处理目前支持两种缩放方式:拉伸缩放`Resizer_Stretch`、 等比例缩放`Resizer_KeepAspectRatio_Fit`。 -- 需要确保编译生成的libclipper.so和libDBPostProcess.so文件权限不高于640, 如果文件权限不满足要求, -进入到libclipper.so和libDBPostProcess.so所在目录, 执行如下命令修改文件权限 -``` - chmod 640 libclipper.so libDBPostProcess.so -``` \ No newline at end of file