I want to reduce/shorten this part quite a lot. If you see that a method can be reduced or even erased please do not hesitate to suggest that. Any answer that rescue the code would be great.
(photos, identification, testers, id) = ([], [], {}, 0)
# Get the folders containing the training data
for (subdirectorys, directorys, files) in os.walk(dir):
# Loop through each folder named after the subject in the photos
for subdirectory in directorys:
testers[id] = subdirectory
subpath = os.path.join(dir, subdirectory)
# Loop through each photo in the folder
for file_name in os.listdir(subpath):
# Skip non-image formates
f_name, f_extension = os.path.splitext(file_name)
if(f_extension.lower() not in
['.png','.jpg','.jpeg','.gif','.pgm']):
print("Skipping "+file_name+", wrong type given for the file")
continue
path = subpath + '/' + file_name
lable = id
# Add to training data
photos.append(cv2.imread(path, 0))
identification.append(int(lable))
id += 1
(im_width, im_height) = (112, 92)
# Create a Numpy array from the two lists above
(photos, identification) = [numpy.array(lis) for lis in [photos, identification]]
sub-directory
is not a valid variable name, please ensure the rest of the code works too. \$\endgroup\$directorys
looks really odd to an English-speaker. The correct spelling of the plural form isdirectories
(the same is true forsubdirectorys
, too). \$\endgroup\$