-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (124 loc) · 4.3 KB
/
main.py
File metadata and controls
178 lines (124 loc) · 4.3 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import speaker_verification as sv
from record import Record
from matrix import Matrix
import time
import numpy as np
from pathlib import Path
from scipy.io.wavfile import write
from threading import Thread
import RPi.GPIO as GPIO
#================== AVERAGING NAMES ===================
def average_list(list_names, list_of_elements=None):
# jeśli nie mamy żadnych danych odnośnie nazw, musimy sprawdzić jakie są
if list_of_elements is None:
list_of_elements = []
for name in list_names:
if name not in list_of_elements:
list_of_elements.append(name)
counts = {}
for element in list_of_elements:
counts[element] = list_names.count(element)
out = max(counts, key=counts.get)
return out
global show_name_global
show_name_global = 'Waiting...'
global sufix
sufix = '_train'
global RUN
RUN = True
def mainVR():
number_of_average = 8
moving_classifier = []
for i in range(number_of_average):
moving_classifier.append(None)
# ====================== EMBEDDINGS =======================
path_to_records = Path('rec')
path_to_save_emb = Path('emb')
embeddings = sv.insert_speakers(path_to_records, path_to_save_emb, end_filename=sufix, verbose=1)
speaker_names = list(embeddings.keys())
speaker_names.append(None)
# =================== LIVE RECORDING ====================
fs = 16000
SIGNAL_EMB_TIME = 1.6
N_moving_signal = round(SIGNAL_EMB_TIME*fs)
moving_signal = np.zeros(N_moving_signal)
CHUNK_TIME = 0.4
CHUNK = round(CHUNK_TIME*fs)
rec = Record(fs=fs, chunk_time=CHUNK_TIME)
id = rec.get_id_device()
rec.setup(id)
#================== MAIN LOOP ===================
global show_name_global
global RUN
record_data = np.array([])
i = 0
print("* start embedding")
while RUN:
try:
actual_signal = rec.read()
record_data = np.append(record_data, actual_signal)
# dodanie do bufora sygnału i wyciszenie krańców
moving_signal = np.append(moving_signal[CHUNK:N_moving_signal], actual_signal)
#moving_signal = fading(moving_signal, fs, timefold=0.05)
# weryfikacja mówcy
embedding = sv.count_embedding(moving_signal, fs)
speaker_filename = sv.speaker_ver(embedding, embeddings, coeff=1)
# uśrednienie wyników
moving_classifier.pop(0)
moving_classifier.append(speaker_filename)
best_filename = average_list(moving_classifier)
# wrzucenie nazwy do zmiennej globalnej
#show_name_global = str(best_filename)
show_name_global = str(speaker_filename)
print(i, '->', speaker_filename)
i += 1
except ValueError:
# errory występują w 0.45% przypadków (sprawdzane na godzinnym nagraniu)
print('ERROR')
#errors += 1
print("* done embedding")
rec.stop()
convert_data = record_data*((2**16-1)-1)
write("example.wav", fs, convert_data.astype(np.int16))
def mainM():
print('start matrix thread')
matrix = Matrix()
WIDTH = 32
HEIGTH = 8
global show_name_global
global sufix
color = (50,50,50)
delay = 0.1
name = ''
pos = WIDTH
global RUN
while RUN:
if show_name_global == 'None':
color = (50,0,0)
name = show_name_global
else:
color = (50,0,50)
name = show_name_global.replace(sufix + '.wav', '')
matrix.text(name, color, posx=pos)
pos -= 1
if pos <= -(8*len(name)):
pos = WIDTH
time.sleep(delay)
if __name__ == '__main__':
VoiceRecognitionThread = Thread(target=mainVR)
MatrixThread = Thread(target=mainM)
VoiceRecognitionThread.start()
MatrixThread.start()
# === MAIN PROGRAM ===
BUTTON = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN)
state = False
while True:
state = GPIO.input(BUTTON)
if not state:
print('Interrupt application')
RUN = False
break
VoiceRecognitionThread.terminate()
MatrixThread.terminate()