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]

snake plissken
  • 2,489
  • 9
  • 37
  • 61
  • have you getting any error? – Nilesh Mar 04 '14 at 08:42
  • No, onlyfiles contains all the files from the folder. From the terminal colorDescriptor works like here: http://koen.me/research/colordescriptors/readme – snake plissken Mar 04 '14 at 08:44
  • Will you please elaborate what output you expect ? – Nilesh Mar 04 '14 at 08: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. – snake plissken Mar 04 '14 at 08: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. – alexis Mar 04 '14 at 08:50
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – gpgekko Mar 04 '14 at 08:50

2 Answers2

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)
Steve Barnes
  • 24,968
  • 6
  • 54
  • 63
  • 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. – snake plissken Mar 04 '14 at 09:00
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.

alexis
  • 43,587
  • 14
  • 86
  • 141
  • I change the code, however, I am receiving the same error. – snake plissken Mar 04 '14 at 09:04
  • 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) – snake plissken Mar 04 '14 at 09:19
  • 1
    The best way to use a value multiple times is with the new python substitution syntax; see my updated answer. – alexis Mar 04 '14 at 13:00