无法从通过os.system运行的Tesseract命令获得输出

时间:2016-01-16 13:42:59

标签: python subprocess tesseract popen os.system

我创建了一个循环图像的函数,并使用tesseract库从图像中获取方向。代码如下所示:

def fix_incorrect_orientation(pathName):
    for filename in os.listdir(pathName):
        tesseractResult = str(os.system('tesseract ' + pathName + '/' + filename + ' -  -psm 0'))
        print('tesseractResult: ' + tesseractResult)
        regexObj = re.search('([Orientation:]+[\s][0-9]{1})',tesseractResult)
        if regexObj:
            orientation = regexObj.groups(0)[0]
            print('orientation123: ' + str(orientation))
        else:
            print('Not getting in the Regex.')

变量tesseractResult的结果总是0。但是在终端中,我将从命令中得到以下结果:

Orientation: 3
Orientation in degrees: 90
Orientation confidence: 19.60
Script: 1
Script confidence: 21.33

我尝试以多种方式捕获os.system的输出,例如使用Popensubprocess,但没有任何成功。似乎我无法捕获tesseract库的输出。 那么,我该怎么做呢?

谢谢, Yenthe

1 个答案:

答案 0 :(得分:0)

在询问问题之后10分钟我找到了一个方法..首先导入命令:

import commands

然后以下代码将解决这个问题:

def fix_incorrect_orientation(pathName):
    for filename in os.listdir(pathName):
        tesseractResult = str(commands.getstatusoutput('tesseract ' + pathName + '/' + filename + ' -  -psm 0'))
        print('tesseractResult: ' + tesseractResult)
        regexObj = re.search('([Orientation:]+[\s][0-9]{1})',tesseractResult)
        if regexObj:
            orientation = regexObj.groups(0)[0]
            print('orientation123: ' + str(orientation))
        else:
            print('Not getting in the Regex.')

这将通过commands库传递命令,并且感谢来自getstatusoutput库的commands输出。