OpenCV tesseract无法检测图像中的单个数字

2024-09-30 20:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我将tesseract与python一起使用。它可以识别几乎所有带有2个或更多数字或字符的图像

我不想用“只有数字”来训练tesseract,因为我也在识别字符

但是附加的图像无法从tessearact中识别


Tags: 图像数字字符tesseracttessearact
1条回答
网友
1楼 · 发布于 2024-09-30 20:22:01

我认为问题是由大胆的边界造成的。 去掉这个数字后,数字被正确识别了

上面是校正后的图像: enter image description here

如果您感兴趣,下面是代码:

import cv2
import numpy as np
import pytesseract


def discard(image):
    image = np.uint8(image)
    _, im_label, stts, _ = cv2.connectedComponentsWithStats(image, connectivity=4)

    msk1 = np.isin(im_label, np.where(stts[:, cv2.CC_STAT_WIDTH] > 100)[0])
    msk2 = np.isin(im_label, np.where(stts[:, cv2.CC_STAT_HEIGHT] > 100)[0])

    image[(msk1 | msk2)] = 0
    return image


img = cv2.imread("check_img.jpg", 0)

# Binarization
thresh = 255 - img
ret, thresh = cv2.threshold(thresh, 5, 255, cv2.THRESH_BINARY)

# removing long connected-components
thresh = discard(thresh)

# remove noise
thresh = cv2.medianBlur(thresh, 3)

# invert again 
thresh = 255 - thresh

# showing the image
cv2.imshow("img", thresh)

# Using Tesseract OCR
custom_config = r' oem 3  psm 6'
text = pytesseract.image_to_string(thresh, config=custom_config)

print(text)

cv2.waitKey(0)

相关问题 更多 >