Can somebody explain me this method? CNN Keras - starter
-
29-11-2019 - |
Question
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?
Solution
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
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange