PaddleOCR是百度飞桨开发的OCR库
安装PaddleOCR,只需要两个命令:
pip install paddlepaddle==2.4.2
pip install paddleocr
PaddleOCR的使用也很简单:
from paddleocr import PaddleOCR
# use_angle_cls:是否使用角度分类
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
img_path = './test.png'
result = ocr.ocr(img_path, cls=True)
for res in result:
for line in res:
print(line)
测试图片:
输出:
[[[34.0, 12.0], [351.0, 12.0], [351.0, 53.0], [34.0, 53.0]], ('二、优惠券活动', 0.9964595437049866)]
[[[124.0, 75.0], [1644.0, 75.0], [1644.0, 114.0], [124.0, 114.0]], ('优惠券获得活动为皮筋枪射击活动。有7个靶子,共7次射击机会,每射', 0.9964429140090942)]
[[[127.0, 134.0], [1080.0, 134.0], [1080.0, 174.0], [127.0, 174.0]], ('中一个优惠券面额增大0.1元(直接免费好吧)', 0.9746130108833313)]
[[[36.0, 197.0], [443.0, 197.0], [443.0, 233.0], [36.0, 233.0]], ('三、个人商品优惠券', 0.9967468976974487)]
[[[108.0, 255.0], [777.0, 255.0], [777.0, 294.0], [108.0, 294.0]], ('a)个人商品优惠券有以下面额:', 0.9874213933944702)]
[[[141.0, 313.0], [284.0, 313.0], [284.0, 363.0], [141.0, 363.0]], ('i.', 0.9226597547531128)]
[[[281.0, 318.0], [1453.0, 318.0], [1453.0, 357.0], [281.0, 357.0]], ('无门槛减0.1元,满50元立减1元,满200元立减10元', 0.9977045655250549)]
[[[127.0, 378.0], [196.0, 378.0], [196.0, 419.0], [127.0, 419.0]], ('ii.', 0.9997437596321106)]
[[[288.0, 380.0], [1455.0, 380.0], [1455.0, 419.0], [288.0, 419.0]], ('无门槛减0.2元,满50元立减2元,满200元立减20元', 0.9980664253234863)]
[[[105.0, 441.0], [189.0, 441.0], [189.0, 480.0], [105.0, 480.0]], ('iii.', 0.9985755681991577)]
[[[289.0, 441.0], [1455.0, 441.0], [1455.0, 480.0], [289.0, 480.0]], ('无门槛减0.3元,满50元立减3元,满200元立减30元', 0.9984785914421082)]
[[[126.0, 502.0], [188.0, 502.0], [188.0, 543.0], [126.0, 543.0]], ('iv.', 0.9995737075805664)]
[[[291.0, 502.0], [1455.0, 502.0], [1455.0, 541.0], [291.0, 541.0]], ('无门槛减0.4元,满50元立减4元,满200元立减40元', 0.998653769493103)]
[[[148.0, 570.0], [189.0, 570.0], [189.0, 608.0], [148.0, 608.0]], ('V.', 0.9148564338684082)]
[[[289.0, 563.0], [1455.0, 563.0], [1455.0, 603.0], [289.0, 603.0]], ('无门槛减0.5元,满50元立减5元,满200元立减50元', 0.9982918500900269)]
[[[127.0, 623.0], [195.0, 623.0], [195.0, 667.0], [127.0, 667.0]], ('vi.', 0.9913569092750549)]
[[[288.0, 625.0], [1455.0, 625.0], [1455.0, 664.0], [288.0, 664.0]], ('无门槛减0.6元,满50元立减6元,满200元立减60元', 0.9956303834915161)]
[[[107.0, 688.0], [298.0, 688.0], [298.0, 723.0], [107.0, 723.0]], ('Vii。', 0.9345121383666992)]
[[[279.0, 684.0], [1455.0, 684.0], [1455.0, 725.0], [279.0, 725.0]], ('无门槛减0.7元,满50元立减7元,满200元立减70元', 0.9973915815353394)]
[[[109.0, 744.0], [806.0, 746.0], [806.0, 786.0], [108.0, 785.0]], ('b)同一商品只能使用一张优惠券', 0.9931768178939819)]
它会根据不同文本框进行输出。输出结果是一个列表,每个子项包含了文本框的四个顶点的坐标,文字和识别置信度。
可以使用pillow库绘制文本框,在安装paddleocr后,它已经被安装好了,因此不需要安装。可以使用以下代码实现绘制文本框:
from paddleocr import PaddleOCR
from PIL import Image, ImageDraw
# use_angle_cls:是否使用角度分类
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
img_path = './test.png'
image = Image.open("test.png")
draw = ImageDraw.Draw(image)
result = ocr.ocr(img_path, cls=True)
for res in result:
for line in res:
print(line)
pos = line[0]
draw.rectangle((tuple(pos[0]), tuple(pos[2])), outline='red')
image.save('result.png')
输出的result.png如下:
PaddleOCR/doc/doc_ch/quickstart.md at release/2.7 · PaddlePaddle/PaddleOCR · GitHub
https://yiyan.baidu.com/share/PLKx28jPOQ
更多【编程技术-Python使用PaddleOCR进行图片转文字】相关视频教程:www.yxfzedu.com