Pytesser不准确
这是个简单的问题。当我用 这个 图片去运行 pytesser 的时候,我得到的结果是 $+s
。我该怎么解决这个问题呢?
补充说明
所以……我的代码生成的图片和上面链接的图片类似,只是数字不同,目的是要解决一个简单的数学题。如果我从图片中只能得到 $+s
,那显然是没办法完成这个任务的。
这是我现在使用的代码:
from pytesser import *
time.sleep(2)
i = 0
operator = "+"
while i < 100:
time.sleep(.1);
img = ImageGrab.grab((349, 197, 349 + 452, 197 + 180))
equation = image_to_string(img)
然后我会继续解析 equation
……只要我能让 pytesser 正常工作。
2 个回答
1
如果你在使用Linux,建议用gocr,它的准确性更高。你可以通过
os.system("/usr/bin/gocr %s") % (sample_image)
来使用它,并且可以从标准输出中读取每一行,这样你就可以对输出结果进行处理,做成你想要的任何东西(比如从gocr生成特定变量的输出)。
2
试试我这个小函数。我是在svn
代码库中运行tesseract
,所以我的结果可能会更准确。
我在使用Linux系统,所以如果你在Windows上,可能需要把tesseract
换成tesseract.exe
才能正常工作。
import tempfile, subprocess
def ocr(image):
tempFile = tempfile.NamedTemporaryFile(delete = False)
process = subprocess.Popen(['tesseract', image, tempFile.name], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.STDOUT)
process.communicate()
handle = open(tempFile.name + '.txt', 'r').read()
return handle
还有一个Python的示例会话:
>>> import tempfile, subprocess
>>> def ocr(image):
... tempFile = tempfile.NamedTemporaryFile(delete = False)
... process = subprocess.Popen(['tesseract', image, tempFile.name], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.STDOUT)
... process.communicate()
... handle = open(tempFile.name + '.txt', 'r').read()
... return handle
...
>>> print ocr('326_fail.jpg')
0+1