安装参考:https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_ch/installation.md
1.安装cpu版本paddle
python -m pip install paddlepaddle==2.0.0rc1 -i https://mirror.baidu.com/pypi/simple
2.用码云下载paddleocr项目并解压
https://gitee.com/paddlepaddle/PaddleOCR
3.安装第三方库
pip3 install -r requirements.txt
4.模型下载
https://github.com/PaddlePaddle/PaddleOCR/blob/dygraph/doc/doc_ch/quickstart.md
轻量级模型的模型文件似乎缺少东西,使用通用模型,下载画红线的三个文件并解压,组合成如下文件结构(忽略mobile_det和mobile_rec,轻量模型)。
det:检测模型。
cls:方向分类器。
rec:识别模型。
G:\github\\PaddleOCR>
python tools/infer/predict_system.py
--image_dir="test"
--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_gpu=False
注释:
python tools/infer/predict_system.py
–image_dir=“test” #图片或所在文件夹
–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_gpu=False
cmd输出:
[2021/01/02 19:09:44] root INFO: dt_boxes num : 39, elapse : 24.176945447921753
[2021/01/02 19:09:57] root INFO: cls num : 39, elapse : 6.6749467849731445
[2021/01/02 19:10:09] root INFO: rec_res num : 39, elapse : 11.44210934638977
[2021/01/02 19:10:11] root INFO: Predict time of yu_test\1.jpeg: 51.551s
[2021/01/02 19:10:13] root INFO: 山东省医, 0.997
[2021/01/02 19:10:13] root INFO: (电子), 1.000
[2021/01/02 19:10:17] root INFO: 据代码:37060120, 0.992
[2021/01/02 19:10:17] root INFO: 票据号码:1500175912, 0.942
[2021/01/02 19:10:19] root INFO: 交款人统一社会信用代码:, 0.959
[2021/01/02 19:10:19] root INFO: 校验码:, 0.936
[2021/01/02 19:10:19] root INFO: d141be, 0.996
[2021/01/02 19:10:19] root INFO: 文款人:沈俊宇, 0.879
[2021/01/02 19:10:21] root INFO: 数量/单位, 0.944
[2021/01/02 19:10:24] root INFO: 金额(元), 0.770
[2021/01/02 19:10:24] root INFO: 项目名称, 0.973
[2021/01/02 19:10:24] root INFO: 务注, 0.674
[2021/01/02 19:10:24] root INFO: 项目名称, 0.756
[2021/01/02 19:10:24] root INFO: 数量/单位金额(元)各注, 0.879
[2021/01/02 19:10:24] root INFO: 箱:0, 0.617
[2021/01/02 19:10:24] root INFO: 出查体费, 0.824
[2021/01/02 19:10:24] root INFO: 1.00, 0.903
[2021/01/02 19:10:24] root INFO: 40.00, 0.991
[2021/01/02 19:10:24] root INFO: 金合计(大写)肆拾元整, 0.961
[2021/01/02 19:10:24] root INFO: (小写)40.00, 0.955
[2021/01/02 19:10:24] root INFO: 业务流水号:ME182111, 0.929
[2021/01/02 19:10:24] root INFO: 门诊号:212020, 0.999
[2021/01/02 19:10:24] root INFO: 或诊日期:20201112, 0.941
[2021/01/02 19:10:24] root INFO: 医保类型:全自费, 0.934
[2021/01/02 19:10:24] root INFO: 医疗机构类型:保健院, 0.929
[2021/01/02 19:10:24] root INFO: 医保编号:, 0.972
[2021/01/02 19:10:24] root INFO: 性别:男, 0.913
[2021/01/02 19:10:25] root INFO: 其他支付:0.00, 0.971
[2021/01/02 19:10:25] root INFO: 个人账户支付:0.00, 0.997
[2021/01/02 19:10:25] root INFO: 医保统等基全支付:0.00, 0.917
[2021/01/02 19:10:25] root INFO: 个人现金支付:40.00, 0.988
[2021/01/02 19:10:25] root INFO: 信个人自付:0.00, 0.966
[2021/01/02 19:10:25] root INFO: 自费:0.00, 0.988
[2021/01/02 19:10:25] root INFO: 海报新间, 0.926
[2021/01/02 19:10:25] root INFO: 报业, 0.999
[2021/01/02 19:11:26] root INFO: The visualized image saved in ./inference_result
s/1.jpeg
可视化识别结果默认保存在./inference_results文件夹里面
#coding=utf-8
import os
from cv2 import *
img_path="./image_test"
det_model="./inference/ch_ppocr_server_v2.0_det_infer/"
cls_model="./inference/ch_ppocr_mobile_v2.0_cls_infer/"
rec_model="./inference/ch_ppocr_server_v2.0_rec_infer/"
shell="python tools/infer/predict_system.py"+" "
shell+="--det_model_dir="+"\""+det_model+"\""+" "
shell+="--cls_model_dir="+"\""+cls_model+"\""+" "
shell+="--rec_model_dir="+"\""+rec_model+"\""+" "
shell+="--use_angle_cls=True"+" "
shell+="--use_space_char=True"+" "
shell+="--use_gpu=False"+" "
shell+="--image_dir="+"\""+img_path+"\""
if __name__=="__main__":
#paddleOCR检测,将检测结果写入文件
os.chdir("./PaddleOCR")
with os.popen(shell) as out:
text=out.read()
print(text)
with open("../result.txt","w") as f:
f.write(text)
#读取结果图片
os.chdir("./inference_results")
namedWindow("result",0)
for root,dirs,files in os.walk("./"):
for f in files:
imshow("result",imread(f,-1))
waitKey(0)
destroyAllWindows()