字符识别问题pytesseract/tesseract-OCR/无法识别

时间:2021-01-30 19:00:17

标签: python tesseract python-tesseract

正如标题中提到的,我无法识别字符,tesseract 无法识别任何数字。我如何解决这个问题?

图像:

enter image description here

enter image description here

enter image description here

代码:

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\\Tesseract-OCR\tesseract.exe'

img = cv2.imread('img002.png')
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(gry, 100, 128, cv2.THRESH_BINARY_INV)[1]
cv2.imshow('Show', thr)
cv2.waitKey(0)
cmd = pytesseract.image_to_string(thr)
xx = pytesseract.image_to_boxes(thr)
print(cmd)
print(xx) 

我的输出

ann

a 8 0 34 0 0
n 50 0 75 0 0
n 92 0 118 0 0

1 个答案:

答案 0 :(得分:0)

对于第一张和第三张图片,您可以找到包含 OTSU's thresholding 的数字。

thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
<头>
图片 结果
enter image description here 10
4
20
enter image description here 10
3
5

该技术对第二张图片没有用:

enter image description here

我们需要改变阈值方法

thresh = cv2.threshold(crop, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

如果我们逐行

<头>
当前数字 结果
enter image description here 6
enter image description here 4
enter image description here

不幸的是,8 无法识别。您需要应用不同的组合来查找 8

代码:

import cv2
import pytesseract

names = ["C2GJJ.png", "GKH7k.png", "neIWN.png"]

for name in names:
    image = cv2.imread(name)
    (height, width) = image.shape[:2]
    image = cv2.resize(image, (width*4, height*4))
    grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    text = pytesseract.image_to_string(thresh, config="--psm 6 digits")
    print(text)
    cv2.imshow("result", thresh)
    cv2.waitKey(0)

second_image = cv2.imread("GKH7k.png")
(height, width) = second_image.shape[:2]
second_image = cv2.resize(second_image, (width * 4, height * 4))
second_grey = cv2.cvtColor(second_image, cv2.COLOR_BGR2GRAY)
start = 0
end = int(height/2) + 20

for _ in range(0, 3):
    crop = second_grey[start:end, 0:width]
    (height_crop, width_crop) = crop.shape[:2]
    crop = cv2.resize(crop, (width_crop*2, height_crop*2))
    second_thresh = cv2.threshold(crop, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    second_text = pytesseract.image_to_string(second_thresh,
                                              config="--psm 6 digits")
    print(second_text)
    start = end
    end = start + int(height/2) + 40
    cv2.imshow("second_thresh", second_thresh)
    cv2.waitKey(0)