Home > Net >  Listing image files in a directory in the order that they are displayed in
Listing image files in a directory in the order that they are displayed in

Time:07-09

I am working on an semantic segmentation task using the MHP dataset. I've downloaded the dataset and it has the following structure:

folder_structure

I am trying to create a list of training images and a list of their corresponding segmentation mask which lives in the "train" folder which looks like this:

train_folder

Each input image has several masks for each person in the image, and for my purposes I'm only interested in the first one. For instance, image "1.jpg" has the following masks: "1_02_01.png" and "1_02_02.png", but I'm only interested in the first one i.e. "1_02_01.png".

Now when I try to create the lists I've mentioned before using the following code:

code_snippet1

It produces the following output:

code_snippet2

Which is totally different than what the original file is structured and how the image pairs should be matched which looks like this: training images:

imgs

Their corresponding masks:

masks

What I want to achieve is to have an img_list which stores the images in order e.g. ["1.jpg", "4.jpg", ...] and their corresponding masks in another list e.g. ["1_02_01.png", "4_02_01.png", ...] or any other way that could match each image to its corresponding first mask, doesn't necessarily have to be in the order that they are shown in the file explorer.

CodePudding user response:

The pathnames are sorted as strings. This can be confusing with numbers because 11 is sorted before 2. This is because the 1 character sorts before the 2 character in Unicode/ASCII. If we replace 1 and 2 with a and b, respectively, we would expect aa to be sorted before b.

That said, the sort function accepts a lambda that allows you to influence the sort order of your list. For example:

list = ["2", "11", "3", "1"]
list.sort() #=> ["1", "11", "2", "3"]
list.sort(key=lambda n: int(n)) #=> ["1", "2", "3", "11"]

Notice how the lambda doesn't transform the result. It transforms the value for purposes of sorting. The values remain strings.

  • Related