-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport_label_data.py
executable file
·196 lines (172 loc) · 6.94 KB
/
import_label_data.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import os
import pandas as pd
from PIL import Image
##The following functions allow importing:
## all datasets with: import_all(scaleIn, scaleOut)
## single dataset with: import_single(scaleIn, scaleOut, dataName)
## multiple datasets with: import_multiple(scaleIn, scaleOut, dataNameList)
global root_dir_data, list_dir, DataNameList, NameListLength
# Directory to the .png data sets
root_dir_data = os.path.abspath('../Dehazing_Datasets')
# Directory to list of image names
list_dir = os.path.abspath('../neural_net/list')
# Check for existence of firectories
os.path.exists(root_dir_data)
os.path.exists(list_dir)
DataNameList = os.listdir(root_dir_data)
NameListLength = np.size(DataNameList)
#Assign labels to each dataset
def assign_label(a):
if a=="Dataset Kelp":
label = 0
elif a=="Dataset Rocks":
label = 1
elif a=="Dataset Rocks-Sand":
label = 2
elif a=="shallowCorals":
label = 3
elif a=="mediumCorals":
label = 4
elif a=="deepCorals":
label = 5
return label
# Class for easy return of image matrices
class ReturnValue(object):
def __init__(self, train_x, train_y, test_x, test_y, testImgs_x, testImgs_y):
self.train_x = train_x
self.train_y = train_y
self.test_x = test_x
self.test_y = test_y
self.testImgs_x = testImgs_x
self.testImgs_y = testImgs_y
print "Please wait. Importing image data..."
#Populating the vectors with all the data sets of images
def import_all(scaleIn, scaleOut):
global root_dir_data, list_dir, DataNameList, NameListLength
i = 0
train_x = []
train_y = []
test_x = []
test_y = []
testImgs_x = []
testImgs_y = []
while i < NameListLength:
data_dir_in = os.path.join(root_dir_data, DataNameList[i], 'proc')
train = pd.read_csv(os.path.join(list_dir, 'train', DataNameList[i] + "." + "csv"))
test = pd.read_csv(os.path.join(list_dir, 'test', DataNameList[i] + "." + "csv"))
for img_name in train.name:
image_path = os.path.join(data_dir_in, img_name)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn, img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
train_x.append(rsizeArr)
for img_name in train.name:
label = assign_label(DataNameList[i])
train_y.append(label)
for img_name in test.name:
image_path = os.path.join(data_dir_in, img_name)
testImgs_x.append(image_path)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn,img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
test_x.append(rsizeArr)
for img_name in test.name:
label = assign_label(DataNameList[i])
testImgs_y.append(image_path)
test_y.append(label)
print "{0} successfully imported!".format(DataNameList[i])
i+=1
train_x = np.stack(train_x)
train_y = np.stack(train_y)
test_x = np.stack(test_x)
test_y = np.stack(test_y)
testImgs_x = np.stack(testImgs_x)
testImgs_y = np.stack(testImgs_y)
return ReturnValue(train_x, train_y, test_x, test_y, testImgs_x, testImgs_y)
#Populating the vectors with only one dataset
def import_single(scaleIn, scaleOut, DataName):
global root_dir_data, list_dir
i = 0
train_x = []
train_y = []
test_x = []
test_y = []
testImgs_x = []
testImgs_y = []
data_dir_in = os.path.join(root_dir_data, DataNameList, 'proc')
train = pd.read_csv(os.path.join(list_dir, 'train', DataNameList + "." + "csv"))
test = pd.read_csv(os.path.join(list_dir, 'test', DataNameList + "." + "csv"))
for img_name in train.name:
image_path = os.path.join(data_dir_in, img_name)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn, img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
train_x.append(rsizeArr)
for img_name in train.name:
label = assign_label(DataNameList[i])
train_y.append(label)
for img_name in test.name:
image_path = os.path.join(data_dir_in, img_name)
testImgs_x.append(image_path)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn,img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
test_x.append(rsizeArr)
for img_name in test.name:
label = assign_label(DataNameList[i])
testImgs_y.append(image_path)
test_y.append(label)
print "{0} successfully imported!".format(DataNameList[i])
i+=1
train_x = np.stack(train_x)
train_y = np.stack(train_y)
test_x = np.stack(test_x)
test_y = np.stack(test_y)
testImgs_x = np.stack(testImgs_x)
testImgs_y = np.stack(testImgs_y)
return ReturnValue(train_x, train_y, test_x, test_y, testImgs_x, testImgs_y)
#Populating the vectors with multiple sets of data
def import_multiple(scaleIn, scaleOut, DataNameList):
global root_dir_data, list_dir
i = 0
NameListLength = np.size(DataNameList)
train_x = []
train_y = []
test_x = []
test_y = []
testImgs_x = []
testImgs_y = []
while i < NameListLength:
data_dir_in = os.path.join(root_dir_data, DataNameList[i], 'proc')
train = pd.read_csv(os.path.join(list_dir, 'train', DataNameList[i] + "." + "csv"))
test = pd.read_csv(os.path.join(list_dir, 'test', DataNameList[i] + "." + "csv"))
for img_name in train.name:
image_path = os.path.join(data_dir_in, img_name)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn, img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
train_x.append(rsizeArr)
for img_name in train.name:
label = assign_label(DataNameList[i])
train_y.append(label)
for img_name in test.name:
image_path = os.path.join(data_dir_in, img_name)
testImgs_x.append(image_path)
img = Image.open(image_path) # Open image as PIL image object
rsize = img.resize((img.size[0]/scaleIn,img.size[1]/scaleIn)) # Use PIL to resize
rsizeArr = np.asarray(rsize) # Get array back
test_x.append(rsizeArr)
for img_name in test.name:
label = assign_label(DataNameList[i])
testImgs_y.append(image_path)
test_y.append(label)
print "{0} successfully imported!".format(DataNameList[i])
i+=1
train_x = np.stack(train_x)
train_y = np.stack(train_y)
test_x = np.stack(test_x)
test_y = np.stack(test_y)
testImgs_x = np.stack(testImgs_x)
testImgs_y = np.stack(testImgs_y)
return ReturnValue(train_x, train_y, test_x, test_y, testImgs_x, testImgs_y)