Question

I am trying to read files from folder and to run the colorDescriptor.exe which is in the same directory with .py file. Actually I want, every time that it reads a file to calculate the colorDescriptor.

My code is the below:

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

From the terminal, the syntax to use colorDescriptor.exe is for example:

colorDescriptor image.jpg --detector harrislaplace --descriptor sift --output onlyfiles.txt

I am receiving as an error:

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

After change it with the proposed code:

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)

I am receiving now the below:

 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 

file: is it really a valid image? C:/Documents [Finished in 0.1s]

Était-ce utile?

La solution

The problem is that you are not using the values you generate in your command. You need to use glob.glob to get a list of the image, (probably '*.jpg'), files in the directory and then for each create a new outfile.text name and command something like:

    cmd = "colorDescriptor %s --detector harrislaplace --descriptor sift --output %s.txt " % (imagepath, imagepath)
    os.popen(cmd)

Autres conseils

The error message makes it clear: Your sample code runs colorDescriptor on the file image in the current directory. From the code context, though, we can see that image is a variable containing the path and real filename. So do it like this:

pattern = "colorDescriptor %s --detector harrislaplace --descriptor sift --output onlyfiles.txt" 
os.popen(pattern % image)

Edit: To use the same variable in for the output filename as well, the best way is to switch to python's new named syntax. Here's an example that puts all output files in the directory from which you run the script, rather than in the same directory as each file (I trust you see how to change this if it's not what you want).

pattern = "colorDescriptor {path}/{file} --output {file}.txt --detector harrislaplace --descriptor sift" 
os.popen( pattern.format(path=mypath1, file=f) )

I rearranged the order of arguments for better visibility-- I assume it makes no difference.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top