0
$\begingroup$
def ReadImages(Path):
    LabelList = list()
    ImageCV = list()
    classes = ["nonPdr", "pdr"]

    FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]

    for File in FolderList:
        for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
            ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (224,224)))
            LabelList.append(classes.index(os.path.splitext(File)[0])) 
            print(FolderList)
    return ImageCV, LabelList

I got this method, and I'm not understanding properly what it do line by line, can someone help me?

$\endgroup$

2 Answers 2

1
$\begingroup$
def ReadImages(Path):  #path to dir with all images
    LabelList = list() #Initialized empty labels list
    ImageCV = list()   #Initialized empty ImageCV list initialized
    classes = ["nonPdr", "pdr"]  #classes labels all image files startwith
    #return list of image folders in main dir path/
    FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]

    #loop over dir of image folders
    for File in FolderList:
        #for loop returns index of each image in a image folder 
        #and the image name
        for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
            #Read image file resize it and append it to ImageCv list
            ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (224,224)))
            #return string of characters before dot 1 in file name 
            #return index from classes !!!dangerous line assumes all files 
            #of form nonPdr.* or pdr.*
            #append that index value either 1 or 0 to libelist
            LabelList.append(classes.index(os.path.splitext(File)[0]))  
            print(FolderList) # print the folder just parsed
    return ImageCV, LabelList # return the two list created at the start
    #now containing images and labels based on names of image files
$\endgroup$
1
  • $\begingroup$ Why when I run print(File) it returns "nonPDR" or "PDR" $\endgroup$
    – user84391
    Commented Nov 14, 2019 at 11:49
0
$\begingroup$

Answers will be put together as comments on your code

def ReadImages(Path):
    LabelList = list() ## Get empty list for labels
    ImageCV = list() #Empty list for images
    classes = ["nonPdr", "pdr"]

    FolderList = [f for f in os.listdir(Path) if not f.startswith('.')] 
    #Collect names of all folders within path, ignore those files that starts with dot

    for File in FolderList:
        # The structure of the directory is assumed to be : Path contains folders and images are contained within these folders.
        # We iterate for each image within the each folder
        for index, Image in enumerate(os.listdir(os.path.join(Path, File))):

            # We read and resized to (224,224) and store it in our list 
            # The file will be read from "Path/File/(image file name).(image extensions)"
            ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (224,224)))

            # After that labels for each image is determined by its filename (if nonPdr label is 0, if filename is Pdr label is 1)
            LabelList.append(classes.index(os.path.splitext(File)[0])) 
            print(FolderList)

    #Return Result
    return ImageCV, LabelList
$\endgroup$
2
  • $\begingroup$ Why when I run print(File) it returns "nonPDR" or "PDR" $\endgroup$
    – user84391
    Commented Nov 14, 2019 at 11:50
  • $\begingroup$ What do you expect it to be? We don't know how the folder name is, but File is supposed to be members of FolderList so that means that the folder you are iterating from is named nonPDR and PDR. $\endgroup$ Commented Nov 14, 2019 at 12:43

Your Answer

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