简介
地址
- https://gitee.com/paddlepaddle/PaddleOCR
- 参考 https://blog.csdn.net/qq_52852432/article/details/131817619
安装测试
pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本 切换到目录https://gitee.com/paddlepaddle/PaddleOCR 安装依赖 python -m pip install -r requirements.txt
from paddleocr import PaddleOCR, draw_ocr
#执行报错 name 'predict_system' is not defined,需要修改predict_system的引用
# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
img_path = './ch.jpg'
result = ocr.ocr(img_path, cls=True)
for idx in range(len(result)):
res = result[idx]
for line in res:
print(line)
# 显示结果
from PIL import Image
result = result[0]
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='./simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
模型下载
- 应用模型

- 通用模型
地址 https://gitee.com/paddlepaddle/PaddleOCR/blob/main/doc/doc_ch/models_list.md

- 需要下载两个模型用来进行验证,分别为文本检测模型和文本识别模型,选择下载推理模型

- 模型下载之后为两个压缩包,在PaddleOCR-realase-2.6根目录下新建文件夹inference_model

- 将压缩包解压到该文件夹下,如下图所示。

- 测试
python tools/infer/predict_system.py --image_dir="test.jpg" --det_model_dir="./inference_model/ch_PP-OCRv3_det_infer/" --rec_model_dir="./inference_model/ch_PP-OCRv3_rec_infer"
训练集制作
打开终端进入PPOCRLabel
- python PPOCRLabel.py --lang ch

全部打标完成之后,点击文件选择导出标记结果,再点击文件选择导出识别结果,完成后再文件夹多出四个文件fileState,Label,rec_gt, crop_img。其中crop_img中的图片用来训练文字识别模型,fileState记录图片的打标完成与否,Label为训练文字检测模型的标签,rec_gt为训练文字识别模型的标签。
所有文件放入drivingData包括图片(不止那4个文件)
- 标注结果

- 在PaddleOCR根目录下建立train_data文件夹,并且将打标签生成的文件和图片放在该文件夹下。

- 切分数据
python gen_ocr_train_val_test.py --trainValTestRatio 6:2:2 --datasetRootPath ../train_data/drivingData
- 切分结果

训练
- 下载训练模型(非推理模型)
ch_ppocr_server_v2.0_rec ch_ppocr_server_v2.0_det
- PaddleOCR-release-2.6根目录下建立pretrain_models文件夹,并将训练模型解压至该文件夹下

- 选用检测配置文件

- 手动配置

- 训练检测模型
python tools/train.py -c configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml
- 测试
python tools/infer_det.py -c configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml -o Global.pretrained_model=output/ch_db_res18/latest.pdparams Global.infer_img="D:\code\PaddleOCR-main\PaddleOCR\test.jpg"
- 选用识别配置文件

- 训练
python tools/train.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_common_train_v2.0.yml
- 测试
python tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_common_train_v2.0.yml -o Global.pretrained_model=output/rec_chinese_common_v2.0/latest.pdparams Global.infer_img="D:\code\PaddleOCR-main\PaddleOCR\test.jpg"
- 转换成推理模型 其中Global.pretrained_model是我们训练好并且需要推理的模型,Global.save_inference_dir为所要保存推理模型的位置。推理模型是可以直接被调用进行识别和检测。分别把训练好的文字检测模型和文字识别模型推理
python tools/export_model.py -c "configs/rec/ch_ppocr_v2.0/rec_chinese_common_train_v2.0.yml" -o Global.pretrained_model="./output/rec_chinese_common_v2.0/latest.pdparams" Global.save_inference_dir="./inference_model/rec/"
python tools/export_model.py -c "./configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml" -o Global.pretrained_model="./output/ch_db_res18/latest.pdparams" Global.save_inference_dir="./inference_model/det/"
- 转换结果

- 其中det和rec即是我们的推理模型,可以用predict_system.py进行验证
python tools/infer/predict_system.py --image_dir="D:\code\PaddleOCR-main\PaddleOCR\test.jpg" --det_model_dir="./inference_model/det/" --rec_model_dir="./inference_model/rec"
应用
from paddleocr import PaddleOCR, draw_ocr
ocr = PaddleOCR(use_angle_cls=True, rec_model_dir='./inference_model/rec',
det_model_dir='./inference_model/det')
result = ocr.ocr(frame, cls=True)
result = result[0]
texts = [line[1][0] for line in result]