PaddleOCR安装使用
1. 安装PaddlePaddle
在pyCharm中新建项目PaddleOCR,并在该项目文件下添加python 3.7版本的Interpreter。
在官网中根据自己的电脑配置选择安装的版本,我这里安装的是GPU版本
python -m pip install paddlepaddle-gpu==2.1.0.post101 -f https://paddlepaddle.org.cn/whl/mkl/stable.html
2 安装shapely包
官方强调windows环境下,建议从这里下载shapely安装包完成安装, 直接通过pip安装的shapely库可能出现[winRrror 126] 找不到指定模块的问题。
2.1 下载shapely安装包
打开网址,根据电脑64位和python3.7的版本选择下载Shapely‑1.7.1‑cp37‑cp37m‑win_amd64.whl
2.2 安装shapely包
将下载下来的shapely whl安装包放到我们的PaddleOCR文件夹下,在Terminal命令框中执行命令:
pip install Shapely-1.7.1-cp37-cp37m-win_amd64.whl
3 下载项目代码及模型
3.1 下载项目代码
从GitHub上将项目代码下载下来,同时下载需要的第三方库
pip install -r requirements.txt
3.2 下载inference模型等
将中英文超轻量OCR模型中的检测模型、方向分类器、识别模型的推理模型下载下来,并解压至项目文件中的inference文件夹下。
值得注意的是,这三个文件解压完毕后应有如下文件结构:
├── 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
cls和det这两个文件我都可以正常解压,但是ch_ppocr_mobile_v2.0_rec_infer文件解压后却没有出现上面3个文件,尝试了使用Git进行解压也没有得到正常的文件,最终还是用Mac电脑解压后就正常了,不知道是什么原因。
不过后续运行时发现,如果模型路径下没有找到模型文件,会自动下载。
4 安装paddleocr
pip install "paddleocr>=2.0.1"
5 运行程序
按照官网提示,运行下方代码即可预测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
最终看了这位博主的文章【PaddleOcr系列】之一 安装与使用全程指南,找到了别的方法,在项目文件中新建了ocr.py,简单测试一下效果。
from paddleocr import PaddleOCR, draw_ocr
import cv2
# 模型路径下必须含有model和params文件,如果没有,现在可以自动下载了,不过是最简单的模型
# use_gpu 如果paddle是GPU版本请设置为 True
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
img_path = 'H:/PaddleOCR/doc/imgs/11.jpg' # 这个是自己的图片,可以根据情况自行修改
result = ocr.ocr(img_path, cls=True)
# print结果
for line in result:
print(line)
img=cv2.imread(img_path)
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
# 显示图片结果
img = draw_ocr(img, boxes, txts, scores)
cv2.imshow("result",img)
cv2.waitKey(0)
运行成功~
输出结果的结构包括两部分:每个文本识别结果,都包括四个点的坐标组成的数组和识别结果的元组(识别的文字结果,置信度)。