4
社区成员
发帖
与我相关
我的任务
分享
import easyocr
import cv2
import matplotlib.pyplot as plt
import numpy as np
#OCR识别之easyocr
# 创建 OCR 阅读器,指定语言(中文简体和英文)
reader = easyocr.Reader(['ch_sim', 'en'])
# 读取图像
image = cv2.imread('9.jpg')
# 识别文字
results = reader.readtext(image)
# 显示识别结果
for (bbox, text, prob) in results:
# 解析边界框
(top_left, top_right, bottom_right, bottom_left) = bbox
top_left = (int(top_left[0]), int(top_left[1]))
top_right = (int(top_right[0]), int(top_right[1]))
bottom_right = (int(bottom_right[0]), int(bottom_right[1]))
bottom_left = (int(bottom_left[0]), int(bottom_left[1]))
# 在图像上绘制边界框和文本
cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
cv2.putText(image, text, (top_left[0], top_left[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# 打印识别结果和置信度
print(f"文本: {text}, 置信度: {prob:.2f}")
# 显示结果图像
plt.figure(figsize=(10, 8))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()