参考github的官网就可以:
简单的说,就是两句话:
python3 -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple
pip install "paddleocr>=2.0.1" # 推荐使用2.0.1+版本
from paddleocr import PaddleOCR
import cv2
import numpy as np
if __name__=="__main__":
ocr = PaddleOCR(use_angle_cls=True, use_gpu=True,ocr_version='PP-OCRv4')
img_path = r'F:\dataset\images\1.bmp'
img=cv2.imread(img_path)
result = ocr.ocr(img, cls=True)[0]
for line in result:
list_points=line[0]
str_data,conf=line[1]
pts = np.array(list_points, np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 255), thickness=1)
cv2.imshow("img",img)
cv2.waitKey(0)
如果想对每一个字符都做框选,可以多写几句画,把每个框都拆出来。
from paddleocr import PaddleOCR
import cv2
import numpy as np
if __name__=="__main__":
ocr = PaddleOCR(use_angle_cls=True, use_gpu=True,ocr_version='PP-OCRv4')
img_path = r'F:\dataset\images\1.bmp'
img=cv2.imread(img_path)
result = ocr.ocr(img, cls=True)[0]
for line in result:
list_points=line[0]
str_data,conf=line[1]
len_str=len(str_data)
p0,p1,p2,p3=np.array(list_points)
for ic in range(len_str):
p0_c=p0+(p1-p0)/len_str*ic
p1_c=p0+(p1-p0)/len_str*(ic+1)
p2_c=p3+(p2-p3)/len_str*(ic+1)
p3_c=p3+(p2-p3)/len_str*(ic)
list_ps=[p0_c,p1_c,p2_c,p3_c]
pts = np.array(list_ps, np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 255), thickness=1)
cv2.imshow("img",img)
cv2.waitKey(0)