- 依赖库,保存为requirements.txt
altgraph==0.17
astor==0.8.1
chardet==3.0.4
click==7.1.2
cycler==0.10.0
decorator==4.4.2
funcsigs==1.0.2
future==0.18.2
gast==0.3.3
graphviz==0.14.2
idna==2.10
imageio==2.9.0
imgaug==0.4.0
joblib==0.17.0
kiwisolver==1.2.0
lmdb==1.0.0
matplotlib==3.3.2
networkx==2.5
nltk==3.5
np-utils==0.5.12.1
numpy==1.19.2
objgraph==3.4.1
opencv-python==4.2.0.32
paddleocr==1.0.0
paddlepaddle==1.8.5
pathlib==1.0.1
pefile==2019.4.18
Pillow==7.1.2
prettytable==1.0.1
protobuf==3.13.0
py-notifier==0.1.0
pyclipper==1.2.0
pyinstaller==4.0
pyinstaller-hooks-contrib==2020.9
pyparsing==2.4.7
pyperclip==1.8.0
pypiwin32==223
PyQt5==5.14.2
PyQt5-sip==12.7.2
pytesseract==0.3.4
python-dateutil==2.8.1
PyWavelets==1.1.1
pywin32==228
pywin32-ctypes==0.2.0
PyYAML==5.3.1
rarfile==4.0
regex==2020.10.11
requests==2.24.0
scikit-image==0.17.2
scipy==1.5.2
six==1.15.0
system-hotkey==1.0.3
tifffile==2020.10.1
tqdm==4.50.2
urllib3==1.25.10
wcwidth==0.2.5
wincertstore==0.2
flask==1.1.2
- conda构建虚拟环境
conda create -n pppocr python=3.7
conda activate pppocr
cd %~dp0
%~d0
pip install -r requirements.txt
3.打包主文件ocr.py
#!/usr/bin/env python
# coding=utf-8
# 文件上传服务器端,只考虑文件在当前目录下
from paddleocr import PaddleOCR
from os.path import join, dirname, exists, splitext
from flask import Flask, request
import os
import uuid
from configparser import ConfigParser
import re
# web服务
app = Flask(__name__)
# paddlerocr对象
pocr = None
# 配置文件
config = {}
def init():
global pocr
global config
conf = ConfigParser()
confpath = os.path.join(os.getcwd(), "config.ini")
conf.read(confpath, encoding="utf-8-sig")
for sec in conf.sections():
temp = dict(conf[sec])
for item in temp:
if temp[item].lower() == "true":
temp[item] = True
elif temp[item].lower() == "false":
temp[item] = False
elif temp[item].lower() == "none":
temp[item] = None
elif re.search("^\d+\.\d+$",temp[item].lower()):
temp[item] = float(temp[item])
elif re.search("^[\d]+$",temp[item].lower()):
temp[item] = int(temp[item])
config[sec] = temp
print(config["main"])
pocr = PaddleOCR(**config["main"])
# 上传文件
@app.route('/ocr', methods=['POST'])
def upload_file():
global pocr
global config
f = request.files.get('img')
if f is None:
# 表示没有发送文件
return "未上传文件"
# 设置保存路径
save_father_path = 'images'
img_path = os.path.join(save_father_path, str(
uuid.uuid1()) + splitext(f.filename)[-1])
if not os.path.exists(save_father_path):
os.makedirs(save_father_path)
f.save(img_path)
print(img_path)
res = pocr.ocr(img_path, **config["ocr"])
os.remove(img_path)
return str(res)
if __name__ == '__main__':
init()
# 启动服务,并指定端口号
app.run(**config["web"])
- 配置文件config.ini,对应的方法参数体。
[main]
det_model_dir = model/det
rec_model_dir = model/rec/ch
cls_model_dir = model/cls
rec_char_dict_path = model/ppocr_keys_v1.txt
use_gpu = False
[ocr]
det = True
rec = True
cls = False
[web]
port = 8080
host = 127.0.0.1
debug = None
load_dotenv = True
-
模型放置在model下(必须:ppocr_keys_v1.txt是从paddleocr中提取出来的,其他模型缺失可以自动下载)。结构如下:
PyInstaller Hook文件hook-ctypes.macholib.py
from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('prettytable')
- 打包脚本,这里的G:\Miniconda3\envs\pppocr替换为你实际pppocr环境地址。打包完成放置在dist。
conda activate pppocr
pyinstaller -D -y --clean --exclude matplotlib -p G:\Miniconda3\envs\pppocr\Lib\site-packages\paddleocr;G:\Miniconda3\envs\pppocr\Lib\site-packages\paddle\libs ocr.py -i scr2txt.ico --add-binary G:\Miniconda3\envs\pppocr\Lib\site-packages\paddle\libs;. --add-data .\model;.\model --add-data .\config.ini;.\ --additional-hooks-dir=.
-
整体目录结构:
运行效果:
总结:
1、CPU模式下启动需要加载模型,时间超长,出去晃两圈再回来!!没有8G别跑了。
2、PaddleOcr的识别率没话说,识别速度稍慢,有好的GPU显卡当我没说;
3、纯离线识别,作为一个web服务在内网跑,可以解决特定场景问题;
4、出发点是为UiBot内网识别做的一个补充。
下载地址:
链接:https://pan.baidu.com/s/13Lf5X6hM9w3gXfkgPal07A
提取码:2h3c