-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
136 lines (117 loc) · 5.24 KB
/
app.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
"""
Author: Harshad Reddy Nalla
Python version: 2.7
"""
import os, sys, inspect, thread, time
from feature_extractor import Feature_Extractor
from ml_model import ML_Model
import Leap,json
import speech
import numpy as np
src_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
arch_dir = '../lib/x64' if sys.maxsize > 2 ** 32 else '../lib/x86'
sys.path.insert(0, os.path.abspath(os.path.join(src_dir, arch_dir)))
sys.path.append("..\LeapDeveloperKit_3.2.1+45911_win\LeapSDK\lib\x86")
def write_json(data,k):
# store the data generated by the leap motion sensor
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
print "DONE!"
print "Start proccess"
# Execute the normalization and feature extraction
ex = Execute('data.txt')
value = ex.writecsv_cluster()
print "input file",value
pr = PR(value)
# Feed the input to the trained machine learning model and gather the result from the model
result = pr.run()
# The result is displayed on the html page
with open('index.html', 'w') as f:
f.write(
'<html><head><meta http-equiv="refresh" content="1"/></head><body><h1 style="text-align:center;font-size:300px">%s</h1></body></html>' % result)
# Return the result through speech api.
speech.say(result)
class SampleListener(Leap.Listener):
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
final_json=[]
count=0
check=False
def on_connect(self, controller):
print "Connected"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
frame = controller.frame()
fps = frame.current_frames_per_second
print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % (
frame.id, frame.timestamp, len(frame.hands), len(frame.fingers))
one_frame=[]
# Get hands
for hand in frame.hands:
handType = "Left hand" if hand.is_left else "Right hand"
print " %s, id %d, position: %s" % (
handType, hand.id, hand.palm_position)
# Get the hand's normal vector and direction
hand_val={}
normal = hand.palm_normal
direction = hand.direction
hand_val["normal"]=[hand.palm_normal[i] for i in range(3)]
hand_val["position"] = [hand.palm_position[i] for i in range(3)]
hand_val["velocity"] = [hand.palm_velocity[i] for i in range(3)]
hand_val["confidence"] = hand.confidence
hand_val["pinch_strength"] = hand.pinch_strength
hand_val["sphere_center"] = [hand.sphere_center[i] for i in range(3)]
hand_val["sphere_radius"]=hand.sphere_radius
hand_val["grab_strength"] = hand.grab_strength
# Calculate the hand's pitch, roll, and yaw angles
print " pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
direction.pitch * Leap.RAD_TO_DEG,
normal.roll * Leap.RAD_TO_DEG,
direction.yaw * Leap.RAD_TO_DEG)
# Get arm bone
arm = hand.arm
print " Arm direction: %s, wrist position: %s, elbow position: %s" % (
arm.direction,
arm.wrist_position,
arm.elbow_position)
# Get fingers
# finger.JOINT_PIP = 1
# finger.JOINT_MCP = 0
all_finger={}
for finger in hand.pointables:
finger=Leap.Finger(finger)
finger_val = {}
finger_val["finger"] = finger.id-hand.id*10+1
finger_val["direction"] = [finger.direction[i] for i in range(3)]
finger_val["length"] = finger.length
finger_val["tipPosition"] = [finger.tip_position[i] for i in range(3)]
finger_val["tipVelocity"] = [finger.tip_velocity[i] for i in range(3)]
finger_val["dipPosition"] = [finger.joint_position(2)[i] for i in range(3)]
finger_val["pipPosition"] = [finger.joint_position(1)[i] for i in range(3)]
finger_val["mcpPosition"] = [finger.joint_position(0)[i] for i in range(3)]
all_finger[finger_val["finger"]]=finger_val
all_finger["6"]=hand_val
if all_finger:
self.final_json.append(all_finger)
if len(self.final_json)==60:
self.count += 1
write_json([{"label":"BAD","data":self.final_json}],self.count)
self.final_json=[]
if not frame.hands.is_empty:
print ""
def main():
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)
# Keep this process running until Enter is pressed
print "Press Enter to quit..."
try:
sys.stdin.readline()
except KeyboardInterrupt:
pass
finally:
# Remove the sample listener when done
controller.remove_listener(listener)
if __name__ == "__main__":
main()