测试代码
官方给出了两种模式,一是命令行执行,一是代码执行。为了直观的看到配置,我这里使用的是代码模式。
准备一张带文字的图片
测试代码如下
#!/user/bin/env python # coding=utf-8 """ @project : ocr_paddle @author : huyi @file : test.py @ide : PyCharm @time : 2021-11-15 14:56:20 """ from paddleocr import PaddleOCR, draw_ocr # Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换 # 例如`ch`, `en`, `fr`, `german`, `korean`, `japan` ocr = PaddleOCR(use_angle_cls=True, use_gpu=False, lang="ch") # need to run only once to download and load model into memory img_path = './data/2.jpg' result = ocr.ocr(img_path, cls=True) for line in result: # print(line[-1][0], line[-1][1]) print(line) # 显示结果 from PIL import Image 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='./fonts/simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result.jpg')
代码说明
1、因为我的电脑没有显卡,所以设置了use_gpu=False。
2、显示结果部分会将识别的文字用框标出来,并且展示识别的结果。
验证一下
我们看到,打印的内容有识别出来的每句话所在的图片位置,以及识别结果和可信度。而上面的结果图中,将每句话对应的文字都框了出来。效果很不错!
参数补充
官方还给出了一些参数,可以调整输出的内容。可以参看quickstart.md文件。参数补充:
- 单独使用检测:设置`--rec`为`false`
- 单独使用识别:设置`--det`为`false`
官方还提供一个标准的json结构输出数据
PP-Structure的返回结果为一个dict组成的list,示例如下 ```shell [{ 'type': 'Text', 'bbox': [34, 432, 345, 462], 'res': ([[36.0, 437.0, 341.0, 437.0, 341.0, 446.0, 36.0, 447.0], [41.0, 454.0, 125.0, 453.0, 125.0, 459.0, 41.0, 460.0]], [('Tigure-6. The performance of CNN and IPT models using difforen', 0.90060663), ('Tent ', 0.465441)]) } ] ```
总结
总的来说,这个项目还是很有意思的,训练的部分我就不多赘述了,毕竟准备数据挺麻烦的。回头我再想想这个项目可不可以魔改成好用的工具。