本帖最后由 云天徽上 于 2024-9-14 13:55 编辑
1、使用默认路径
import cv2
from paddleocr import PaddleOCR
paddleocr = PaddleOCR(lang='ch', show_log=False)
img = cv2.imread('test.jpg') # 打开需要识别的图片
result = paddleocr.ocr(img)
for i in range(len(result[0])):
print(result[0][i][1][0]) # 输出识别结果
2、使用本地路径打开
import cv2
from paddleocr import PaddleOCR
# 设定模型路径
paddleocr = PaddleOCR(lang='ch', show_log=False,
det_model_dir='.paddleocr\\whl\\det\\ch\\ch_PP-OCRv4_det_infer',
rec_model_dir='.paddleocr\\whl\\rec\\ch\\ch_PP-OCRv4_rec_infer') # 推理模型路径
img = cv2.imread('test.jpg') # 打开需要识别的图片
result = paddleocr.ocr(img)
for i in range(len(result[0])):
print(result[0][i][1][0]) # 输出识别结果
3、OCR的结果进行绘制
def show_ocr_image(result,image):
# ocr的结果显示,result是OCR的结果,image是图片的矩阵
result = result[0]
#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)
image0 = cv2.cvtColor(np.asarray(im_show), cv2.COLOR_RGB2BGR)
cv2.imshow('rec_result',image0)
cv2.waitKey(0)