3
\$\begingroup\$

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]]
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please post working code on Code Review. sub-directory is not a valid variable name, please ensure the rest of the code works too. \$\endgroup\$
    – Peilonrayz
    Aug 13, 2017 at 21:29
  • \$\begingroup\$ It works well, i just had to change the original variable names in order to paste here, and that was mistakenly written - not _. i updated my post :) \$\endgroup\$
    – Minä
    Aug 13, 2017 at 21:37
  • \$\begingroup\$ A small note (not a full review): directorys looks really odd to an English-speaker. The correct spelling of the plural form is directories (the same is true for subdirectorys, too). \$\endgroup\$ Aug 14, 2017 at 8:51

1 Answer 1

2
\$\begingroup\$

Some notes:

(photos, identification, testers, id) = ([], [], {}, 0)

may become

photos, identification, testers, id = [], [], {}, 0

as parentheses are superfluous (and even it's recommended not use them).


for (subdirectorys, directorys, files) in os.walk(dir):

may become

for __, directorys, __ in os.walk(dir):

as the names subdirectorys and files are never used (not two consecutive underline symbols - __ - for ignoring their values).


Instead of maintaining the value of id manually (initializing it to 0 and then increase it by id += 1) you may use Pythonic way with help of enumerate() function - the very first line of you code will totally omit id:

photos, identification, testers = [], [], {}

and your for loop

    for subdirectory in directorys:

becomes

    for id, subdirectory in enumerate(directorys):

Two notes about your if statement

        if(f_extension.lower() not in
                ['.png','.jpg','.jpeg','.gif','.pgm']):
  1. Python is not C/C++/Java, so outermost parentheses are superfluous.
  2. Your may prepare your list of extensions in advance

        EXTENSIONS = '.png .jpg .jpeg .gif .pgm'.split()       # split() creates a list
    

    (nice method for saving some typing, isn't it?), and then use it:

        if f_extension.lower() not in EXTENSIONS:
    

(note the use of uppercase letters for a constant list).

\$\endgroup\$
3
  • \$\begingroup\$ Isn't id a builtin name? \$\endgroup\$
    – Luke
    Aug 13, 2017 at 23:36
  • \$\begingroup\$ @LukaszSalitra - The correct term for id is a built-in function. You are right - id() IS a built-in function, so it would be definitely better to choose other name - in spite of the use of the names of built-in functions for other purpose is not forbidden. \$\endgroup\$
    – MarianD
    Aug 14, 2017 at 0:30
  • \$\begingroup\$ nitpick: you may want to talk about the proper pluralization of directory, which is in fact not directorys \$\endgroup\$
    – Vogel612
    Aug 15, 2017 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.