-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUI.py
executable file
·275 lines (237 loc) · 9.75 KB
/
UI.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
import time
import pyaudio
import numpy as np
from Main import Main
from scipy.io import wavfile
import traceback as tb
from PyQt4 import uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class RecorderThread(QThread):
def __init__(self, ui):
QThread.__init__(self)
self.ui = ui
def run(self):
self.start_time = time.time()
while True:
data = self.ui.stream.read(1)
i = ord(data[0]) + 256 * ord(data[1])
if i > 32768:
i -= 65536
self.ui.recordData.append(i)
if self.ui.stopped:
break
class UI(QMainWindow):
CONV_INTERVAL = 0.4
CONV_DURATION = 1.5
CONV_FILTER_DURATION = CONV_DURATION
TEST_DURATION = 3
def __init__(self, parent=None):
QWidget.__init__(self, parent)
uic.loadUi("new.ui", self)
try :
self.main = Main.load('02-09-2017.model')
except :
self.main = Main()
self.task = None
self.recordData = []
self.statusBar()
self.timer = QTimer(self)
self.timer.timeout.connect(self.timer_callback)
self.Startrecord.clicked.connect(self.startenrollrecord)
self.Stoprecord.clicked.connect(self.stopenrollrecord)
self.Choosefile.clicked.connect(self.chooseenrollfile)
self.Register.clicked.connect(self.register)
self.Clear.clicked.connect(self.clear)
self.Exit.clicked.connect(self.exit)
self.Startrecordreco.clicked.connect(self.startrecorecord)
self.Stoprecordreco.clicked.connect(self.stoprecorecord)
self.Choosefilereco.clicked.connect(self.chooserecofile)
self.Startrecordconv.clicked.connect(self.startconvrecord)
self.Stoprecordconv.clicked.connect(self.stopconvrecord)
self.Exitconv.clicked.connect(self.exit)
self.Clearconv.clicked.connect(self.clear)
fs, signal = wavfile.read('background earphone.wav')
self.main.ad.init_noise(fs, signal)
def timer_callback(self):
self.record_time += 1
#self.status("Recording..." + time_str(self.record_time))
minutes = int(self.record_time / 60)
sec = int(self.record_time % 60)
s = "{:02d}:{:02d}".format(minutes, sec)
#s = time_str(self.record_time)
''' Instead of updating all the timers try updating only the required one'''
if self.task == 'enroll' :
self.Recordtime.setText(s)
elif self.task == 'recognise':
self.Recordtimereco.setText(s)
elif self.task == 'conversation':
self.Recordtimeconv.setText(s)
def startrecord(self):
self.statusBar().showMessage("Recording...")
self.pyaudio = pyaudio.PyAudio()
self.recordData = []
self.stream = self.pyaudio.open(format=self.main.FORMAT, channels=1, rate=self.main.FS,
input=True, frames_per_buffer=1)
self.stopped = False
self.reco_th = RecorderThread(self)
self.reco_th.start()
self.timer.start(1000)
self.record_time = 0
def stoprecord(self):
self.stopped = True
self.reco_th.wait()
self.timer.stop()
self.stream.stop_stream()
self.stream.close()
self.pyaudio.terminate()
self.statusBar().showMessage("Recording Stopped.")
self.recordData = np.array(self.recordData, dtype=self.main.NPDtype)
wavfile.write('myaudio.wav', self.main.FS, self.recordData)
def startenrollrecord(self):
self.task = 'enroll'
self.main.name = str(self.Username.text().trimmed())
if not name:
QMessageBox.warning(self, "Warning", "Please enter your name first.")
self.statusBar().showMessage('')
elif self.main.name == 'Unknown':
QMessageBox.warning(self, "Warning", "Please enter a valid name.")
self.statusBar().showMessage('')
else:
self.startrecord()
def stopenrollrecord(self):
self.stoprecord()
''' remove silence here and wait for regitser button click for further'''
self.main.name = str(self.Username.text().trimmed())
self.main.signal = self.main.ad.filter(self.main.FS, self.recordData)
wavfile.write('silence removed.wav', self.main.FS, self.main.signal)
if len(self.main.signal) > 50:
self.main.getFeatures()
else :
QMessageBox.warning(self, "Warning", "Audio was silent.Try Again.")
#print "features extracted"
def chooseenrollfile(self):
self.main.name = str(self.Username.text().trimmed())
if not self.main.name:
QMessageBox.warning(self, "Warning", "Please enter your name first.")
self.statusBar().showMessage('')
elif self.main.name == 'Unknown':
QMessageBox.warning(self, "Warning", "Please enter a valid name.")
self.statusBar().showMessage('')
else:
fname = QFileDialog.getOpenFileName(self, "Select Audio File", "", "Files (*.wav)")
if not fname:
return
self.statusBar().showMessage('Loaded '+ fname)
self.Filename.setText(fname)
''' remove silence here and wait for register button click for further '''
self.main.FS, self.recordData = wavfile.read(fname)
self.main.signal = self.main.ad.filter(self.main.FS, self.recordData)
self.main.getFeatures()
#print "features extracted"
def startrecorecord(self):
self.task = 'recognise'
self.startrecord()
def stoprecorecord(self):
self.stoprecord()
user = None
''' remove silence and proceed with recognition here only'''
self.main.signal = self.main.ad.filter(self.main.FS, self.recordData)
if len(self.main.signal) > 50:
user = self.main.predict()
self.IdentifiedUser.setText(user)
def chooserecofile(self):
self.IdentifiedUser.setText('Unknown')
fname = QFileDialog.getOpenFileName(self, "Select Audio File", "", "Files (*.wav)")
if not fname:
return
user = None
self.statusBar().showMessage('Loaded '+ fname)
self.Filenamereco.setText(fname)
''' remove silence and extract features '''
self.main.FS, self.recordData = wavfile.read(fname)
self.main.signal = self.main.ad.filter(self.main.FS, self.recordData)
if len(self.main.signal) > 50:
user = self.main.predict()
self.IdentifiedUser.setText(user)
self.statusBar().showMessage('')
def startconvrecord(self):
self.task = 'conversation'
self.conv_result_list = []
self.startrecord()
self.conv_now_pos = 0
self.conv_timer = QTimer(self)
self.conv_timer.timeout.connect(self.do_conversation)
self.conv_timer.start(self.CONV_INTERVAL * 1000)
def stopconvrecord(self):
self.statusBar().showMessage("Recording Stopped.")
self.stoprecord()
self.conv_timer.stop()
def do_conversation(self):
interval_len = int(self.CONV_INTERVAL * self.main.FS)
segment_len = int(self.CONV_DURATION * self.main.FS)
self.conv_now_pos += interval_len
to_filter = self.recordData[max([self.conv_now_pos - segment_len, 0]):
self.conv_now_pos]
signal = np.array(to_filter, dtype=self.main.NPDtype)
label = None
print label, "in do_conversation"
'''
try:
signal = self.backend.filter(self.main.FS, signal)
if len(signal) > 50:
label = self.backend.predict(self.main.FS, signal, True)
except Exception as e:
print tb.format_exc()
print str(e)
'''
try:
self.main.signal = self.main.ad.filter(self.main.FS, signal)
if len(self.main.signal) > 50:
label = self.main.predict()
except Exception as e:
print tb.format_exc()
print str(e)
global last_label_to_show
label_to_show = label
if label and self.conv_result_list:
last_label = self.conv_result_list[-1]
if last_label and last_label != label:
label_to_show = last_label_to_show
self.conv_result_list.append(label)
print label_to_show, "label to show"
last_label_to_show = label_to_show
#ADD FOR GRAPH
if label_to_show is None:
label_to_show = 'Nobody'
'''
if len(NAMELIST) and NAMELIST[-1] != label_to_show:
NAMELIST.append(label_to_show)
'''
self.IdentifiedConvUser.setText(label_to_show)
def register(self):
if not self.Username.text().trimmed():
QMessageBox.warning(self, "Warning", "Please enter your name.")
elif len(self.main.signal) == 0:
QMessageBox.warning(self, "Warning", "Input signal is silent. Try again.")
else:
self.statusBar().showMessage('Registration started...')
self.main.train()
self.main.dump()
self.statusBar().showMessage('Registration complete.')
def clear(self):
self.Username.setText("")
self.Filenamereco.setText("")
self.Filename.setText("")
self.Recordtime.setText("00:00")
self.Recordtimereco.setText("00:00")
self.Recordtimeconv.setText("00:00")
self.main.FS = 8000
self.main.signal = []
self.statusBar().showMessage('')
self.recordData = []
self.IdentifiedUser.setText("Unknown")
self.IdentifiedConvUser.setText("Unknown")
def exit(self):
self.close();