-
Notifications
You must be signed in to change notification settings - Fork 2
/
klgparser.py
executable file
·329 lines (269 loc) · 10.3 KB
/
klgparser.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
#!/usr/bin/python
'''
File name: klgparser.py
Based on code by Neil Ibrahimli
'''
__author__ = "Carlos Beltran-Gonzalez"
__copyright__ = "Copyright 2017, Carlos Beltran-Gonzalez"
__credits__ = "Neil Ibrahimli"
__licence__ = "GPL"
__version__ = "3.0"
__maintainer__ = "Carlos Beltran-Gonzalez"
__email__ = "[email protected]"
import numpy as np
import cv2
import zlib
import Image
import filecmp
import unittest
import os
import shutil
import sys, getopt
import bitstring
class TestKLGParser(unittest.TestCase):
def __init__(self, testName, outputfolder, testfolder, framesrange):
super(TestKLGParser, self).__init__(testName)
self.outputfolder = outputfolder
self.testfolder = testfolder
self.framesrange = framesrange
def testDepthOutput(self):
_outputfolder = self.outputfolder
_testfolder = self.testfolder
testindx = 0 #Test index always starts at 0
for indx in self.framesrange:
filename = "depth_aug" + str(indx) +".png"
filenametest = "depth_aug" + str(testindx) +".png"
testindx+=1;
file1 = _outputfolder + filename
file2 = _testfolder + filenametest
self.assertTrue(filecmp.cmp(file1,file2,shallow=False))
def testRGBOutput(self):
_outputfolder = self.outputfolder
_testfolder = self.testfolder
testindx = 0 #Test index always starts at 0
for indx in self.framesrange:
filename = "rgb_aug" + str(indx) + ".png"
filenametest = "rgb_aug" + str(testindx) + ".png"
testindx+=1;
file1 = _outputfolder + filename
file2 = _testfolder + filenametest
self.assertTrue(filecmp.cmp(file1,file2,shallow=False))
def checkCreateOutputFolder(folder):
if not os.path.exists(folder):
os.makedirs(folder)
def removeFolder(folder):
if os.path.exists(folder):
shutil.rmtree(folder)
def klg2png(inputfile,firstframe,lastframe, outputfolder):
f = open(inputfile, "rb")
# extract number of frames (32 bits)
byte = f.read(4)
a=map(ord,byte)
numberofframes= a[3]*256*256*256+a[2]*256*256+a[1]*256+a[0]
count=0
# initialize images matrices
depth = np.ones( (480,640), dtype=np.uint16)
rgb = np.ones( (480,640,3), dtype=np.uint8)
#numberofframes = 10 #TOFIX: hack for testing
while count < lastframe:
print count
#reading timestamp
byte = f.read(8)
a=map(ord,byte)
#reading depthsize
byte = f.read(4)
a=map(ord,byte)
depthsize = a[3]*256*256*256+a[2]*256*256+a[1]*256+a[0]
#reading imagesize
byte = f.read(4)
a=map(ord,byte)
imagesize = a[3]*256*256*256+a[2]*256*256+a[1]*256+a[0]
print imagesize
#extracting depth image
byte = f.read(depthsize)
if count >= firstframe and count < lastframe:
dimage = zlib.decompress(byte)
depth = np.fromstring(dimage,dtype=np.uint16)
depth = depth.byteswap()
depth.shape = (480,640)
#a=map(ord,dimage)
#for i in range(len(a)-1):
# depth[(i/1280)][(i%1280)/2]=a[i+1]*256+a[i]
dname= outputfolder + "depth_aug"+str(count)+".png"
cv2.imwrite(dname,depth)
#extracting rgb image
byte = f.read(imagesize)
if count >= firstframe and count < lastframe:
timage = np.fromstring(byte, dtype=np.uint8)
rgb=cv2.imdecode(timage,1)
cname= outputfolder + "rgb_aug"+str(count)+".png"
cv2.imwrite(cname,cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB))
count+=1
f.close()
def klg2klg(inputfile,outputfile,firstframe=0,lastframe=0, step=1, undistort=True):
f = open(inputfile, "rb")
fout = open(outputfile, "wb")
# extract number of frames (32 bits)
byte = f.read(4)
numberofframes = bitstring.BitArray(bytes=byte,length=32).uintle
print "Original number of frames = ", numberofframes
if (lastframe > numberofframes or \
lastframe == 0):
lastframe = numberofframes
framesrange = range(firstframe,lastframe,step)
#dstnumofframes = lastframe - firstframe;
dstnumofframes = len(framesrange)
print "New number of frames = ", numberofframes
dstnumofframes = np.uint32(dstnumofframes);
# save to output file
fout.write(dstnumofframes);
count=0
# initialize images matrices
depth = np.ones( (480,640), dtype=np.uint16)
rgb = np.ones( (480,640,3), dtype=np.uint8)
#numberofframes = 10 #TOFIX: hack for testing
while count < lastframe:
#print count
#reading timestamp
bytetimestamp = f.read(8)
#reading depthsize
bytedepthsize = f.read(4)
depthsize = bitstring.BitArray(bytes=bytedepthsize,length=32).uintle
#reading imagesize
byteimagesize = f.read(4)
imagesize = bitstring.BitArray(bytes=byteimagesize,length=32).uintle
#extracting depth image
bytedepth = f.read(depthsize)
#extracting rgb image
byteimage = f.read(imagesize)
#if count >= firstframe and count < lastframe:
if count in framesrange:
if (undistort):
# copy parameters to arrays
# using parameters from the Kinect RBG calibration
K = np.array([[528.4692, 0., 313.7945], [0, 524.2487, 263.1390],[0, 0, 1]])
d = np.array([.2075, -0.3904, 0, 0, 0]) # just use first two terms
h, w = rgb.shape[:2]
newcamera, roi = cv2.getOptimalNewCameraMatrix(K, d, (w,h), 0)
# Undistort depth
dimage = zlib.decompress(bytedepth)
depth = np.fromstring(dimage,dtype=np.uint16)
depth = depth.byteswap()
depth.shape = (480,640)
u_depth = cv2.undistort(depth, K, d, None, newcamera)
u_depth = u_depth.byteswap()
u_bytedepth = zlib.compress(u_depth,9)
newsize = len(u_bytedepth)
bytedepthsize = np.uint32(newsize)
# Undistort RGB
timage = np.fromstring(byteimage, dtype=np.uint8)
rgb = cv2.imdecode(timage,1)
u_rgb = cv2.undistort(rgb, K, d, None, newcamera)
jpg = cv2.imencode('.jpg',u_rgb)[1].tostring()
newsize = len(jpg)
byteimagesize = np.uint32(newsize)
fout.write(bytetimestamp)
fout.write(bytedepthsize)
fout.write(byteimagesize)
if (undistort):
fout.write(u_bytedepth)
fout.write(jpg)
else:
fout.write(bytedepth)
fout.write(byteimage)
count+=1
f.close()
def Test():
klg2png_output = "klg2png_output/"
klg2png_output_test = "output_test/"
dynfolder = "dyn_output/"
dyntestfolder = "dyn_test/"
# Preparing the environment
removeFolder(klg2png_output);
checkCreateOutputFolder(klg2png_output)
removeFolder(dyntestfolder);
checkCreateOutputFolder(dyntestfolder)
removeFolder(dynfolder);
checkCreateOutputFolder(dynfolder)
# Actuating algorihtms for first 10 frames
klg2klg("2017-08-01.00.klg","outklg.klg",0,10)
klg2png("outklg.klg",0,10,klg2png_output)
# Actuating algorihtms for frame interval
klg2png("2017-08-01.00.klg",50,60,dynfolder)
klg2klg("2017-08-01.00.klg","outklg.klg",50,60)
klg2png("outklg.klg",0,10,dyntestfolder)
# calling tests
suite = unittest.TestSuite()
suite.addTest(TestKLGParser('testDepthOutput', klg2png_output, klg2png_output_test, range(10)))
suite.addTest(TestKLGParser('testRGBOutput', klg2png_output, klg2png_output_test, range(10)))
suite.addTest(TestKLGParser('testDepthOutput', dynfolder, dyntestfolder, range(50,60)))
suite.addTest(TestKLGParser('testRGBOutput', dynfolder, dyntestfolder, range(50,60)))
unittest.TextTestRunner(verbosity=2).run(suite)
def printUse():
print 'klgparser.py -i <inputfile> -o <outputfile> -s <startframe> -e <endframe>'
print 'klgparser.py --ifile <inputfile> --ofile <outputfile> --fstar <startframe --fend <endframe>'
def printUseUndistort():
print 'klgparser.py -u -i <inputfile> -o <outputfile>'
print 'klgparser.py -u --ifile <inputfile> --ofile <outputfile>'
def printUseDecimation():
print 'klgparser.py -i <inputfile> -o <outputfile> -f fstep'
print 'klgparser.py --ifile <inputfile> --ofile <outputfile> -f fstep'
def main(argv):
inputfile = ''
outputfile = ''
fstart = ''
fend = ''
try:
opts, args = getopt.getopt(argv,"htpuf:i:o:s:e:",["ifile=","ofile=","fstart=","fend="])
except getopt.GetoptError:
printUse()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printUse()
sys.exit()
elif opt == '-t':
print 'Testing'
Test()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt == '-p':
print "Tranforming to png"
sys.exit()
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in '-u':
if not inputfile or \
not outputfile:
printUseUndistort()
sys.exit()
print "Generating undirstoted klg"
sys.exit()
elif opt in ("-s", "--fstart"):
fstart = arg
elif opt in ("-e", "--fend"):
fend = arg
elif opt in '-f':
if not inputfile or \
not outputfile:
printUseDecimation()
sys.exit()
fstep = arg
print ("Generating decimated klg with frames step:" + str(fstep))
klg2klg(inputfile,outputfile,step=int(fstep))
sys.exit()
if not inputfile or \
not outputfile or \
not fstart or \
not fend:
printUse()
sys.exit()
print "Converting file"
klg2klg(inputfile,outputfile,int(fstart),int(fend))
print "Done"
#removeFolder("finaltest");
#checkCreateOutputFolder("finaltest")
#klg2png(outputfile,0,int(fend)-int(fstart),"finaltest/")
if __name__ == "__main__":
main(sys.argv[1:])