Python PyteSeract无法识别图像

Python PyteSeract无法识别图像,python,opencv,python-tesseract,Python,Opencv,Python Tesseract,我目前面临PyteSeract的一个问题,软件无法检测到此图像中的数字: 这是从应用了阈值滤波器的较大图像中获取的 出于某种原因,pytesseract不想识别此图像中的6。有什么建议吗?这是我的密码: image = #Insert raw image here. My code takes a screenshot. image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) image = cv2.medianBlur(image, 3) rel, g

我目前面临PyteSeract的一个问题,软件无法检测到此图像中的数字:

这是从应用了阈值滤波器的较大图像中获取的

出于某种原因,pytesseract不想识别此图像中的6。有什么建议吗?这是我的密码:

image = #Insert raw image here. My code takes a screenshot.
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image = cv2.medianBlur(image, 3)
rel, gray = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# If you want to use the image from above, start here.
image = Image.fromarray(image)
string = pytesseract.image_to_string(image)
print(string)

编辑:经过进一步的调查,我的代码可以很好地处理包含2位数字的数字。但不是那些具有单数数字的字符。

PyteSeract默认为查找大块文本的模式(PSM_SINGLE_BLOCK或--PSM 6),为了让它检测单个字符,需要使用选项--PSM 10(PSM_SINGLE_CHAR)运行它。但是,由于您提供的图像角落中存在黑点,因此它会将它们检测为随机破折号,并且在此模式下不会返回任何内容,因为它认为存在多个字符,因此在本例中,您需要使用--psm 8(psm_SINGLE_WORD):

string=pytesseract.image_to_string(image,config='--psm 8')

此操作的输出将包括这些随机字符,因此您需要在pytesseract运行后剥离它们,或者改进数字周围的边界框以消除任何噪波。此外,如果检测到的所有字符都是数字,则可以在“-psm 8”之后添加“-c tessedit_char_whitelist=0123456789”以改进检测


简化代码的其他一些小技巧是,cv2.imread有一个选项,可以将图像读取为黑白,这样以后就不需要运行cvtColor,只需执行以下操作:

image=cv2.imread('/path/to/image/6.png',0)

此外,您还可以在对pytesseract的调用中创建PIL image对象,以便将该行简化为:

string=pytesseract.image_to_string(image.fromarray(img),config='--psm 8')


只要您的脚本顶部有“from PIL import Image”(从PIL导入图像)。

是“Image.Image.fromarray”还是“Image.fromarray”?取决于您在Python脚本中导入PIL的方式,如果您执行
将PIL作为图像导入
,则需要
图像.Image.fromarray
,但是,如果从PIL导入图像执行
,则只需编写
Image.fromarray
。现在再看看枕头的文档,看起来他们通常使用第二种方式,所以我将更新我的帖子以匹配这一点。