from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
from PIL import Image
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
# 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
img_path = 'img/test.png'#改成自己图片的地址
result = ocr.ocr(img_path, cls=True)
for line in result:
print(line)
# 显示结果
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/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.show()
im_show.save('img/result.jpg')
12345678910111213141516171819
当然,也可以对整个文件夹里的图片内容进行检测识别并可以保存识别结果。
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
from PIL import Image
import os
import csv
def pre_save(img_path,save_path,csv_path):
f = open(csv_path, 'w', encoding='utf-8')
writer = csv.writer(f)
writer.writerow(["img", "result"])
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
# 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。
ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
i=0
for img in os.listdir(img_path):
print(img_path+'/'+img)
i+=1
result = ocr.ocr(img_path+'/'+img, cls=True)
image = Image.open(img_path+'/'+img).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/simfang.ttf')
im_show = Image.fromarray(im_show)
#im_show.show()
im_show.save(save_path+img)
al = []
for res in result:
result = res[1][:][:]
al.append(result)
print(str(al))
writer.writerow([img,str(al)])
print(i)
##img_path是要检测的图片的文件夹地址,save_path是要保存结果的文件夹的地址(注意后面要有个/),csv_path是识别结果要保存的csv文件地址(可以自动创建)
img_path=r'F:\lzpython\PaddleOCR-develop\doc\imgs'
save_path=r'F:\chrome\zrbdata\imgs\result1/'
csv_path=r'F:\chrome\zrbdata\imgs\result.csv'
pre_save(img_path,save_path,csv_path)
1234567891011121314151617181920212223242526272829303132333435363738