-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateDepthImages.py
More file actions
231 lines (197 loc) · 7.41 KB
/
createDepthImages.py
File metadata and controls
231 lines (197 loc) · 7.41 KB
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
#!/usr/bin/python
import os
import sys
import math
from DepthImage import *
trace_level = 0
debug_level = 1
info_level = 2
warn_level = 3
error_level = 4
#Set True to print logging, False to quiet
log_level = info_level
write_debug_map = False
pixelDelimiter = ' '
image_width = 500
image_height = 500
current_x_index = 0
#Walk the data directory looking for all composite.json files
def buildDirectoryList(root):
dirList = []
for dirName, subdirList, fileList in os.walk(root):
debug('Found directory: %s' % dirName)
for fname in fileList:
if fname == 'composite.json' :
dirList.append(dirName)
return dirList
#Figure out the univers of possible objects for this composite.
#We'll use the query file since it's small to parse
def buildObjectList(dirName) :
trace('Bulding object list for: ' + dirName)
objSet = set()
qfin = open(os.path.join(dirName, 'query.json'), 'r')
for line in qfin :
idx = line.find('+')
trace( 'line: ' + line )
#We found our starting point ( '+' )
#Start parsing the object lists
if idx != -1 :
#Skip the leading "
token = line[1:idx]
trace('token: ' + token)
#if not token, we have the +
if not token :
trace('We have the +')
objSet.add('+')
elif (not token.isspace()) :
trace( 'Token is not empty' )
#Add found tokens to the set of possible
for c in token :
trace( 'c: ' + c )
objSet.add(c)
return objSet
#Create the list of possible output file names for this directory
def createOutputFiles(dirName, objSet) :
outfileList = dict()
for o in objectSet :
trace( 'create file name for: ' + o)
fname = 'depthImage' + o + '.png'
fullPath = os.path.join(dirName, fname)
trace('fullPath: ' + fullPath)
i = DepthImage(image_width, image_height, fullPath)
#Create the file in write mode so we always start clean
outfileList[o] = i
return outfileList
#Given a composite.json file, parse its tokens to create depth
def parseCompositeFile(dirName) :
cfin = open(os.path.join(dirName, 'composite.json'), 'r')
debug( 'Parsing ' + os.path.join(dirName, 'composite.json') )
#Iterate the lines in composite.json
searchToken = 'pixel-order'
for line in cfin :
idx = line.find(searchToken)
#We found the starting point, pixel-order
if idx != -1 :
debug( 'Found the starting point, \'' + searchToken + '\'')
line = line[idx+len(searchToken):]
#Below is to clean off the ": " from the line
return line.translate(None, ":\" ")
#Given a composites contents, write out the depths for each token
def writeCompositeTokens(line, outfileDict) :
objCountDict = dict()
tokensWritten = 0
numTokens = 0
plusTokens = 0
objTokens = 0
debug( 'Writing composite tokens, line: ' + line[0:20] )
idx = line.find('+')
while idx != -1 :
token = line[0:idx]
line = line[idx+1:]
trace('\ntoken: ' + token)
trace('new line: ' + line[0:40])
trace('idx: ' + str(idx))
if token and token[0] == '@' :
tokensWritten += int(token[1:])
numTokens += int(token[1:])
trace(token[1:] + ' tokensWritten: ' + str(tokensWritten))
writeBackgroundPixel(token[1:], outfileDict)
elif token :
tokensWritten += 1
objTokens += 1
default = objCountDict.setdefault(token, 0)
trace('token ' + token + ' has default ' + str(default))
objCountDict[token] = (default + 1)
trace('obj tokensWritten: ' + str(tokensWritten))
writeDepthValues(token, outfileDict)
if line[0] == '+' :
plusCount = 0
while line[plusCount] == '+' :
plusCount = plusCount + 1
line = line[plusCount:]
trace('plusCount: ' + str(plusCount))
trace('plus line: ' + line[0:20])
tokensWritten += int(plusCount)
plusTokens += int(plusCount)
trace('+ tokensWritten: ' + str(tokensWritten))
writeBackgroundPixel(plusCount, outfileDict)
idx = line.find('+')
trace( 'Wrote out ' + str(tokensWritten) + ' pixels' )
trace( 'Wrote out ' + str(numTokens) + ' numTokens' )
trace( 'Wrote out ' + str(plusTokens) + ' plusTokens' )
trace( 'Wrote out ' + str(objTokens) + ' objTokens' )
#The following block writes out a file that is similar to the query.json
#It is useful for debugging when the counts are off.
if write_debug_map :
tout = open('tokenCounts', 'w')
tout.write('{\n\"dimensions\": [500, 500],\n')
tout.write('\"counts\": {\n')
tout.write('\"+\" : 61164')
for key in sorted(objCountDict) :
tout.write(',\n')
tout.write('\"' + key + '+\" : ' + str(objCountDict[key]))
tout.write('\n}\n}')
tout.close()
#Write out a 1 for each file, numPixels times
def writeBackgroundPixel(numPixels, outfileDict):
trace('Writing out 1 for ' + str(numPixels) + ' pixels')
for key in outfileDict :
f = outfileDict[key]
outValue = 1.0
if key == '+' :
outValue = 0.0
trace('bg pixel for +: %d' % outValue)
f.addRowData(outValue, numPixels)
#Write out the depth for all values in token
def writeDepthValues(token, outfileDict) :
trace( 'writeDepthValues token: ' + token )
numTokens = len(outfileDict)
length = len(token)
trace('token len: ' + str(length))
increment = 1/float(numTokens)
trace('increment ' + str(increment) + ' for ' + str(numTokens) + ' objects')
for key in outfileDict :
f = outfileDict[key]
idx = token.find(key)
outVal = 1
if idx != -1:
outVal = idx*increment
elif key == '+':
outVal = length*increment
trace( 'Writing val ' + str(outVal) + ' for object ' + key )
trace('Value: ' + str(outVal) + ' Quantized 8-bit: ' + str(int(math.floor(outVal*255))) + ' Quantized 16-bit: ' + str(int(math.floor(outVal*65535))) )
f.addRowData(outVal, 1)
def writeImageFiles(outfileDict) :
for key in outfileDict :
f = outfileDict[key]
f.writeFile()
#Helper function to abstract logging and easily disable
def debug(msg) :
if (log_level <= debug_level) :
print( msg )
#Helper function to abstract logging and easily disable
def trace(msg) :
if (log_level <= trace_level) :
print( msg )
#Helper function to abstract logging and easily disable
def info(msg) :
if (log_level <= info_level) :
print( msg )
def error(msg) :
if (log_level <= error_level) :
print( msg )
#Main
if __name__ == '__main__':
if len(sys.argv) != 2 :
error('You must 1 arg, the path to the data directory to work with')
sys.exit(2)
directoryList = buildDirectoryList(str(sys.argv[1]))
debug( 'directoryList size: ' + str(len(directoryList)) )
#for each directory
for d in sorted(directoryList) :
info( 'Processing directory: ' + d )
objectSet = buildObjectList(d)
outfileDict = createOutputFiles(d, objectSet)
line = parseCompositeFile(d)
writeCompositeTokens(line, outfileDict)
writeImageFiles(outfileDict)