-
Notifications
You must be signed in to change notification settings - Fork 9
/
speech_classifier_gui.py
69 lines (60 loc) · 1.88 KB
/
speech_classifier_gui.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
from gui import *
from distances import *
from nearest_neighbor_classifier import *
import sounddevice as sd
import numpy as np
import time
sd.default.samplerate = 8000
sd.default.channels = 1
points = []
labels = []
distance = dtw(L2_vector(L2_scalar))
def start_recording(maximum_duration, for_classify):
def internal():
if (not for_classify) or len(points)>0:
global waveform, start_time
message("")
waveform = sd.rec(maximum_duration*sd.default.samplerate)
start_time = time.time()
return internal
def stop_recording():
global waveform
actual_time = time.time()-start_time
sd.stop()
samples = min(int(actual_time*sd.default.samplerate), len(waveform))
waveform = waveform[0:samples, 0]
get_axes().clear()
spectrum, freqs, t, im = get_axes().specgram(waveform,
Fs=sd.default.samplerate)
redraw()
sd.play(waveform)
time.sleep(float(len(waveform))/sd.default.samplerate)
return np.transpose(spectrum)
def clear_command():
global points, labels
points = []
labels = []
message("")
get_axes().clear()
redraw()
def dog_command():
message("")
points.append(stop_recording())
labels.append("Dog")
def cat_command():
message("")
points.append(stop_recording())
labels.append("Cat")
def classify_command():
message("")
if len(points)==0:
message("No data")
else:
message(classify(stop_recording(), distance, points, labels))
add_button(0, 0, "Clear", clear_command, nothing)
add_button(0, 1, "Dog", start_recording(10, False), dog_command)
add_button(0, 2, "Cat", start_recording(10, False), cat_command)
add_button(0, 3, "Classify", start_recording(10, True), classify_command)
add_button(0, 4, "Exit", done, nothing)
message = add_message(1, 0, 5)
start_variable_size_matplotlib(7, 7, 2, 5)