百度飞桨 OCR识别

发布于:2024-08-08 ⋅ 阅读:(116) ⋅ 点赞:(0)

百度飞桨 OCR识别代码

因为识别的有中文,且要用opencv渲染中文,所要卸载的opencv-python

pip uninstall opencv-python
pip uninstall opencv-contrib-python

然后安装

pip install opencv-python-rolling -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-contrib-python-rolling -i https://pypi.tuna.tsinghua.edu.cn/simple

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"执行时间: 0.0535 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)

显示效果图
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到