【问题标题】:OpenCV tesseract not detect single digit number in an imageOpenCV tesseract 无法检测图像中的单个数字
【发布时间】:2020-06-01 07:59:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: python ocr tesseract


    【解决方案1】:

    我认为问题是由粗边框引起的。 删除后,数字被正确识别。

    以上是校正后的图像:

    如果你有兴趣,这里是代码:

    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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 2017-06-14
      相关资源
      最近更新 更多