forked from gabrielhuang/OhSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.py
58 lines (54 loc) · 1.55 KB
/
realtime.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
import pyaudio
import segment
import matplotlib.pyplot as plt
import transform_mfcc as transform
from sklearn.externals import joblib
import sys
tap_recog = joblib.load('forest_recog.bin')
p = pyaudio.PyAudio()
realtime = True
over=False
if realtime:
stream = p.open(format=p.get_format_from_width(2),
channels=1,
rate=44100,
input=True)
else:
chunk = segment.wav_to_np('snaps/gss.wav')[:,0]/32768.
# segment.play_wav(chunk)
frames = []
CHUNK = 10000
threshold = 0.4
text=[]
try:
last_click = None
while True:
print 'loop'
if realtime:
chunk = stream.read(CHUNK)
chunk = segment.decode(chunk, 1)[:,0]/32678.
elif over:
break
smoothed = segment.smooth(chunk, 4)
clicks, last_click = segment.get_clicks(smoothed, threshold, 10000, last_click=last_click)
if last_click is not None:
last_click -= CHUNK
taps = segment.chop(chunk, clicks, afterlength=1300, prelength=50)
#print len(taps)
if taps.shape[0]==0:
print ' ... '
for tap in taps:
ft = transform.sndFeature(tap)
letter = tap_recog.transform(ft)
proba = tap_recog.predict_proba(ft)
text.append(letter)
print '{} --> {}'.format(proba, letter)
over = True
except KeyboardInterrupt:
print "finished recording"
finally:
stream.stop_stream()
stream.close()
p.terminate()
text_str = ' '.join(text)
#print text_str