-
Notifications
You must be signed in to change notification settings - Fork 1
/
downscaleImages.py
295 lines (235 loc) · 9.78 KB
/
downscaleImages.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
import os
import os.path
import sys
sys.dont_write_bytecode = True
import multiprocessing
from multiprocessing import Process, Queue
import time
from PIL import Image
import scipy.misc as misc
import numpy as np
import scipy.misc
import scipy.ndimage
from scipy.stats import norm
from classDefinitions import *
from random import random
#DEBUG = False
DEBUG = True
dirpath = list()
if(len(sys.argv) > 1):
for i in range(1, len(sys.argv)):
print sys.argv[i]
dirpath.append(sys.argv[i])
else:
dirpath.append("testfolder/")
MAX_ALLOWED_DEPTH = 600
MIN_AVG_DEPTH = 200
fromdirimg="color"
fromdirdep="depth"
todirimg="debug/color"
todirdep="debug/depth"
todirtarg="target"
coords_folder="coords"
output_folder="merged_out"
fromimgend=".png"
fromdepend=".npz"
toimgend=".png"
posoutend=".npy"
old_internal_name = "arr_0.npy"
original_image_size = (640, 480)
#image_size = np.array([64, 48])
#image_size = np.array([128, 96])
image_size = np.array([256, 192])
use_depth_channel = True
num_color_channels = 4 if use_depth_channel else 3
use_fake_distfunc = True
scalingFactor = np.divide(image_size.astype(np.float32), original_image_size)
loadFolders = ['Axis', 'Bearing', 'Bearing_Box', 'Distance_Tube', 'F20_20_B', 'F20_20_G', 'M20', 'M20_100', 'M30', 'Motor', 'R20', 'S40_40_B', 'S_40_40_G']
NUM_CLASSES = len(loadFolders)
NUM_CLASSES_AND_NOTHING = NUM_CLASSES + 1
def ensure_dir(file_path):
if not os.path.exists(file_path):
os.makedirs(file_path)
def handleSingleFile(f, subdirpath, cls):
colorFold = os.path.join(subdirpath, fromdirimg)
depthFold = os.path.join(subdirpath, fromdirdep)
debugColorOut = os.path.join(subdirpath, todirimg)
debugDepthOut = os.path.join(subdirpath, todirdep)
outputPath = os.path.join(os.path.dirname(subdirpath), output_folder)
ensure_dir(outputPath)
coordOutputPath = os.path.join(os.path.dirname(subdirpath), coords_folder)
ensure_dir(coordOutputPath)
fname = os.path.splitext(os.path.basename(f))[0]
iname = os.path.join(colorFold, f)
dname = os.path.join(depthFold, fname+fromdepend)
output_filename = os.path.join(outputPath, fname+toimgend)
posfilename = os.path.join(coordOutputPath, fname+posoutend)
if os.path.isfile(iname) and os.path.isfile(dname):
isAKeeper = True
#downsample image and save as numpy array
image = scipy.misc.imread(iname)
arrXd = np.array(image)
arr = arrXd[:,:,0:3] # handle alpha channel separately - has pos info encoded!
coordinateList = []
if arrXd.shape[2] == 4: #we have an alpha channel
alpha = arrXd[:,:,3]
foundPos = np.where(alpha != classes['unspecified'])
objectIndices = zip(*foundPos)
values = [alpha[i] for i in objectIndices]
coordinateList = zip(values, *foundPos)
#scale down cordinates by same ratio
coordinateList = [(c,y*scalingFactor[1],x*scalingFactor[0]) for (c,y,x) in coordinateList]#TODO 123 test test
#print(coordinateList) # a list of (type, x, y) tuples
else:
return
downscaled = scipy.misc.imresize(arr, (image_size[1], image_size[0]), 'lanczos')
#now convert and downsample depth image
dinp = np.load(dname)[old_internal_name]
#if(np.amax(dinp) >= MAX_ALLOWED_DEPTH): #sort out weird files with spikes in depth channel. (could probably also just clamp at 600, but meh)
# isAKeeper = False
dinp *= (255.0/MAX_ALLOWED_DEPTH) # scale from 0 to 600 to 0..255
#replace depth with zeros
#dinp = np.zeros(dinp.shape)
#replace depth with random:
#dinp = np.random.random_integers(0, 255, dinp.shape)
downscaledD = scipy.misc.imresize(dinp, (image_size[1], image_size[0]), 'lanczos')
avgDepthList = []
#for i in range(1, len(sys.argv)):
for idx in range(0, len(coordinateList)):
centerCoordinates = np.array([coordinateList[idx][1], coordinateList[idx][2]])
addedDepth = 0.0
countedPixels = 0
for y, x in np.ndindex(image_size[1], image_size[0]):
distance = np.linalg.norm(centerCoordinates-np.array((y, x)))
if distance < 15: # allows other classes not to be overwritten
ix = int(round(x))
iy = int(round(y))
dvalue = downscaledD[iy, ix]
if dvalue > 0:
addedDepth += dvalue
countedPixels += 1
avgDepth = MIN_AVG_DEPTH if countedPixels == 0 else addedDepth / countedPixels
if countedPixels != 0:
avgDepth *= (MAX_ALLOWED_DEPTH/255.0) # scale back from 0..255 to 0..600
avgDepth *= 0.1 # convert mm to cm
avgDepthList.append(avgDepth)
coordinateList = [(a,b,c,d) for (a,b,c),d in zip(coordinateList,avgDepthList)] # is now a 4-tuple, type, y, x, depth
#debug image output
if DEBUG:
ensure_dir(debugColorOut)
ensure_dir(debugDepthOut)
debugColorName = os.path.join(debugColorOut, fname+toimgend)
debugDepthName = os.path.join(debugDepthOut, fname+toimgend)
img = Image.fromarray(downscaled.astype(np.uint8))
img.save(debugColorName)
img = Image.fromarray(downscaledD.astype(np.uint8))
img.save(debugDepthName)
#add "additional" empty dimension to concat later
depthreshaped = np.reshape(downscaledD, ((image_size[1], image_size[0], 1)))
rgbd = downscaled
if(use_depth_channel):
rgbd = np.concatenate((downscaled, depthreshaped), axis=2)
if(not isAKeeper):
print('File '+posfilename+' contained undesired depth value, dropping...')
return
if(not np.all(np.isfinite(rgbd))): # should not be triggered since values are ints now
print('File '+posfilename+' contained NaN/inf, dropping...')
return
count = np.uint32(len(coordinateList))
#if count > 1:
# print('Warning, image '+posfilename+' has more than one object already! skipping...') #TODO remove this when multi obj scans are available
# return
if count < 1:
#print('Warning, image '+posfilename+' has no object defined! skipping...') #TODO eventually we want to allow empty lists as well, but for now, it sorts out images we haven't tagged
return
if use_fake_distfunc:
#mean = 0
#standard_deviation = 10
#valueAtZero = norm.pdf(0, mean, standard_deviation)
fake = np.zeros((image_size[1], image_size[0]))
fake.fill(classes['nothing']) #backdrop of "13"s - no obj here # TODO make it 0 at some point
for v,ty,tx,_ in coordinateList:
centerCoordinates = np.array([ty, tx])
for y, x in np.ndindex(image_size[1], image_size[0]):
distance = np.linalg.norm(centerCoordinates-np.array((y, x)))
#value = (norm.pdf(distance, mean, standard_deviation) / valueAtZero) * 255
if distance < 15: # allows other classes not to be overwritten
fake[y, x] = v
debugTargetOut = os.path.join(os.path.dirname(subdirpath), todirtarg)
ensure_dir(debugTargetOut)
debugTargetName = os.path.join(debugTargetOut, fname+toimgend)
img = Image.fromarray(fake.astype(np.uint8))
img.save(debugTargetName)
coordsfile = open(posfilename,"w")
#if(random() < 0.5):
# coordinateList.extend(coordinateList) #dirty hack to have more items in some cases, don't use - doesn't work on other end anyway, change to always X objects
coordinateList = np.array(coordinateList)
#print(coordinateList.shape) #check size
np.save(coordsfile, coordinateList)
img = Image.fromarray(rgbd.astype(np.uint8))
img.save(output_filename)
averagePixel = np.average(rgbd, axis=(0,1))
return averagePixel
def handleListOfFiles(filesToProcess, resultqueue):
result = {}
totalNumber = 0
summedPixel = np.zeros(num_color_channels)
for [f, subdirpath, cls] in filesToProcess:
meanPixel = handleSingleFile(f, subdirpath, cls)
if (meanPixel is not None):
totalNumber += 1
summedPixel += meanPixel
result['count'] = totalNumber
result['summedAveragePixel'] = summedPixel
resultqueue.put(result)
def chunker_list(seq, size):
return list(seq[i::size] for i in range(size))
def generateListOfFiles(thepath):
filesToProcess = []
for fold in os.listdir(thepath):
subdirpath = os.path.join(thepath, fold)
if not os.path.isdir(subdirpath):
continue
if(fold == coords_folder or fold == todirtarg or fold == output_folder or fold == "MultipleDuplicateObjects" or fold == "NoObjects"): # TODO added the second 2 for now # and now removed -> or fold == "MultipleObjects" <-
print('Skipping directory: ' + fold)
continue
print('Processing directory: ' + fold)
if fold in classes:
cls = np.uint8(classes[fold])
else:
cls = np.uint8(classes['unspecified'])
folderToLoadImagesFrom = os.path.join(subdirpath, fromdirimg)
for f in os.listdir(folderToLoadImagesFrom):
filesToProcess.append([f, subdirpath, cls])
return filesToProcess
def main():
filesToProcess = []
for folder in dirpath:
filesToProcess.extend(generateListOfFiles(folder))
numCores = multiprocessing.cpu_count()
print "splitting up", len(filesToProcess), "files into", numCores, "work chunks"
chunks = chunker_list(filesToProcess, numCores)
threads = []
results = [Queue() for x in range(numCores)]
index = 0
processedFiles = 0
meanPixel = np.zeros(num_color_channels)
for chunk in chunks:
t = Process(target=handleListOfFiles, args=(chunk,results[index]))
t.start()
threads.append(t)
index=index+1
time.sleep(0.1) # just so not all threads access files synchronously
print("joining in threads...")
for x in threads:
x.join()
print("joined all threads.")
for resultqueue in results:
result = resultqueue.get()
processedFiles += result['count']
meanPixel += result['summedAveragePixel']
meanPixel /= processedFiles
print "out of", len(filesToProcess), "files,", processedFiles,"converted. (", len(filesToProcess)-processedFiles , "skipped)"
print "average pixel:", meanPixel
if __name__ == "__main__":
main()