-
Notifications
You must be signed in to change notification settings - Fork 0
/
arayuz.py
387 lines (297 loc) · 13.1 KB
/
arayuz.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'uygulama.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import pyaudio
import wave
import numpy as np
import tensorflow as tf
import librosa
from scipy import signal
import random
import sys
import os
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#####################################################
mfcc = []
def amplitude_envelope(Signal, frame_size, hop_length):
return np.array([ max(Signal[i:i+frame_size]) for i in range(0, Signal.size, hop_length) ]).tolist()
def RMS(Signal, frame_size, hop_length):
return librosa.feature.rms(Signal, frame_length=frame_size, hop_length = hop_length).tolist()[0]
def ZeroCrossingRate(Signal, frame_size, hop_length):
return librosa.feature.zero_crossing_rate(Signal, frame_length=frame_size, hop_length = hop_length).tolist()[0]
def STFT(signal, frame_size, hop_length):
a = librosa.stft(signal, n_fft = frame_size, hop_length=hop_length)
a = np.abs(a) ** 2
a = np.resize(a, (216,1))
return a.T.tolist()[0]
def Mels(signal, frame_size, hop_length):
global mfcc
mfcc = []
mfcc = librosa.feature.mfcc(signal, n_fft = frame_size, hop_length= hop_length, n_mfcc = 13)
return mfcc.tolist()
def Mel_Delta_1(signal, frame_size, hop_length):
global mfcc
d = librosa.feature.delta(mfcc)
return d.tolist()
def Mel_Delta_2(signal, frame_size, hop_length):
global mfcc
d2 = librosa.feature.delta(mfcc, order = 2)
return d2.tolist()
def SpectralCentroid(signal, frame_size, hop_length):
a = librosa.feature.spectral_centroid(signal, n_fft = frame_size, hop_length=hop_length)
return a.tolist()[0]
def SpectralBandwith(signal, frame_size, hop_length):
a = librosa.feature.spectral_bandwidth(signal, n_fft = frame_size, hop_length=hop_length)
return a.tolist()[0]
functions = [amplitude_envelope, RMS, ZeroCrossingRate, STFT, Mels, Mel_Delta_1, Mel_Delta_2, SpectralCentroid, SpectralBandwith]
names = ["envelope", "rms", "zero_crossing", "stft", "mel", "mel_delta_1", "mel_delta_2", "spectral_centroid", "spectral_bandwith"]
def get_features(signal):
y = signal
FRAME_LENGTH = 1024
HOP_LENGTH = 512
x = np.array(functions[0](y, FRAME_LENGTH, HOP_LENGTH))
leng = x.shape[0]
if x.shape[0] > 100:
leng = 1
for i in range(len(functions)):
a = np.array(functions[i](y, FRAME_LENGTH, HOP_LENGTH))
x = np.vstack((x, a))
return x[leng:]
def get_all_features(path):
y, sr = librosa.load(path)
leng = 22050 * 5
a = []
for i in range(0, len(y) - leng + 1, leng):
a.append(get_features(y[i: i+leng]))
a = np.array(a)
return a
#####################################################
class Worker(QtCore.QObject):
finished = QtCore.pyqtSignal()
progress = QtCore.pyqtSignal(int)
kaydediliyorText = None
pushButton = None
def run(self):
self.kaydediliyorText.setText("Recording ...")
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 22050
RECORD_SECONDS = 31
WAVE_OUTPUT_FILENAME = resource_path("voice.wav")
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames= []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
audio.terminate()
sound_file = wave.open(WAVE_OUTPUT_FILENAME, "wb")
sound_file.setnchannels(CHANNELS)
sound_file.setsampwidth(audio.get_sample_size(FORMAT))
sound_file.setframerate(RATE)
sound_file.writeframes(b''.join(frames))
sound_file.close()
self.finished.emit()
class Worker2(QtCore.QObject):
kaydediliyorText = None
oran = 0
finished = QtCore.pyqtSignal()
progress = QtCore.pyqtSignal(int)
model = None
def run(self):
self.kaydediliyorText.setText("Calculating ...")
if self.model is None:
print("model none")
self.model = tf.keras.models.load_model(resource_path("model"))
else:
print("model not none")
healthy_rates = []
covid_rates = []
k = get_all_features(resource_path("voice.wav"))
k = np.stack((k,k,k), -1)
a = self.model.predict(k)
healthy = []
for i in range(a.shape[0]):
healthy.append(a[i][1])
self.oran = sum(healthy) / len(healthy)
self.oran = self.oran * 100.0
self.oran = np.round(self.oran, 2)
self.finished.emit()
class Ui_MainWindow(object):
icon = QtGui.QIcon()
thread = None
thread2 = None
model = None
def setModel(self, model_):
self.model = model_
def hesaplaVeYazdir(self):
# Load TFLite model and allocate tensors.
self.thread2 = QtCore.QThread()
self.worker2 = Worker2()
self.worker2.kaydediliyorText = self.kaydediliyorText
self.worker2.model = self.model
self.worker2.moveToThread(self.thread2)
self.thread2.started.connect(self.worker2.run)
self.worker2.finished.connect(self.thread2.quit)
self.worker2.finished.connect(self.worker2.deleteLater)
self.thread2.finished.connect(self.thread2.deleteLater)
self.covidOran.setVisible(True)
self.covidText.setVisible(True)
self.saglikliOran.setVisible(True)
self.saglikliText.setVisible(True)
self.thread2.finished.connect(
lambda : self.covidOran.setText("% " + str(100 - self.worker2.oran))
)
self.thread2.finished.connect(
lambda : self.saglikliOran.setText("% " + str(self.worker2.oran))
)
self.thread2.finished.connect(
lambda : self.setModel(self.worker2.model)
)
self.thread2.start()
def recordClicked(self):
self.kaydediliyorText.setVisible(True)
self.thread = QtCore.QThread()
self.worker = Worker()
self.worker.kaydediliyorText = self.kaydediliyorText
self.worker.pushButton = self.pushButton
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.finished.connect(
self.hesaplaVeYazdir
)
self.thread.start()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.UstMetin1 = QtWidgets.QLabel(self.centralwidget)
self.UstMetin1.setGeometry(QtCore.QRect(20, 80, 741, 91))
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.UstMetin1.setFont(font)
self.UstMetin1.setObjectName("UstMetin1")
self.UstMetin2 = QtWidgets.QLabel(self.centralwidget)
self.UstMetin2.setGeometry(QtCore.QRect(180, 140, 431, 41))
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.UstMetin2.setFont(font)
self.UstMetin2.setAlignment(QtCore.Qt.AlignCenter)
self.UstMetin1.setAlignment(QtCore.Qt.AlignCenter)
self.UstMetin2.setObjectName("UstMetin2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 190, 221, 171))
self.pushButton.setStyleSheet("background-color: rgba(255, 255, 255, 0);")
self.pushButton.setText("")
self.icon.addPixmap(QtGui.QPixmap(resource_path("icon.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(self.icon)
self.pushButton.setIconSize(QtCore.QSize(128, 128))
self.pushButton.setObjectName("pushButton")
self.kaydediliyorText = QtWidgets.QLabel(self.centralwidget)
self.kaydediliyorText.setGeometry(QtCore.QRect(330, 370, 161, 21))
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.kaydediliyorText.setFont(font)
self.kaydediliyorText.setObjectName("kaydediliyorText")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(230, 400, 321, 171))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.saglikliText = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.saglikliText.setFont(font)
self.saglikliText.setObjectName("saglikliText")
self.horizontalLayout_2.addWidget(self.saglikliText)
self.saglikliOran = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.saglikliOran.setFont(font)
self.saglikliOran.setObjectName("saglikliOran")
self.horizontalLayout_2.addWidget(self.saglikliOran)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.covidText = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.covidText.setFont(font)
self.covidText.setObjectName("covidText")
self.horizontalLayout.addWidget(self.covidText)
self.covidOran = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Kalam Light")
font.setPointSize(14)
self.covidOran.setFont(font)
self.covidOran.setObjectName("covidOran")
self.horizontalLayout.addWidget(self.covidOran)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.covidOran.setVisible(False)
self.covidText.setVisible(False)
self.saglikliOran.setVisible(False)
self.saglikliText.setVisible(False)
self.kaydediliyorText.setVisible(False)
self.pushButton.clicked.connect(self.recordClicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "COVID-19 TEST"))
self.UstMetin1.setText(_translate("MainWindow", "You need 30 seconds of audio recording to perform the test. Please Press"))
self.UstMetin2.setText(_translate("MainWindow", "The Button Below To Record Your Voice"))
self.kaydediliyorText.setText(_translate("MainWindow", "Recording....."))
self.saglikliText.setText(_translate("MainWindow", "Healthy : "))
self.saglikliOran.setText(_translate("MainWindow", "% ??.??"))
self.covidText.setText(_translate("MainWindow", "COVID - 19 : "))
self.covidOran.setText(_translate("MainWindow", "% ??.??"))
from PyQt5 import QtWidgets
import sys
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()