2

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]

6
  • have you getting any error? Commented Mar 4, 2014 at 8:42
  • No, onlyfiles contains all the files from the folder. From the terminal colorDescriptor works like here: koen.me/research/colordescriptors/readme Commented Mar 4, 2014 at 8:44
  • Will you please elaborate what output you expect ? Commented Mar 4, 2014 at 8:48
  • Ok, to be more clear, it seems that os.popen didnt work. It doesnt do anything. What I expect is, a txt file onlyfiles.txt to be created with color description of the image.jpg, as it does when I am running the batch file. Commented Mar 4, 2014 at 8:50
  • What is the problem you want help with? If you want to see the output of colorDescriptor on the console, or if you want python to receive it in a variable, see the popen documentation; or better yet, use the subprocess module. Commented Mar 4, 2014 at 8:50

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are totally right, this is exactly what I am intend to do, but I ve got problems using the file that I read in colorDescriptor.
2

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.

3 Comments

I change the code, however, I am receiving the same error.
How can I use this syntax for two string like here: fileOut = f+".txt" pattern = "colorDescriptor %s --detector harrislaplace --descriptor sift --output %s" os.system(pattern % image fileOut)
The best way to use a value multiple times is with the new python substitution syntax; see my updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.