创建了一个虚拟环境:conda create -n orc python==3.11.7
进入虚拟环境后执行2条命令
bash
pip install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
安装好后,在网上找了一段识别图片的代码,文字识别的精度还挺高的
python
######################1.文字识别#########################
from paddleocr import PaddleOCR, draw_ocr
# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
ocr = PaddleOCR(use_angle_cls=True,# 设置使用方向分类器识别180度旋转文字,处理一些不是正放的文字
use_gpu=True,#使用gpu
lang="ch"#中文识别
)
img_path = 'orc1.jpg'
result = ocr.ocr(img_path, cls=True)#进行识别
for idx in range(len(result)):
res = result[idx]
for line in res:
print(line)
#######################2.生成目标检测的图片,用检测框框选文字#########################
# 显示结果
# 如果本地没有simfang.ttf,可以在doc/fonts目录下下载
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='Songti.ttc') #字体文件可引用路径,或复制到目录下
im_show = Image.fromarray(im_show)
im_show.save('resultx.jpg')
效果
各库版本
bash
(orc) mwj@mwjdeMac-mini orcBD % pip list
Package Version
---------------------- -----------
anyio 4.4.0
astor 0.8.1
attrdict 2.0.1
Babel 2.15.0
bce-python-sdk 0.9.17
beautifulsoup4 4.12.3
blinker 1.8.2
cachetools 5.3.3
certifi 2024.6.2
charset-normalizer 3.3.2
click 8.1.7
contourpy 1.2.1
cssselect 1.2.0
cssutils 2.11.1
cycler 0.12.1
Cython 3.0.10
decorator 5.1.1
et-xmlfile 1.1.0
fire 0.6.0
Flask 3.0.3
flask-babel 4.0.0
fonttools 4.53.0
future 1.0.0
h11 0.14.0
httpcore 1.0.5
httpx 0.27.0
idna 3.7
imageio 2.34.2
imgaug 0.4.0
itsdangerous 2.2.0
Jinja2 3.1.4
kiwisolver 1.4.5
lazy_loader 0.4
lmdb 1.5.1
lxml 5.2.2
MarkupSafe 2.1.5
matplotlib 3.9.0
more-itertools 10.3.0
networkx 3.3
numpy 1.26.0
opencv-contrib-python 4.6.0.66
opencv-python 4.6.0.66
opencv-python-headless 4.10.0.84
openpyxl 3.1.5
opt-einsum 3.3.0
packaging 24.1
paddleocr 2.7.3
paddlepaddle 2.6.1
pandas 2.2.2
pdf2docx 0.5.8
pillow 10.3.0
pip 24.0
premailer 3.10.0
protobuf 5.27.2
psutil 6.0.0
pyclipper 1.3.0.post5
pycryptodome 3.20.0
PyMuPDF 1.24.7
PyMuPDFb 1.24.6
pyparsing 3.1.2
python-dateutil 2.9.0.post0
python-docx 1.1.2
pytz 2024.1
PyYAML 6.0.1
rapidfuzz 3.9.3
rarfile 4.2
requests 2.32.3
scikit-image 0.24.0
scipy 1.14.0
setuptools 70.1.1
shapely 2.0.4
six 1.16.0
sniffio 1.3.1
soupsieve 2.5
termcolor 2.4.0
tifffile 2024.6.18
tqdm 4.66.4
typing_extensions 4.12.2
tzdata 2024.1
urllib3 2.2.2
visualdl 2.5.3
Werkzeug 3.0.3
wheel 0.43.0
(orc) mwj@mwjdeMac-mini orcBD %
经过测试在win环境下同样适用