我正在尝试从文件夹中读取文件并运行与 .py
文件位于同一目录中的 colorDescriptor.exe
。实际上我想要,每次读取文件来计算 colorDescriptor 时。
我的代码如下:
import os
from os import listdir
from os.path import isfile, join
mypath1 = "/clothes/"
mypath2 = "/i386-win-vc/"
onlyfiles = [ f for f in listdir(mypath1) if isfile(join(mypath1,f)) ]
image = mypath1+f
os.popen("colorDescriptor image --detector harrislaplace --descriptor sift --output
onlyfiles.txt ")
print image
在终端中,使用 colorDescriptor.exe 的语法如下:
colorDescriptor image.jpg --detector harrislaplace --descriptor sift --output onlyfiles.txt
我收到错误:
Tue04 10:53:30,248 - [Impala.Persistency.FileSystem ] Unable to find image in path
Tue04 10:53:30,248 - [Impala.Core.Array.ReadFile ] Don't know how to read
Tue04 10:53:30,248 - [Sandbox.koen.mainColorDescriptor ] [ERROR] Could not read input
file: is it really a valid image? image
使用建议的代码更改后:
import os
from os import listdir
from os.path import isfile, join
mypath1 = "C:/Documents and Settings/Desktop/clothes/"
mypath2 = "C:/Documents and Settings/My
Documents/colordescriptors40/i386-win-vc/"
onlyfiles = [ f for f in listdir(mypath1) if isfile(join(mypath1,f)) ]
image = mypath1+f
print image
pattern = "colorDescriptor %s --detector harrislaplace --descriptor sift --output
onlyfiles.txt"
os.system(pattern % image)
我现在收到以下内容:
Tue04 11:06:45,091 ERROR [Impala.Persistency.FileSystem ] Unable to find C:/Documents
in
path
Tue04 11:06:45,091 INFO [Impala.Persistency.FileSystem ]
Tue04 11:06:45,091 ERROR [Impala.Core.Array.ReadFile ] Don't know how to read
Tue04 11:06:45,091 ERROR [Sandbox.koen.mainColorDescriptor ] [ERROR] Could not read input
文件:这真的是一个有效的图像吗? C:/文件 【0.1秒完成】
最佳答案
问题是您没有使用在命令中生成的值。您需要使用 glob.glob 获取目录中的图像(可能是“*.jpg”)、文件的列表,然后为每个文件创建一个新的 outfile.text 名称和命令,如下所示:
cmd = "colorDescriptor %s --detector harrislaplace --descriptor sift --output %s.txt " % (imagepath, imagepath)
os.popen(cmd)
关于python - 从 python 脚本调用 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166769/