forked from ssaamm/sign-language-tutor
/
trainer.py
78 lines (58 loc) · 1.9 KB
/
trainer.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
from classifier import clf
from collections import OrderedDict
from db import add_data
from hand_data import get_hand_position
from lib import Leap
import time
import pickle
NUM_SAMPLES = 100
SAMPLE_DELAY = .1
NUM_FEATURES = 60
with open('wordLemPoS/markov.pkl', 'r') as f:
markov = pickle.load(f)
def get_char_to_train():
training_char = raw_input("Enter char to train: ")
while len(training_char) != 1 or not training_char.isalpha():
print "Please enter a single alpha character"
training_char = raw_input("Enter char to train: ")
return training_char.lower()
def train_char(training_char):
controller = Leap.Controller()
for t in range(NUM_SAMPLES):
time.sleep(SAMPLE_DELAY)
sample = get_hand_position(controller, True)
while len(sample) != NUM_FEATURES:
print "Please place only right hand in view"
sample = get_hand_position(controller, True)
print sample
add_data(sign=training_char, **sample)
print "Done training"
prev = 'a'
def guess_char():
global prev
controller = Leap.Controller()
controller.set_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES)
classes = clf.classes_
probs = zip(classes, clf.predict_proba([v for k, v in
get_hand_position(controller, True).iteritems()])[0])
m = markov[prev]
most_sym, most_val = m.most_common(1)[0]
alpha = max([score for sym, score in probs])
most_probable = sorted([(sym, alpha * score + (1 - alpha) * m[sym] /
most_val) for sym, score in probs], key=lambda t: t[1],
reverse=True)
print most_probable[:3]
prev = most_probable[0][0]
print prev
def train():
while True:
training_char = get_char_to_train()
time.sleep(2)
train_char(training_char)
def guess():
while True:
guess_char()
time.sleep(1)
if __name__ == "__main__":
#train()
guess()