-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdata_handler.py
367 lines (291 loc) · 12.3 KB
/
data_handler.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import cv2, sys, os, shutil, csv
from random import randint, sample
import numpy as np
import pdb
class Process:
'''
functions:
OSWalk() = walks through directory and lists all the images in the directory
generateData(num) = selects a random set of 'num' images from the listed images
process(num, indices) = converts the selected 'num' images to 128*num samples
fetch(num) = selects a random set of 'num' samples from the produced samples using process()
helper functions:
getLabels() = walks through directory and populates the labels
getName(name) = converts ./path/to/here/img.png to here/img.png
reset() = resets the counters when you have used up all the images
numimages() = prints total number of images in the directory
numsamples() = prints total number of samples generated from selected images
remimages() = prints total number of images that remain in the directory
remsamples() = prints total number of samples that remain from the selected lot of images
'''
def __init__(self, location, height = 224, width = 224, depth = 3):
self.img2labels = dict()
self.idx2img = dict()
def __init__(self, location, file = None, flag = False, height = 224, width = 224, depth = 3):
self.img2labels = dict()
self.idx2img = dict()
#self.num_crops=1
self.location = location
self.height = height
self.width = width
self.depth = depth
self.numImages = 0
self.numSamples = 0
self.imageLocs = []
self.genNum = 128
self.sampleImages = []
self.remImages = []
self.remSamples = []
# flag for processing data from NCLT / Cambridge
self.flag = flag
self.file = file
# OS Walk for getting all image files
self.OSWalk()
# print number of images
self.numimages()
# populate labels dict
self.getLabels()
def OSWalk(self):
images = [os.path.join(root, name) for root, dirs, files in os.walk(self.location)
for name in files if name.endswith((".png", ".jpg", ".jpeg", ".gif"))]
self.imageLocs = images
for i in range(len(images)):
a,b = images[i].split('\\')
self.imageLocs[i] = a + '/' + b
# populate labels dict
# False implies Cambridge Dataset, True is NCLT
if self.flag == False:
self.getLabels()
else:
self.getGround()
def OSWalk(self):
images = [os.path.join(root, name) for root, dirs, files in os.walk(self.location + 'seq/')
for name in files if name.endswith((".png", ".jpg", ".jpeg", ".gif", ".tiff"))]
self.imageLocs = images
# for i in range(len(images)):
# a,b = images[i].split('\\')
# self.imageLocs[i] = a + '/' + b
if self.flag==False:
for i in range(len(images)):
a,b = images[i].split('/')
self.imageLocs[i] = a + '/' + b
else:
for i in range(len(images)):
self.imageLocs[i] = images[i]
# array to store which image has been used
self.numImages = len(self.imageLocs)
self.remImages = np.ones((self.numImages), dtype = bool)
def generateData(self, num):
ctr = 0
indices = ['']*num
rem = np.sum(self.remImages)
# Pick random images
if rem < num:
print('Number of Images left to select: ', rem)
print('Generating samples for the remaining images')
num = min(rem, num) # only activated when the number of images left is less than the request
while ctr<num:
idx = randint(0,self.numImages-1)
if self.remImages[idx]==True:
self.remImages[idx] = False
indices[ctr] = self.imageLocs[idx]
ctr += 1
# array to store which sample has been used
self.numSamples = self.genNum*num
self.remSamples = np.ones((self.numSamples), dtype = bool)
# generate samples
self.process(num, indices)
def process(self, num, indices):
print('Generating Samples .... ')
self.sampleImages = np.zeros((num*128, self.height, self.width, self.depth), dtype=np.uint8)
idx = 0
for i in range(num):
# read image
img = cv2.resize(cv2.imread(indices[i], 1), (455,256), interpolation = cv2.INTER_CUBIC)
name = self.getName(indices[i])
# generate 128 random indices for crop
for j in range(idx, idx+128):
x = randint(0,31)
y = randint(0,230)
self.sampleImages[j, :, :, :] = img[x:x + self.height, y:y + self.width, :].copy()
self.idx2img[j] = name
idx += 128
def centeredCrop(self, img, output_side_length):
height, width, depth = img.shape
new_height = output_side_length
new_width = output_side_length
if height > width:
new_height = output_side_length * height / width
else:
new_width = output_side_length * width / height
height_offset = (new_height - output_side_length) / 2
width_offset = (new_width - output_side_length) / 2
cropped_img = img[height_offset:height_offset + output_side_length,
width_offset:width_offset + output_side_length]
return cropped_img
def process(self, num, indices):
print('Generating Samples .... Note: no img normalization ')
self.sampleImages = np.zeros((num*self.genNum, self.height, self.width, self.depth), dtype=np.uint8)
idx = 0
imgs = np.zeros((num, 256, 455, 3))
names = ['' for i in range(num)]
for i in range(num):
# read image
img = cv2.resize(cv2.imread(indices[i], 1).astype(float), (455,256), interpolation = cv2.INTER_CUBIC)
name = self.getName(indices[i])
if (name in self.img2labels ):
names[i] = name
imgs[i,:,:,:] = img
means = np.mean(imgs, axis=0)
imgs = imgs - means
#temp_mean = np.mean(img, axis=0)
#temp_mean = np.mean(temp_mean, axis=0)
#temp_std = np.zeros(self.depth)
#for i in range(self.depth):
# img[:,:,i] -= temp_mean[i]
# temp_std[i] = np.std(img[:,:,i])
# img[:,:,i] /= temp_std[i]
for i in range(num):
# rotate image if NCLT
img = imgs[i, :,:,:]
if self.flag==True:
rows, cols, _ = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
img = cv2.warpAffine(img,M,(cols,rows))
# generate 128 random indices for crop
for j in range(idx, idx + self.genNum):
x = randint(0,31)
y = randint(0,230)
self.sampleImages[j, :, :, :] = img[x:x + self.height, y:y + self.width, :].copy()
try:
self.idx2img[j] = names[i]
except IndexError:
pdb.set_trace()
idx += self.genNum
for i in range(self.numImages):
if self.remImages[i] == False:
self.store(self.imageLocs[i])
def fetch(self, num):
samples = np.zeros((num, self.height, self.width, self.depth), dtype=np.float)
labels = ['']*num
ctr = 0
flag = True
# number of remaining samples
rem = np.sum(self.remSamples)
if rem<num:
flag = False
print("Number of samples left to select: ", rem)
else:
while ctr<num:
idx = randint(0,self.numSamples-1)
if self.remSamples[idx]==True:
self.remSamples[idx] = False
# gaussian normalization of image to have mean 0, variance 1
temp = self.sampleImages[idx, :, :, :].astype(float)
temp_mean = np.mean(temp, axis=0)
temp_mean = np.mean(temp_mean, axis=0)
temp_std = np.zeros(self.depth)
for i in range(self.depth):
temp[:,:,i] -= temp_mean[i]
temp_std[i] = np.std(temp[:,:,i])
temp[:,:,i] /= temp_std[i]
# assign sample and labels
samples[ctr,:,:,:] = temp
labels[ctr] = self.img2labels[self.idx2img[idx]]
if self.idx2img[idx] in self.img2labels.keys():
labels[ctr] = self.img2labels[self.idx2img[idx]]
else:
labels[ctr] = self.img2labels[self.idx2img[idx]+1]
samples[ctr,:,:,:] = self.sampleImages[idx, :, :, :].astype(float)
# gaussian normalization of image to have mean 0, variance 1
ctr += 1
return [flag, samples, labels]
def getLabels(self):
f1 = open(self.location+'dataset_test.txt')
lines = f1.readlines()
f2 = open(self.location+'dataset_train.txt')
lines2 = f2.readlines()
# append the total list and process labels
lines.extend(lines2)
numLines = len(lines)
for i in range(numLines):
line = lines[i].strip()
line = line.split()
self.img2labels[line[0]] = list(map(float,line[1:8]))
def getName(self, loc):
ctr = 0
for i in range(len(loc)):
if loc[i]=='/':
ctr += 1
if ctr==2:
return loc[i+1:]
def getGround(self):
filename = self.location + self.file
odom = np.loadtxt(filename, delimiter = ",")
length, _ = odom.shape
# the number we have to divide by to get a correct association
param = 4
for i in range(length):
name = int(odom[i, 0].item()/10**param)
x = float(odom[i, 1])
y = float(odom[i, 2])
z = float(odom[i, 3])
r = float(odom[i, 4])
p = float(odom[i, 5])
h = float(odom[i, 6])
data = [x,y,z,r,p,h]
self.img2labels[name] = data
def getName(self, loc):
if self.flag == False:
ctr = 0
for i in range(len(loc)):
if loc[i]=='/':
ctr += 1
if ctr==2:
return loc[i+1:]
else:
params = loc.split('/')
name = params[-1][:-9]
return int(name)
def store(self, image):
name = self.getName(image)
location = self.location + 'usedImages/'
# copy image to folder
if os.path.exists(location):
shutil.rmtree(location)
if not os.path.exists(location):
os.makedirs(location)
shutil.copy(image, location + str(name) + '.tiff')
# write image with labels to folder
file = location + 'trainingSet.csv'
csv = open(file, "a")
if name in self.img2labels.keys():
x = str(self.img2labels[name][0])
y = str(self.img2labels[name][1])
z = str(self.img2labels[name][2])
r = str(self.img2labels[name][3])
p = str(self.img2labels[name][4])
h = str(self.img2labels[name][5])
else:
name += 1
x = str(self.img2labels[name][0])
y = str(self.img2labels[name][1])
z = str(self.img2labels[name][2])
r = str(self.img2labels[name][3])
p = str(self.img2labels[name][4])
h = str(self.img2labels[name][5])
row = str(name) + ',' + x + ',' + y + ',' + z + ',' + r + ',' + p + ',' + h + '\n'
csv.write(row)
def reset(self):
self.remImages = np.ones((self.numImages), dtype = bool)
def numimages(self):
print('The total number of images are: ', self.numImages)
return self.numImages
def numsamples(self):
print('The total number of samples are: ', self.numSamples)
def remsamples(self):
print('The number of samples that remain are: ',np.sum(self.remSamples))
return self.remSamples
def remimages(self):
print('The number of images that remain are: ',np.sum(self.remImages))
return self.remImages