Pytesseract 不检测图像中的数字

Pytesseract doesn't detect number in a image

我有两张图片,用 opencv 读入并尝试用 pytesseract 识别里面的数字。其中一张图像检测到正确的数字。另一个根本没有检测到任何数字。两张图片都是来自同一个 phone 的截屏,取自同一个应用。所以字体和对齐方式是一样的。下面是我为此目的使用的代码。

import cv2
import pytesseract
import os

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
xconfig='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'

plist = [x for x in os.listdir() if x.endswith(".png")]
for pt in plist:
    img = cv2.imread(pt)
    pytesseract.image_to_string(img,config=xconfig)

这是第一张图片,正确检测到这里的数字

下面的数字没有被检测到。

在上面的例子中,如果我们在没有任何自定义配置的情况下使用,则会检测到以下字符:'lO R Ly Reb yL\n\x0c'

你应该取图像的阈值:

thr = cv2.threshold(src=gry, thresh=0, maxval=255, type=cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]

现在阅读

txt = pytesseract.image_to_string(thr)
print(txt)

结果:

662,157,015,578

代码:

import cv2
import pytesseract

img = cv2.imread("v0cUq.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.threshold(src=gry, thresh=0, maxval=255, type=cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
txt = pytesseract.image_to_string(thr)
print(txt)