第1步,安装环境
安装paddlepaddle-gpu
安装CUDA 10.0
对应的飞桨2.0.0,GPU版本:
# 创建虚拟环境
conda create -n paddle_env python=3.7
# 进入虚拟环境
activate paddle_env
# 安装paddlepaddle-gpu
python -m pip install paddlepaddle-gpu==2.0.0rc1.post100 -f https://paddlepaddle.org.cn/whl/stable.html
查看详细安装步骤
下载PaddleOCR
进入GitHub下载PaddleOCR
,并解压在指定目录中,如PaddleOCR-dygraph
。
第2步,下载推理模型
模型简介 | 模型名称 | 推荐场景 | 检测模型 | 方向分类器 | 识别模型 |
中英文超轻量OCR模型(9.4M) | ch_ppocr_mobile_v2.0_xx | 移动端&服务器端 | |||
中英文通用OCR模型(143.4M) | ch_ppocr_server_v2.0_xx | 服务器端 |
下载上述模型列表中的某一个,如中英文超轻量OCR模型,将其对应的检测模型、方向分类器、识别模型的推理模型,并将其解压至PaddleOCR-dygraph\inference
目录下,Windows用户解压上述文件时会出现问题,建议不要使用解压软件解压,使用命令行解压,:
mkdir inference && cd inference
# 解压超轻量级中文OCR模型的检测模型
tar xf ch_ppocr_mobile_v2.0_det_infer.tar
# 解压超轻量级中文OCR模型的识别模型
tar xf ch_ppocr_mobile_v2.0_rec_infer.tar
# 解压超轻量级中文OCR模型的文本方向分类器模型
tar xf ch_ppocr_mobile_v2.0_cls_infer.tar
cd ..
解压完毕后应有如下文件结构:
├── ch_ppocr_mobile_v2.0_cls_infer
│ ├── inference.pdiparams
│ ├── inference.pdiparams.info
│ └── inference.pdmodel
├── ch_ppocr_mobile_v2.0_det_infer
│ ├── inference.pdiparams
│ ├── inference.pdiparams.info
│ └── inference.pdmodel
├── ch_ppocr_mobile_v2.0_rec_infer
├── inference.pdiparams
├── inference.pdiparams.info
└── inference.pdmodel
第3步,预测
以下代码实现了文本检测、方向分类器和识别串联推理,在执行预测时,需要通过参数image_dir
指定单张图像或者图像集合的路径、参数det_model_dir
指定检测inference
模型的路径、参数rec_model_dir
指定识别inference
模型的路径、参数use_angle_cls
指定是否使用方向分类器、参数cls_model_dir
指定方向分类器inference
模型的路径、参数use_space_char
指定是否预测空格字符。可视化识别结果默认保存到./inference_results
文件夹里面。
# 预测image_dir指定的单张图像
python tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer" --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer" --use_angle_cls=True --use_space_char=True
# 预测image_dir指定的图像集合
python tools/infer/predict_system.py --image_dir="./doc/imgs/" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/" --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True
# 如果想使用CPU进行预测,需设置use_gpu参数为False
python tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_mobile_v2.0_det_infer/" --rec_model_dir="./inference/ch_ppocr_mobile_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True --use_gpu=False
- 通用中文OCR模型
请按照上述步骤下载相应的模型,并且更新相关的参数,示例如下:
# 预测image_dir指定的单张图像
python tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_ppocr_server_v2.0_det_infer/" --rec_model_dir="./inference/ch_ppocr_server_v2.0_rec_infer/" --cls_model_dir="./inference/ch_ppocr_mobile_v2.0_cls_infer/" --use_angle_cls=True --use_space_char=True
- 注意:
- 如果希望使用不支持空格的识别模型,在预测的时候需要注意:请将代码更新到最新版本,并添加参数
--use_space_char=False
。 - 如果不希望使用方向分类器,在预测的时候需要注意:请将代码更新到最新版本,并添加参数
--use_angle_cls=False
。