无法通过 os.system 从 Tesseract 命令 运行 获取输出
Can't get output from Tesseract command run through 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
的输出,例如使用 Popen
和 subprocess
但没有任何成功。看来我无法捕捉到 tesseract
库的输出。
那么,我究竟应该怎么做呢?
谢谢,
言特
在问完这个问题 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
库传递命令,并且由于来自 commands
库的 getstatusoutput
捕获了输出。
我创建了一个函数,它循环图像并使用 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
的输出,例如使用 Popen
和 subprocess
但没有任何成功。看来我无法捕捉到 tesseract
库的输出。
那么,我究竟应该怎么做呢?
谢谢, 言特
在问完这个问题 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
库传递命令,并且由于来自 commands
库的 getstatusoutput
捕获了输出。