新建项目
下面放这两个文件:
1.run.py
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
def get_result_list(img_path):
# 模型路径下必须含有model和params文件,如果没有,现在可以自动下载了,不过是最简单的模型
# use_gpu 如果paddle是GPU版本请设置为 True
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
result_list = ocr.ocr(img_path, cls=True)
return result_list
def save_img(img_path, result_list):
# 显示结果
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result_list]
txts = [line[1][0] for line in result_list]
scores = [line[1][1] for line in result_list]
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
im_show.save('result.jpg') # 结果图片保存在代码同级文件夹中。
if __name__ == '__main__':
img_path = r'./test.jpg' # 这个是自己的图片,自行放置在代码目录下修改名称
result_list = get_result_list(img_path)
print(result_list)
save_img(img_path, result_list)
2.test.jpg
任意一张带文字的图片
(此图为paddleocr自带的示例图)
安装依赖
python -m pip install paddlepaddle paddleocr -i https://mirror.baidu.com/pypi/simple
图片识别
右键运行 run.py
得到如下结果