DayBreak-u / chineseocr_lite

超轻量级中文ocr,支持竖排文字识别, 支持ncnn、mnn、tnn推理 ( dbnet(1.8M) + crnn(2.5M) + anglenet(378KB)) 总模型仅4.7M
GNU General Public License v2.0
11.77k stars 2.26k forks source link

识别不了竖排文字 #455

Open 12dc32d opened 3 months ago

12dc32d commented 3 months ago

我尝试在图像预处理中使用opencv添加识别和旋转图像功能,分别是: def detect_and_rotate_image(image_path):

读取图像

image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
adaptive = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 4)

cnts = cv2.findContours(adaptive, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

mask = np.zeros(image.shape, dtype=np.uint8)
for c in cnts:
    area = cv2.contourArea(c)
    if area < 45000 and area > 20:
        cv2.drawContours(mask, [c], -1, (255, 255, 255), -1)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
h, w = mask.shape

# 检测图像的方向并旋转
angle = None
if w > h:
    left = mask[0:h, 0:w//2]
    right = mask[0:h, w//2:]
    left_pixels = cv2.countNonZero(left)
    right_pixels = cv2.countNonZero(right)
    angle = 0 if left_pixels >= right_pixels else 180
else:
    top = mask[0:h//2, 0:w]
    bottom = mask[h//2:, 0:w]
    top_pixels = cv2.countNonZero(top)
    bottom_pixels = cv2.countNonZero(bottom)
    angle = 90 if bottom_pixels >= top_pixels else 270

if angle == 90 or angle == 270:
    if angle == 90:
        rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
    else:
        rotated_image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
    # 保存旋转后的图片,替换原图片
    cv2.imwrite(image_path, rotated_image)
return image_path

##########################

检测并旋转竖排文本图片

    detect_and_rotate_image(image_path_list[i])

    # 循环遍历图片路径进行判断
    image = Image.open(image_path_list[i])
    res = ocr.ocr(image)

############## 为什么加入处理后还跟没加这段代码前有相同的乱码输出,是什么原因导致代码没生效呢?