0

I'm trying to get data from this function:

def read_images(path, sz=None):
"""Reads the images in a given folder, resizes images on the fly if size is given.
Args:
 path: Path to a folder with subfolders representing the subjects (persons).
 sz: A tuple with the size Resizes
Returns:
 A list [X,y]
 X: The images, which is a Python list of numpy arrays.
 y: The corresponding labels (the unique number of the subject, person) in a Python list.
"""
c = 0
X,y = [], []
for dirname, dirnames, filenames in os.walk(path):
 for subdirname in dirnames:
 subject_path = os.path.join(dirname, subdirname)
 for filename in os.listdir(subject_path):
 try:
 im = Image.open(os.path.join(subject_path, filename))
 im = im.convert("L")
 # resize to given size (if given)
 if (sz is not None):
 im = im.resize(self.sz, Image.ANTIALIAS)
 X.append(np.asarray(im, dtype=np.uint8))
 y.append(c)
 except IOError, (errno, strerror):
 print "I/O error({0}): {1}".format(errno, strerror)
 except:
 print "Unexpected error:", sys.exc_info()[0]
 raise
 c = c+1
return [X,y]

The problem is that when I call the function

[X,y] = read_images('/Trainer')

where Trainer is the folder that have the images. Now, when I call this function, the function can't reach the folder and the values of X and y are still empty. I tried to print X and print y and both values where X=[] y=[] I tried also to do it like this

TrainerPath = "C:\Users\Eng. Aladdin Hammodi\Desktop\recognizer\Trainer"
[X,y] = read_images(TrainerPath) 

but still have the same thing. What should I do?

asked Jul 1, 2017 at 13:22
4
  • Have you tried listing your files using os.listdir('C:\Users\Eng. Aladdin Hammodi\Desktop\recognizer\Trainer')? Commented Jul 1, 2017 at 13:37
  • No, I haven't. What exactly I should add to the code? Commented Jul 1, 2017 at 13:38
  • Exactly what I wrote above. Add it at the beginning and print the output. Commented Jul 1, 2017 at 13:40
  • I got this Error os.listdir('C:\Users\Eng. Aladdin Hammodi\Desktop\recognizer\Trainer') WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Eng. Aladdin Hammodi\\Desktop\recognizer\\Trainer/*.*' Commented Jul 1, 2017 at 13:44

2 Answers 2

1

The \ symbol escapes characters in your string. Try using a raw string modifier, like this: TrainerPath = r"C:\Users\Eng. Aladdin Hammodi\Desktop\recognizer\Trainer"

answered Jul 1, 2017 at 13:40
Sign up to request clarification or add additional context in comments.

4 Comments

Have you debugged it? What does os.walk(...) return, just an empty generator or an error? at which point in the code does it break?
File "C:\Python27\lib\site-packages\numpy\core\shape_base.py", line 234, in vstack return _nx.concatenate([atleast_2d(_m) for _m in tup], 0) ValueError: all the input array dimensions except for the concatenation axis must match exactly >>>
This is specific to how you concatenate numpy arrays, can't say anything without looking at the data. Perhaps try posting a separate question since this is unrelated to this one.
0

What happens if you escape the "\" - either by putting and r before the path ( r'C:\Users....') or like 'C:\\Users...'?

answered Jul 1, 2017 at 13:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.