一:easyocr识别不旋转的图片的文字效果还可以
import json
import os
import easyocr
import numpy as np
import cv2
#初始化加载模型, 创建reader对象
reader = easyocr.Reader(['ch_sim', 'en'])
## 图片旋转
def rotate_bound(image, angle):
# 获取宽高
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# 提取旋转矩阵 sin cos
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# 计算图像的新边界尺寸
nW = int((h * sin) + (w * cos))
# nH = int((h * cos) + (w * sin))
nH = h
# 调整旋转矩阵
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
return cv2.warpAffine(image, M, (nW, nH), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
## 获取图片旋转角度
def get_minAreaRect(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# print(thresh)
coords = np.column_stack(np.where(thresh > 0))
# print(coords)
return cv2.minAreaRect(coords)
def get_text(path):
# 创建reader对象
# reader = easyocr.Reader(['ch_sim', 'en'])
image = cv2.imread(path)
# image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
#方法不好用,暂时关闭
angle = get_minAreaRect(image)[-1]
# rotated = rotate_bound(image, angle)
# 读取图像文字
# result = reader.readtext(rotated)
result = reader.readtext(image)
# result = reader.readtext_batched(image)
print(result)
#保存识别结果
def save_text(text,save_files):
with open(save_files, 'w',encoding="utf-8") as f:
json.dump(text, f, ensure_ascii=False)
if __name__ == '__main__':
# img_path="./statics"
img_path="./healthcodeimg"
# save_path="./ocr_result_new"
save_path="./healthcode_ocr_result"
os.makedirs(save_path, exist_ok=True)
img_list=os.listdir(img_path)
for img_name in img_list:
img_file=os.path.join(img_path,img_name)
# path1 = 'Snipaste_2021-11-15_14-23-53.png'
text1 = get_text(img_file)
print(text1)
# save_file=os.path.join(save_path,os.path.splitext(img_name)[0]+".json")
# save_text(text1,save_file)
break
如下图:右边是识别效果



