克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

PaddleOCR

一、环境搭建

# 1. 安装PaddleOCR模型
%cd ~/Project
!git clone -b release/2.1 https://github.com/PaddlePaddle/PaddleOCR.git
# 准备超轻量级检测模型inference model
! mkdir inference
# 下载超轻量级中文OCR模型的检测模型并解压
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar && tar xf ch_ppocr_mobile_v2.0_det_infer.tar && rm ch_ppocr_mobile_v2.0_det_infer.tar
# 下载超轻量级中文OCR模型的识别模型并解压
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar && tar xf ch_ppocr_mobile_v2.0_rec_infer.tar && rm ch_ppocr_mobile_v2.0_rec_infer.tar
# 下载超轻量级中文OCR模型的文本方向分类器模型并解压
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar && tar xf ch_ppocr_mobile_v2.0_cls_infer.tar && rm ch_ppocr_mobile_v2.0_cls_infer.tar
# 2. 下载超轻量级中文OCR模型
%cd ~/Project/PaddleOCR
!mkdir inference && cd inference
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar && tar xf ch_ppocr_mobile_v2.0_det_infer.tar && rm ch_ppocr_mobile_v2.0_det_infer.tar
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar && tar xf ch_ppocr_mobile_v2.0_rec_infer.tar && rm ch_ppocr_mobile_v2.0_rec_infer.tar
! cd inference && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar && tar xf ch_ppocr_mobile_v2.0_cls_infer.tar && rm ch_ppocr_mobile_v2.0_cls_infer.tar
# 3. 安装依赖库
%cd ~/Project/PaddleOCR
!pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
# 4. 快速预测单张图像
# 下面开始调用tools/infer/predict_system.py 完成图像文本识别,共需要传入三个参数:
# - image_dir: 指定要测试的图像
# - det_model_dir: 指定轻量检测模型的inference model
# - rec_model_dir: 指定轻量识别模型的inference model
# - cls_model_dir: 指定轻量方向分类器模型的inference model
# 快速运行,识别结果会可视化在图像中并保存在./inference_results文件夹下
%cd ~/Project/PaddleOCR
import matplotlib.pyplot as plt
from PIL import Image
%pylab inline
def show_img(img_path,figsize=(10,10)):
    img = Image.open(img_path)
    plt.figure("test_img", figsize=figsize)
    plt.imshow(img)
    plt.show()
show_img("./doc/imgs/french_0.jpg")
!python3 tools/infer/predict_system.py --image_dir="./doc/imgs/french_0.jpg" \
--det_model_dir="/home/aistudio/Project/PaddleOCR/inference/ch_ppocr_mobile_v2.0_det_infer"  \
--rec_model_dir="/home/aistudio/Project/PaddleOCR/inference/ch_ppocr_mobile_v2.0_rec_infer" \
--cls_model_dir="/home/aistudio/Project/PaddleOCR/inference/ch_ppocr_mobile_v2.0_cls_infer"

二、【检测框】模型训练

数据集以及配置文件的准备

# 1. 下载MobileNetV3\ResNet50的预训练模型
%cd ~/Project/PaddleOCR
!wget -P ./pretrain_models/ https://paddle-imagenet-models-name.bj.bcebos.com/MobileNetV3_large_x0_5_pretrained.tar
! cd pretrain_models/ && tar xf MobileNetV3_large_x0_5_pretrained.tar
!wget -P ./pretrain_models/ https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_vd_ssld_pretrained.tar
! cd pretrain_models/ && tar xf ResNet50_vd_ssld_pretrained.tar
# 2. 训练backbone为MobileNetV3的db算法的检测模型[检测框的训练]
%cd ~/Project
!python3 PaddleOCR/tools/train.py -c PaddleOCR/configs/det/det_mv3_db.yml -o \
Global.epoch_num=1200\
Global.eval_batch_step="[0,500]" \
Global.save_epoch_step=10 \
Global.save_model_dir="./output/Tyre_Defects_detection/det"\
Global.save_res_path="./output/Tyre_Defects_detection/det/predicts_db.txt"\
Train.dataset.data_dir='./train_data/Tyre_Defects_detection/' \
Train.dataset.label_file_list=['./train_data/Tyre_Defects_detection/train_label.txt'] \
Eval.dataset.data_dir='./train_data/Tyre_Defects_detection/' \
Eval.dataset.label_file_list=['./train_data/Tyre_Defects_detection/test_label.txt']
# 3. 模型检测
%cd ~/Project
show_img("./test/test2.jpg")
!python3 PaddleOCR/tools/infer_det.py -c PaddleOCR/configs/det/det_mv3_db.yml -o \
Global.infer_img="./test/test2.jpg" \
Global.checkpoints="./output/Tyre_Defects_detection/det/latest"

三、【文字】模型训练

数据集以及配置文件的准备

# 1. 下载Rec_Mv3的预训练模型
!wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/rec_mv3_none_bilstm_ctc.tar
!cd pretrain_models && tar -xf rec_mv3_none_bilstm_ctc.tar && rm -rf rec_mv3_none_bilstm_ctc.tar
# 2. 训练文字检测模型
%cd ~/Project
!python3 PaddleOCR/tools/train.py -c PaddleOCR/configs/rec/rec_icdar15_train.yml -o \
Global.eval_batch_step="[0,500]" \
Global.save_epoch_step=10 \
Global.save_model_dir='./output/Tyre_Defects_detection/rec/'\
Train.dataset.data_dir='./train_data/Tyre_Defects_detection' \
Train.dataset.label_file_list=['./train_data/Tyre_Defects_detection/rec_gt_train.txt'] \
Eval.dataset.data_dir='./train_data/Tyre_Defects_detection' \
Eval.dataset.label_file_list=['./train_data/Tyre_Defects_detection/rec_gt_test.txt'] \
Optimizer.lr.learning_rate=0.0001
# 3. 模型检测
%cd ~/Project
show_img("./test/test1.jpg")
!python3 PaddleOCR/tools/infer_rec.py -c PaddleOCR/configs/rec/rec_icdar15_train.yml -o \
Global.infer_img="./test/test1.jpg" \
Global.checkpoints="./output/Tyre_Defects_detection/rec/latest" 
MIT License Copyright (c) 2022 GuanXiang_AI Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Paddle OCR特性: 超轻量级中文OCR,总模型仅8.6M 单模型支持中英文数字组合识别、竖排文本识别、长文本识别 检测模型DB(4.1M)+识别模型CRNN(4.5M) 多种文本检测训练算法,EAST、DB 多种文本识别训练算法,Rosetta、CRNN、STAR-Net、RARE 展开 收起
Python 等 5 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化