Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

识别不了竖排文字 #455

Open
12dc32d opened this issue Jul 1, 2024 · 0 comments
Open

识别不了竖排文字 #455

12dc32d opened this issue Jul 1, 2024 · 0 comments

Comments

@12dc32d
Copy link

12dc32d commented Jul 1, 2024

我尝试在图像预处理中使用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)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant