-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataExtractor.py
64 lines (52 loc) · 1.73 KB
/
dataExtractor.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
import sys, math
import base64
import Leap
import ctypes
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
def loadData(filename):
f = open(filename, 'r')
frames = []
for data in f.readlines():
dataDecoded = base64.b64decode( data )
frame = Leap.Frame()
leap_byte_array = Leap.byte_array(len(dataDecoded))
address = leap_byte_array.cast().__long__()
ctypes.memmove(address, dataDecoded, len(dataDecoded))
frame.deserialize((leap_byte_array, len(dataDecoded)))
frames.append( frame )
return frames
def frameToVector(frame):
vector = []
hand = filter(lambda x: x.is_right, frame.hands)
if hand == []:
return None
hand = hand[0]
arm = hand.arm
for finger in hand.fingers:
for i in range(4):
bone = finger.bone(i)
pitch = bone.direction.pitch - arm.direction.pitch
yaw = bone.direction.yaw - arm.direction.yaw
vector.append(str(pitch))
vector.append(str(yaw))
return vector
def main():
if len(sys.argv) != 3:
print "USAGE: python2.7 dataExtractor.py <input.dat> <output.csv>"
return
inputFile = sys.argv[1]
outputFile = sys.argv[2]
frames = loadData(inputFile)
print "Read " + str(len(frames)) + " frames"
vectors = []
for frame in frames:
vector = frameToVector(frame)
if vector is None:
continue
vectors.append( ';'.join(vector) )
f = open(outputFile, 'w')
f.write('\n'.join(vectors))
f.close()
if __name__ == '__main__':
controller = Leap.Controller()
main()