-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleFestureTestSVM.py
73 lines (62 loc) · 2.63 KB
/
DoubleFestureTestSVM.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
import ctypes
import sys
import cv2
import numpy as np
from sklearn.externals import joblib
import Leap
svm_model = joblib.load("./models/svm_v3.pkl")
command_classes = ['Pointing', 'Capture', 'ZoomIn', 'ZoomOut', 'Roaming']
def run(controller):
capture_gesture = False
gesture_sequence = np.array([])
gesture = ''
while True:
frame = controller.frame()
image = frame.images[1]
if image.is_valid:
i_address = int(image.data_pointer)
ctype_array_def = ctypes.c_ubyte * image.height * image.width
as_ctype_array = ctype_array_def.from_address(i_address)
as_numpy_array = np.ctypeslib.as_array(as_ctype_array)
img = np.reshape(as_numpy_array, (image.height, image.width))
if capture_gesture:
for hand in frame.hands:
pv = []
av = []
prev_finger = None
c = hand.palm_position
m = 1
for finger in hand.fingers:
if prev_finger:
ad = finger.tip_position.distance_to(prev_finger.tip_position)
av.append(ad)
prev_finger = finger
else:
prev_finger = finger
pd = finger.tip_position.distance_to(c)
m = pd if finger.type == Leap.Finger.TYPE_MIDDLE else m
pv.append(pd)
gesture_sequence = np.append(gesture_sequence, np.array(pv) / m)
gesture_sequence = np.append(gesture_sequence, np.array(av) / m)
img = cv2.putText(img, '%s' % (gesture), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
if len(gesture_sequence) > 270:
prediction = svm_model.predict([gesture_sequence[:270]])
gesture = command_classes[int(prediction[0])]
print(gesture)
gesture_sequence = gesture_sequence[90:]
cv2.imshow('Image', img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('a'):
capture_gesture = not capture_gesture
print("Capturing gestures %s.." % ("finished", "started")[capture_gesture])
def main():
controller = Leap.Controller()
controller.set_policy_flags(Leap.Controller.POLICY_IMAGES)
try:
run(controller)
except KeyboardInterrupt:
sys.exit(0)
if __name__ == '__main__':
main()