简体   繁体   English

pytesseract不适用于一位数的图像

[英]pytesseract don't work with one digit image

I have code using pytesseract and work perfect, only don't work when the image I try to recognize are 0 to 9. If image only have one digit don't give any result. 我有使用pytesseract的代码和完美的工作,只有当我尝试识别的图像是0到9时才工作。如果图像只有一个数字不给任何结果。

This a sample of image I'm working https://drive.google.com/folderview?id=0B68PDhV5SW8BdFdWYVRwODBVZk0&usp=sharing 这是我正在使用的图片示例https://drive.google.com/folderview?id=0B68PDhV5SW8BdFdWYVRwODBVZk0&usp=sharing

And this the code I'm using 这就是我正在使用的代码

    import pytesseract
    varnum= pytesseract.image_to_string(Image.open('images/table/img.jpg'))
    varnum = float(varnum)
    print varnum    

Thanks!!!! 谢谢!!!!

With this code I'm able to read all numbers 使用此代码,我可以读取所有数字

import pytesseract


start_time = time.clock()
y = pytesseract.image_to_string(Image.open('images/table/1.jpg'),config='-psm 10000')
x = pytesseract.image_to_string(Image.open('images/table/1.jpg'),config='-psm 10000')

print y
print x

y = pytesseract.image_to_string(Image.open('images/table/68.5.jpg'),config='-psm 10000')
x = pytesseract.image_to_string(Image.open('images/table/68.5.jpg'),config='-psm 10000')

print y
print x

print time.clock() - start_time, "seconds" 

result 结果

>>> 
1
1
68.5
68.5
0.485644155358 seconds
>>> 

You would need to set the Page Segmentation mode to be able to read single character/digits. 您需要将页面分割模式设置为能够读取单个字符/数字。

From the tesseract-ocr manual (which is what pytesseract internally uses), you can set the page segmentation mode using - tesseract-ocr手册 (这是pytesseract内部使用的),你可以使用 - 设置页面分割模式 -

-psm N -psm N.

Set Tesseract to only run a subset of layout analysis and assume a certain form of image. 将Tesseract设置为仅运行布局分析的子集并假设某种形式的图像。 The options for N are: N的选项是:

10 = Treat the image as a single character. 10 =将图像视为单个字符。

So you should set the -psm option to 10. Example - 所以你应该将-psm选项设置为10.示例 -

varnum= pytesseract.image_to_string(Image.open('images/table/img.jpg'),config='-psm 10')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM