-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
93 lines (59 loc) · 2.58 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Helper functions
import os
import glob # library for loading images from a directory
import matplotlib.image as mpimg
import cv2
# 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 = ["day", "night"]
# 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
## Standardize the input images
# Resize each image to the desired input size: 600x1100px (hxw).
## Standardize the output
# With each loaded image, we also specify the expected output.
# For this, we use binary numerical values 0/1 = night/day.
# This function should take in an RGB image and return a new, standardized version
# 600 height x 1100 width image size (px x px)
def standardize_input(image):
# Resize image and pre-process so that all "standard" images are the same size
standard_im = cv2.resize(image, (1100, 600))
return standard_im
# Examples:
# encode("day") should return: 1
# encode("night") should return: 0
def encode(label):
numerical_val = 0
if(label == 'day'):
numerical_val = 1
# else it is night and can stay 0
return numerical_val
# using both functions above, standardize the input images and output labels
def standardize(image_list):
# Empty image data array
standard_list = []
# Iterate through all the image-label pairs
for item in image_list:
image = item[0]
label = item[1]
# Standardize the image
standardized_im = standardize_input(image)
# Create a numerical label
binary_label = encode(label)
# Append the image, and it's one hot encoded label to the full, processed list of image data
standard_list.append((standardized_im, binary_label))
return standard_list