百度飞桨 OCR识别代码
python
import warnings
import time
import cv2 as cv
import paddlehub as hub
# Load the image
img = cv.imread("1.jpg")
height, width, channels = img.shape
imglist = [img]
ocr = hub.Module(name="ch_pp-ocrv3", enable_mkldnn=True) # mkldnn acceleration is only effective on CPU
start_time = time.time()
result = ocr.recognize_text(images=imglist)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"执行时间: {elapsed_time} seconds")
for data in result[0]['data']:
text = data['text']
confidence = data['confidence']
if confidence > 0.85:
points = data['text_box_position']
left = min(point[0] for point in points)
top = min(point[1] for point in points)
right = max(point[0] for point in points)
bottom = max(point[1] for point in points)
print(f"Text: {text}, Box: ({left}, {top}), ({right}, {bottom})")
cv.rectangle(img, (left, top), (right, bottom), (0,0, 255), 2)
cv.putText(img, text, (left, top - 10), cv.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2)
resize_img = cv.resize(img, [width // 4, height // 4])
cv.imwrite("result.jpg", resize_img)
cv.imshow("Result",resize_img)
cv.waitKey(0)
cv.destroyAllWindows()
print(result)
显示效果图