forked from qobi/ece57000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtw_gui.py
61 lines (54 loc) · 1.75 KB
/
dtw_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
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
points1 = []
points2 = []
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 dog_command():
message("")
global points1
points1 = stop_recording()
def cat_command():
message("")
global points2
points2 = stop_recording()
def distance_command():
message("")
if len(points1)==0 or len(points2)==0:
message("No data")
else:
d, image = dtw_with_image(L2_vector(L2_scalar))(points1, points2)
message("{:.6f}".format(d))
get_axes().imshow(image, cmap="gray")
redraw()
add_button(0, 0, "Dog", start_recording(10, False), dog_command)
add_button(0, 1, "Cat", start_recording(10, False), cat_command)
add_button(0, 2, "Distance", distance_command, nothing)
add_button(0, 3, "Exit", done, nothing)
message = add_message(1, 0, 4)
start_variable_size_matplotlib(7, 7, 2, 4)