PaddleOCR使用笔记

Linux环境下

paddleocr package使用说明

Windows下使用
安装whl包

pip安装

pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本

在这里插入图片描述
在这里插入图片描述

代码使用
  • 检测+分类+识别全流程
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
# 参数依次为`ch`, `en`, `french`, `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 = './doc/imgs/11.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
    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='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('./results/det_cls_rec_result.jpg')

在这里插入图片描述

  • 检测+识别
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
ocr = PaddleOCR(use_gpu=False) # need to run only once to download and load model into memory
img_path = './doc/imgs/11.jpg'
result = ocr.ocr(img_path)
for line in result:
    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='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('./results/det_rec_result.jpg')

在这里插入图片描述

  • 分类+识别
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_gpu=False, use_angle_cls=True) # need to run only once to download and load model into memory
img_path = './doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False, cls=True)
for line in result:
    print(line)

结果是一个list,每个item只包含识别结果和识别置信度

('韩国小馆', 0.9972204)
  • 单独执行检测
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
ocr = PaddleOCR(use_gpu=False) # need to run only once to download and load model into memory
img_path = './doc/imgs/11.jpg'
result = ocr.ocr(img_path, rec=False)
for line in result:
    print(line)

# 显示结果
from PIL import Image

image = Image.open(img_path).convert('RGB')
im_show = draw_ocr(image, result, txts=None, scores=None, font_path='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('./results/det_result.jpg')

在这里插入图片描述

  • 单独执行识别
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_gpu=False) # need to run only once to download and load model into memory
img_path = './doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False)
for line in result:
    print(line)

结果是一个list,每个item只包含识别结果和识别置信度

('韩国小馆', 0.9972204)
  • 单独执行分类
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_gpu=False, use_angle_cls=True) # need to run only once to download and load model into memory
img_path = './doc/imgs_words/ch/word_1.jpg'
result = ocr.ocr(img_path, det=False, rec=False, cls=True)
for line in result:
    print(line)

结果是一个list,每个item只包含分类结果和分类置信度

['0', 0.9998784]
自定义模型
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
# The path of detection and recognition model must contain model and params files
ocr = PaddleOCR(use_gpu=False, det_model_dir='./inference/ch_det_mv3_db/', rec_model_dir='./inference/rec_en_number_lite/', rec_char_dict_path='./ppocr/utils/dict/en_dict.txt')
img_path = './doc/imgs_10_BZ_test/20160517_100146_H4H2930367BZ_Z.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
    print(line)

# draw result
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='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('./results/custom_result.jpg')

在这里插入图片描述

Linux下命令行使用
安装whl包
pip3 install "paddleocr>=2.0.1" -i https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

在这里插入图片描述

  • 查看帮助信息
paddleocr -h

在这里插入图片描述
在这里插入图片描述

使用自定义模型
  • ~~要换一个目录,~~还要用pip3.8
pip3.8 install "paddleocr>=2.0.1"

在这里插入图片描述

在这里插入图片描述

  • 不用换目录,但要改paddleocr.py的代码:
- SUPPORT_DET_MODEL = ['DB']
+ SUPPORT_DET_MODEL = ['DB', 'EAST']
  • 使用自定义模型
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
# The path of detection and recognition model must contain model and params files
ocr = PaddleOCR(det_model_dir='./inference/det_mv3_east/', rec_model_dir='./inference/rec_en_number_lite_crown/', use_space_char=False, rec_char_type='EN', det_algorithm='EAST', rec_char_dict_path='ppocr/utils/dict/en_dict.txt')
img_path = './doc/imgs_crown_test/20160517_101050_A8Y0930108BZ_Z.jpg'
result = ocr.ocr(img_path)
for line in result:
    print(line)

# draw result
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='./doc/fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('./results/det_rec_20160517_101050_A8Y0930108BZ_Z.jpg')

在这里插入图片描述

创作不易,喜欢的话加个关注点个赞,❤谢谢谢谢❤

GitHub 加速计划 / pa / PaddleOCR
41.53 K
7.59 K
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:3 个月前 )
7bbda2bc 6 天前
1d4e7a80 8 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐