欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 百度飞桨 OCR识别

百度飞桨 OCR识别

2024/10/25 11:21:47 来源:https://blog.csdn.net/zzx2016zzx/article/details/140889872  浏览:    关键词:百度飞桨 OCR识别

百度飞桨 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)

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com