版本python3.12,安装以下包
pip3 install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install setuptools -i https://pypi.tuna.tsinghua.edu.cn/simple
下载并安装仿宋体,用于显示结果图片。
帮助文档:https://paddlepaddle.github.io/PaddleOCR/latest/quick_start.html
from paddleocr import PaddleOCR, draw_ocr
import re
ocr = PaddleOCR(use_angle_cls=False, lang="ch", show_log=False)
img_path = 'b.jpg'
result = ocr.ocr(img_path, cls=False)
name=''
num=''
_num=''
for idx in range(len(result)):
res = result[idx]
for i,line in enumerate(res):
print(i,line[1][0])
if i < 1:
name = line[1][0][2:]
if i ==1 and len(name) < 1:
name=line[1][0]
_num = re.findall("""^[0-9Xx]w{17}$""",line[1][0])
if len(_num)>0:
num=_num
if num !='':
print(name,num[0])
else:
print('number in error')
'''
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='SimHei.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
'''