【问题标题】:Why is Tesseract OCR not capturing the right output? how can this be solved?为什么 Tesseract OCR 没有捕获正确的输出?如何解决?
【发布时间】:2020-04-03 03:48:34
【问题描述】:

下面是我正在使用的代码,但它给了我奇怪的输出,而不是实际的文本。向有更多 OCR 经验的人寻求帮助。

这是我得到的输出 -

预期输出 -

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Resize, grayscale, Otsu's threshold
image = cv2.imread('image1.jpg')
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[4]

# Invert image and perform morphological operations
inverted = 255 - thresh
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,3))
close = cv2.morphologyEx(inverted, cv2.MORPH_CLOSE, kernel, iterations=1)

# Find contours and filter using aspect ratio and area
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[4]
for c in cnts:
    area = cv2.contourArea(c)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    aspect_ratio = w / float(h)
    if (aspect_ratio >= 2.5 or area < 75):
        cv2.drawContours(thresh, [c], -1, (255,255,255), -1)

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

cv2.imshow('close', close)
cv2.imshow('thresh', thresh)
cv2.waitKey()

【问题讨论】:

    标签: python ocr tesseract text-extraction


    【解决方案1】:

    我稍微修改了您的代码。它可以解码除 1 位以外的所有字符。我不知道为什么。也许你可以微调自己。

    我使用轨迹栏来找到合适的值。如果要使用轨迹栏,请取消注释代码。

    import cv2
    import pytesseract
    
    def nothing(x):
        pass
    
    image = cv2.imread('image1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    cv2.namedWindow("Threshold")
    cv2.createTrackbar("min", "Threshold", 0, 255, nothing)
    cv2.createTrackbar("max", "Threshold", 0, 255, nothing)
    
    while True:
        t_min = cv2.getTrackbarPos("min", "Threshold")
        t_max = cv2.getTrackbarPos("max", "Threshold")
    
        ret, thresh = cv2.threshold(gray.copy(), 180, 255, cv2.THRESH_BINARY_INV)
        # ret, thresh = cv2.threshold(gray.copy(), t_min, t_max, cv2.THRESH_BINARY_INV)
        inverted = cv2.bitwise_not(thresh)
        gauss = cv2.GaussianBlur(inverted, (3,3), 0)
    
        custom_config = '-l eng --oem 3 --psm 6 -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./ "'
        data = pytesseract.image_to_string(gauss, config=custom_config)
        print(data)
    
        cv2.imshow('gauss', gauss)
    
        key = cv2.waitKey(1000)
        if key == 27:
            break
    
    cv2.destroyAllWindows()
    

    这是输出。

    3.59 4.79
    /pek /pek
    Tepung Penaik / Halus Bluekey 1kg Marjerin Seri Pelangi kg
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多