找人做软件网站,西宁网站建设学校,一般做网站图是多大的像素,个人建站建设PaddleOCR 算是OCR算法里面较好用的#xff0c;支持的内容多#xff0c;而且社区维护的好(手把手教你#xff0c;生怕你学不会)#xff0c;因此在国内常采用。目前已经更新到 2.8版本了#xff0c;功能更加丰富、强大#xff1b;目前支持通用OCR、表格识别、图片信息提取…PaddleOCR 算是OCR算法里面较好用的支持的内容多而且社区维护的好(手把手教你生怕你学不会)因此在国内常采用。目前已经更新到 2.8版本了功能更加丰富、强大目前支持通用OCR、表格识别、图片信息提取以及文档场景信息提取基本覆盖了常用的场景
首先下载模型代码 https://github.com/PaddlePaddle/PaddleOCR 需要视频课程的可以看 https://aistudio.baidu.com/education/group/info/25207
参考资料https://github.com/PaddlePaddle/PaddleOCR/blob/main/doc/doc_ch/models_list.md 1、安装环境
首先PaddleOCR处理模型训练需要环境数据打标工具PPOCRLabel也需要配置因此一并配置好 conda 环境 paddle_ocr
创建conda环境 paddle_ocr可以参考 doc/doc_ch/environment.md、installation.md 文件
# 推荐环境
PaddlePaddle 2.1.2
Python 3.7
CUDA10.1 / CUDA10.2
CUDNN 7.6# 常规创建 conda环境
conda create -n paddle_ocr python3.7 # python 版本可自定义*# 使用清华源加速下载
conda create --name paddle_ocr python3.8 --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/# 激活paddle_ocr环境
conda activate paddle_ocr# 安装paddle等库
pip3 install --upgrade pip#如果您的机器安装的是CUDA9或CUDA10请运行以下命令安装
python3 -m pip install paddlepaddle-gpu2.0.0 -i https://mirror.baidu.com/pypi/simple#如果您的机器是CPU请运行以下命令安装
python3 -m pip install paddlepaddle2.0.0 -i https://mirror.baidu.com/pypi/simple更多的版本需求请参照[安装文档](https://www.paddlepaddle.org.cn/install/quick)中的说明进行操作。# 安装标注软件 PPOCRLabel
pip install PPOCRLabel2、数据标注
最新版的PaddleOCR内部不再包含PPOCRLabel可以参考 https://github.com/ViatorSun/PPOCRLabel
将conda环境切换成 paddle_ocr然后在终端输入 PPOCRLabel --lang ch 即可启动 PPOCRLabel 标注工具 --lang 设置界面语言默认英文仅支持中英文中文ch、英文en如下图所示 PPOCRLabel 支持预标注选择 PaddleOCR - 选择模型可以选择待识别文字的语言如中英文、英文、法语等左下角的自动标注可以帮助将图片中的文字用 PaddleOCR模型检测并标注下预标注的效果一般比较差但是可以节省拉框的时间还是有一定作用的标注过程中会有几个txt文件用于保存数据Cache.cach 用于保存预标注数据fileState.txt记录图片标注状态Label.txt 为导出的标记结果rec_gt.txt 和 crop_img 为导出的识别结果
# 修改 fileState.txt 文件免去一张一张确认
import os
import os.path as ospshuffixes [.jpg, .jpeg, .png]
file_path /media/sun/Data/Dataset/OCR_Data/ocr_dataif osp.exists(file_path):with open(osp.join(file_path,fileState.txt), w, encodingutf-8) as f:for file in os.listdir(file_path):if osp.splitext(file)[-1].lower() in shuffixes:img_dir osp.join(file_path, file)f.write(img_dir \t1\n)print(fsave {img_dir} \t1)对于预标注的内容未必全部适合项目需求因此可以通过正则化将预标注内容筛选降低手动删除目标框的低效
import re
import json
import os.path as ospLabel_dir /media/sun/DataYZ/DataSet/led_Digital/Label.txtlabel_lst []
if osp.exists(Label_dir):with open(Label_dir, r, encodingutf-8) as f:for line in f.readlines():print(line)save_data []data_list json.loads(line.split(\t)[-1].replace(\n, ))for data in data_list:if bool(re.match(r^[0-9.]$, data[transcription])): # 只保留数字和小数点 的识别结果save_data.append(data)save_label line.split(\t)[0] \t str(save_data) \nlabel_lst.append(save_label)# 重写
with open(Label_dir, w, encodingutf-8) as f:for data in label_lst:f.write(data)注意 发现标注工具有一个隐藏小问题(谈不上bug)可能跟后续的识别有关 当标注框的高与宽的比例大于 1.5则进行 90度旋转默认顺时针旋转如果想避免这种情况将红框两行代码注释掉就可以了 标注完所有的数据之后需要划分数据集可以采用PaddeOCR/PPOCRLabel/gen_ocr_train_val_test.py 脚本进行处理–trainValTestRatio 设置三个数据集的比例–datasetRootPath设置标注好的数据集路径–detRootPath 生成的检测数据集路径–recRootPath 生成识别数据集路径 即可。注意标注文件中的框只能是四个点坐标过多过少都会无法识别
至此数据处理完成 3、模型训练
PaddleOCR 识别分为两个过程目标区域的检测 det 以及 对目标区域的内容识别 rec
训练脚本如下配置好 yaml 文件即可直接训练此处的 data/ 路径是博主自己创建的避免与官方提供的 config/ 下的yaml 文件冲突可将需要的 config/*.yaml 拷贝到 ./data 路径下
import os# detect 模型检测
command python3 tools/train.py -c data/det_r50_vd_db.yml
print(command)
os.system(command)# rec 模型识别
command python tools/train.py -c data/ch_PP-OCRv3_rec.yml
print(command)
os.system(command)det.yaml 文件与 rec.yaml 几乎一致 其中 rec.yml 的 save_model_dir 为模型训练保存路径pretrained_model 为预训练模型权重save_inference_dir 为模型导出时的输出路径character_dict_path 为字符词典路径其余的就是 Train Eval 数据集的路径需要修改 在生成的 模型训练保存路径中有 train.log 、config.yaml 、best_model 和其他中间epoch 权重 导出推理模型
训练过程中保存的模型权重 path.pdopt优化器状态 path.states训练中间信息 path.pdparams保存的模型的参数 导出的推理模型格式为 inference.pdmodel、inference.pdiparams其中 Global.save_inference_dir 不需要重复设置 *yaml 文件中已经包含了如果 *.yaml 中已经设置过了模型转换就无需再赋值当然赋值了会以重新赋值的参数运行 注意 此处有一点需要注意的就是 Global.pretrained_model 必须从新赋值因为 *yaml 文件中的 pretrained_model 为训练时的预训练权重此处为待导出的模型权重路径 还有就是权重路径是 ./runs/20240703/det_r50_vd/best_model/model.pdopt/pdparams 省去后缀的路径而不是文件夹的路径因此必须填写到权重名 import os# det
os.system(fpython tools/export_model.py f-c data/det_r50_vd_db.yml f-o Global.pretrained_model./runs/20240703/det_r50_vd/best_model/model # 待转化权重fGlobal.load_static_weightsFalse # fGlobal.save_inference_dir./inference/20240704/db_r50/ )# ocr
os.system(fpython3 tools/export_model.py f-c data/ch_PP-OCRv3_rec.yml f-o Global.pretrained_model./runs/20240703/rec_ppocr/best_model/model ) # 待转化权重# fGlobal.save_inference_dir./inference/20240704/rec_ppocr/ )模型推理
最后是模型推理此处可以使用如下脚本预测生成的结果在 inference_results 路径下
import os
import os.path as ospif __name__ __main__:img_path ./testdet_model_dir ./inference/20240710/det_inferrec_model_dir ./inference/20240710/rec_inferfor i, file in enumerate(os.listdir(img_path)):img osp.join(img_path, file)command fpython3 tools/infer/predict_system.py --image_dir{img} --det_model_dir{det_model_dir} --rec_model_dir{rec_model_dir} --use_angle_clsfalseprint(command)os.system(command) 文章转载自: http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.gryzk.cn.gov.cn.gryzk.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.wklmj.cn.gov.cn.wklmj.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.jjzxn.cn.gov.cn.jjzxn.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.ldynr.cn.gov.cn.ldynr.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn