-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
35 lines (23 loc) · 1.11 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Helper functions
import os
import glob # library for loading images from a directory
import matplotlib.image as mpimg
# This function loads in images and their labels and places them in a list
# The list contains all images and their associated labels
# For example, after data is loaded, im_list[0][:] will be the first image-label pair in the list
def load_dataset(image_dir):
# Populate this empty image list
im_list = []
image_types = ["red", "yellow", "green"]
# Iterate through each color folder
for im_type in image_types:
# Iterate through each image file in each image_type folder
# glob reads in any image with the extension "image_dir/im_type/*"
for file in glob.glob(os.path.join(image_dir, im_type, "*")):
# Read in the image
im = mpimg.imread(file)
# Check if the image exists/if it's been correctly read-in
if not im is None:
# Append the image, and it's type (red, green, yellow) to the image list
im_list.append((im, im_type))
return im_list