之前试了pytesseract,识别率有点低,而且自己训练也不太行,后来尝试使用 EasyOcr,是基于深度学习的,准确性和功能都更加强大,能输出选框位置和对应转换的文字
需要安装的依赖库
easyocr
cv2
matplotlib
numpy
代码实现
import easyocr
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image,ImageDraw,ImageFont
def cv2AddChineseText(img, text, position, textColor, textSize):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
print("is np.ndarray")
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontStyle = ImageFont.truetype(
"simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text(position, text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
# 创建 EasyOCR 读取器,指定语言
reader = easyocr.Reader(['ch_sim','en']) # 此处可以添加其他语言,如 'ch_sim'(中文)、'fr'(法语)等
# 读取图像
image_path = 'D:\python-workspace\person\images\me.png' # 替换为您的图片路径
image = cv2.imread(image_path)
# 进行 OCR 识别
result = reader.readtext(image)
# 输出识别结果
for (bbox, text, prob) in result:
print(f'Detected text: {text}, Confidence: {prob}')
# 绘制识别结果
(top_left, top_right, bottom_right, bottom_left) = bbox
top_left = tuple(map(int, top_left))
bottom_right = tuple(map(int, bottom_right))
cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
# cv2.putText(image, text, (top_left[0], top_left[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
image = cv2AddChineseText(image, text, (top_left[0], top_left[1] - 5), (255, 0, 0), 20)
# 写文件
cv2.imwrite('me_ocr.png', image)
# 显示结果
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()