-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
502 lines (410 loc) · 17.1 KB
/
Main.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import glob
import shutil
from collections import Counter
import librosa.display
from pydub import AudioSegment
from pydub.silence import split_on_silence
import warnings
import joblib
from features import *
import os
from fastdtw import fastdtw
from scipy.spatial.distance import euclidean
import numpy as np
import librosa
import copy
from speechtotext import get_large_audio_transcription
def speechmodels(sample):
warnings.filterwarnings('ignore')
# for Machine Learning with random Forest
# load, extract, and visualize the features
features = process_data_for_ML_Rf(sample)
# prediction
# pred_ML_RF = predict_ML_RF(features)
# pred_ML_KNN = predict_ML_KNN(features)
pred_ML_SVM= predict_ML_SVM(features)
# pred_ML_Vote= predict_ML_VOTE(features)
# Votes=pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote
return pred_ML_SVM
def wordsmodels(sample):
warnings.filterwarnings('ignore')
# for Machine Learning with random Forest
# load, extract, and visualize the features
features = process_data_for_ML_Rf(sample)
# prediction
pred_ML_RF = word_predict_ML_RF(features)
pred_ML_KNN = word_predict_ML_KNN(features)
pred_ML_SVM= word_predict_ML_SVM(features)
pred_ML_Vote= word_predict_ML_VOTE(features)
# Votes=pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote
return pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote
# for Machine Learning on KNN with Features
def predict_ML_KNN(features):
loaded_model = joblib.load('content/model_knn.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
def predict_ML_SVM(features):
loaded_model = joblib.load('content/model_svm.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
def predict_ML_VOTE(features):
loaded_model = joblib.load('content/model_voting.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
# for Machine Learning on KNN with Features
def word_predict_ML_KNN(features):
loaded_model = joblib.load('content/model_knnwords.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
def word_predict_ML_SVM(features):
loaded_model = joblib.load('content/model_svmwords.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
def word_predict_ML_VOTE(features):
loaded_model = joblib.load('content/model_votingwords.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
# for Machine Learning on random forest with Features
def predict_ML_RF(features):
loaded_model = joblib.load('content/model_3000.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
# for Machine Learning on random forest with Features
def word_predict_ML_RF(features):
loaded_model = joblib.load('content/model_3000words.sav')
pred = loaded_model.predict(features.reshape(1, 6000))
return pred
class feature_analysis_graphs():
def sample_graph(self, samples, sample_rate):
fig, ax = plt.subplots(figsize=(10, 10))
librosa.display.waveplot(samples, sr=sample_rate)
ax.label_outer()
ax.set(title='Data Respresentation')
plt.show()
def MFCC_graph(self, samples):
fig, ax = plt.subplots(figsize=(10, 10))
img = librosa.display.specshow(samples, x_axis='time', ax=ax)
ax.set(title='MFCC')
ax.label_outer()
plt.show()
def melspectrogram_graph(self, data):
fig, ax = plt.subplots(figsize=(10, 10))
S_dB = librosa.power_to_db(data, ref=np.max)
img = librosa.display.specshow(S_dB, x_axis='time',
y_axis='mel', sr=16000,
fmax=8000, ax=ax)
ax.set(title='Mel-frequency spectrogram')
ax.label_outer()
plt.show()
def poly_graph(self, data):
fig, ax = plt.subplots(figsize=(10, 10))
times = librosa.times_like(data)
ax.plot(times, data[1].T, alpha=0.8, label='Poly Feature')
ax.legend()
ax.label_outer()
plt.show()
def zero_crossing_rate_graph(self, data):
fig, ax = plt.subplots(figsize=(10, 10))
times = librosa.times_like(data)
ax.plot(times, data[0], label='zero crossing rate')
ax.legend()
ax.label_outer()
plt.show()
def process_data_for_ML_KNN(samples):
sample_rate = 16000
#graph=feature_analysis_graphs()
#graph.sample_graph(samples, sample_rate)
# Extract Feautures
MFCC = mfcc_feature(samples, sample_rate)
#graph.MFCC_graph(MFCC)
MSS = melspectrogram_feature(samples, sample_rate)
#graph.melspectrogram_graph(MSS)
poly = poly_feature(samples, sample_rate)
#graph.poly_graph(poly)
ZCR = zero_crossing_rate_features(samples)
#graph.zero_crossing_rate_graph(ZCR)
# flatten an array
MFCC = MFCC.flatten()
MSS = MSS.flatten()
poly = poly.flatten()
ZCR = ZCR.flatten()
# adding features into single array
features = np.concatenate((MFCC, MSS, poly, ZCR))
# padding and trimming
max_len = 6000
pad_width = max_len - features.shape[0]
if pad_width > 0:
features = np.pad(features, pad_width=((0, pad_width)), mode='constant')
features = features[:max_len]
return features
def chunk_process_data_for_ML_KNN(samples):
sample_rate = 16000
graph=feature_analysis_graphs()
graph.sample_graph(samples, sample_rate)
# Extract Feautures
MFCC = mfcc_feature(samples, sample_rate)
graph.MFCC_graph(MFCC)
MSS = melspectrogram_feature(samples, sample_rate)
graph.melspectrogram_graph(MSS)
poly = poly_feature(samples, sample_rate)
graph.poly_graph(poly)
ZCR = zero_crossing_rate_features(samples)
graph.zero_crossing_rate_graph(ZCR)
# flatten an array
MFCC = MFCC.flatten()
MSS = MSS.flatten()
poly = poly.flatten()
ZCR = ZCR.flatten()
# adding features into single array
features = np.concatenate((MFCC, MSS, poly, ZCR))
# padding and trimming
max_len = 6000
pad_width = max_len - features.shape[0]
if pad_width > 0:
features = np.pad(features, pad_width=((0, pad_width)), mode='constant')
features = features[:max_len]
return features
def process_data_for_ML_Rf(samples):
sample_rate = 16000
#graph = feature_analysis_graphs()
#graph.sample_graph(samples, sample_rate)
# Extract Feautures
MFCC = mfcc_feature(samples, sample_rate)
#graph.MFCC_graph(MFCC)
MSS = melspectrogram_feature(samples, sample_rate)
#graph.melspectrogram_graph(MSS)
poly = poly_feature(samples, sample_rate)
#graph.poly_graph(poly)
ZCR = zero_crossing_rate_features(samples)
#graph.zero_crossing_rate_graph(ZCR)
# flatten an array
MFCC = MFCC.flatten()
MSS = MSS.flatten()
poly = poly.flatten()
ZCR = ZCR.flatten()
# adding features into single array
features = np.concatenate((MFCC, MSS, poly, ZCR))
# padding and trimming
max_len = 6000
pad_width = max_len - features.shape[0]
if pad_width > 0:
features = np.pad(features, pad_width=((0, pad_width)), mode='constant')
features = features[:max_len]
return features
# for Deep Learningwih features
def normalize_2d(v):
for i in range(v.shape[0]):
norm = np.linalg.norm(v[i])
if norm == 0:
v[i] = v[i]
else:
v[i] = v[i] / norm
return v
# adjust target amplitude
def match_target_amplitude(sound, target_dBFS):
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
def makechunk(Inputfilename, sample_rate, foldername):
#os.remove(foldername)
sound_file = AudioSegment.from_wav(Inputfilename)
audio_chunks = split_on_silence(sound_file, min_silence_len=40, silence_thresh=-36)
# os.mkdir('content')
os.mkdir(foldername)
for i, chunk in enumerate(audio_chunks):
out_file = foldername+"/chunk{0}.wav".format(i)
# print("exporting", out_file)
chunk.export(out_file, format="wav")
sound_files = []
chunks = os.listdir(foldername)
for chunk in chunks:
chunk = foldername + "/" + chunk
# print(chunk)
sample, sample_rate = librosa.load(chunk, sr=sample_rate)
sound_files.append(sample)
from pydub.silence import detect_nonsilent
# Convert wav to audio_segment
audio_segment = AudioSegment.from_wav(Inputfilename)
# normalize audio_segment to -20dBFS
normalized_sound = match_target_amplitude(audio_segment, -20.0)
# print("length of audio_segment={} seconds".format(len(normalized_sound) / 1000))
# # print detected non-silent chunks, which in our case would be spoken words.
nonsilent_data = detect_nonsilent(normalized_sound, min_silence_len=40, silence_thresh=-36, seek_step=1)
# convert ms to seconds
# print("start,Stop")
chunks_timestamps=list()
for chunks in nonsilent_data:
chunks_timestamps.append([chunk / 1000 for chunk in chunks])
# print([chunk / 1000 for chunk in chunks])
return chunks_timestamps
def speechrecognitiontest(Inputfilename):
# Inputfilename = "POC/108/001/108001_01.wav"
sample_rate = 16000
sample, sample_rate = librosa.load(Inputfilename, sr=sample_rate)
pred_ML_SVM= speechmodels(sample)
# print(pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote)
actuallfilename=''
#if a == 0:
# actuallfilename = "POC/108/001/"
#elif a == 1:
# actuallfilename = "POC/108/002/"
return pred_ML_SVM
def wordrecognitiontest(Inputfilename):
# Inputfilename = "POC/108/001/108001_01.wav"
sample_rate = 16000
sample, sample_rate = librosa.load(Inputfilename, sr=sample_rate)
pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote = wordsmodels(sample)
# print(pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote)
actuallfilename=''
#if a == 0:
# actuallfilename = "POC/108/001/"
#elif a == 1:
# actuallfilename = "POC/108/002/"
return pred_ML_RF,pred_ML_KNN,pred_ML_SVM,pred_ML_Vote
def fill_dtw_cost_matrix(s1, s2):
l_s_1, l_s_2 = len(s1), len(s2)
cost_matrix = np.zeros((l_s_1 + 1, l_s_2 + 1))
for i in range(l_s_1 + 1):
for j in range(l_s_2 + 1):
cost_matrix[i, j] = np.inf
cost_matrix[0, 0] = 0
for i in range(1, l_s_1 + 1):
for j in range(1, l_s_2 + 1):
cost = abs(s1[i - 1] - s2[j - 1])
# take last min from the window
prev_min = np.min([cost_matrix[i - 1, j], cost_matrix[i, j - 1], cost_matrix[i - 1, j - 1]])
cost_matrix[i, j] = cost + prev_min
return cost_matrix
def dtw_calculation(samplefile, targetfile, sample_rate):
sample1, sample_rate = librosa.load(targetfile, sr=sample_rate)
mfcc1 = librosa.feature.mfcc(sample1, sample_rate)
sampletest, sample_rate = librosa.load(samplefile, sr=sample_rate)
mfccTest = librosa.feature.mfcc(sampletest, sample_rate)
#chunk_process_data_for_ML_KNN(sample1)
#chunk_process_data_for_ML_KNN(sampletest)
# Remove mean and normalize each column of MFCC
def preprocess_mfcc(mfcc):
mfcc_cp = copy.deepcopy(mfcc)
for i in range(mfcc.shape[1]):
mfcc_cp[:, i] = mfcc[:, i] - np.mean(mfcc[:, i])
mfcc_cp[:, i] = mfcc_cp[:, i] / np.max(np.abs(mfcc_cp[:, i]))
return mfcc_cp
mfcc1 = preprocess_mfcc(mfcc1)
a=mfcc1.shape
mfccTest = preprocess_mfcc(mfccTest)
# padding and trimming
max_len = 500
pad_width = max_len - mfcc1 .shape[1]
if pad_width > 0:
mfcc1 = np.pad(mfcc1 , pad_width=((1, pad_width)), mode='constant')
mfcc1 = mfcc1 [:max_len]
# padding and trimming
max_len = 500
pad_width = max_len - mfccTest.shape[1]
if pad_width > 0:
mfccTest = np.pad(mfccTest, pad_width=((1, pad_width)), mode='constant')
mfccTest = mfccTest[:max_len]
# print(mfcc1.shape)
# print(mfccTest.shape)
#l2_norm = lambda mfcc1, mfccTest: (mfcc1- mfccTest) ** 2
d1, p1 = fastdtw(mfcc1, mfccTest, dist=euclidean)
# print(d1, p1)
return d1
# makechunk(Inputfilename, sample_rate, foldername)
def Main(Inputfilename):
folder = 'chunks'
try:
for dir in os.listdir(folder):
shutil.rmtree(os.path.join(folder, dir))
except OSError as e:
print("Error: %s : %s" % (folder, e.strerror))
#Inputfilename = "ayat/ayat_001/108001_06.wav"
sample_rate = 16000
Predictedfoldername = "chunks/PredictedTest"
# os.chmod(Predictedfoldername, 0o777)
pred_ML_RF = speechrecognitiontest(Inputfilename)
labels = os.listdir("ayat")
print(pred_ML_RF)
Surah_Result = labels[np.int(pred_ML_RF)]
chunks_timestamps=makechunk(Inputfilename, sample_rate, Predictedfoldername)
Targetfoldername = "chunks/TargetTest"
TargetInputfileFolder = "ayat"
SubFolder = TargetInputfileFolder + "/" + Surah_Result + "/"
print(SubFolder)
train_Labels = []
# os.chmod(Targetfoldername, 0o777)
for filename in glob.glob(os.path.join(SubFolder,"*.wav")):
train_Labels.append(filename)
print(train_Labels)
makechunk(train_Labels[0], sample_rate, Targetfoldername)
test_chunks = []
for filename in glob.glob(os.path.join(Predictedfoldername, '*.wav')):
test_chunks.append(filename)
print(test_chunks)
target_chunks = []
for filename in glob.glob(os.path.join(Targetfoldername, '*.wav')):
target_chunks.append(filename)
print(target_chunks)
status = "Processing"
distance=[]
word_labels = os.listdir("worddataset/")
wordsresult=[]
if len(test_chunks) == len(target_chunks):
status = "You read with good normal speed."
print(status)
for i in range(len(test_chunks)):
# most_occur=get_large_audio_transcription(path)
#pred_ML_RF, pred_ML_KNN, pred_ML_SVM, pred_ML_Vote = speechrecognitiontest(test_chunks[i])
#Result = [word_labels[np.int(pred_ML_RF)], word_labels[np.int(pred_ML_KNN)], word_labels[np.int(pred_ML_SVM)],
# word_labels[np.int(pred_ML_Vote)]]
#counter = Counter(Result)
#most_occur = counter.most_common(1)
#print(most_occur)
# wordsresult.append(most_occur)
d1=dtw_calculation(test_chunks[i], target_chunks[i], 16000)
#chunk_comparison(test_chunks[i], target_chunks[i])
distance.append(d1)
elif len(test_chunks) > len(target_chunks):
status = "You read with fast speed read it slow."
print(status)
for i in range(len(target_chunks)):
if (target_chunks[i]):
#pred_ML_RF, pred_ML_KNN, pred_ML_SVM, pred_ML_Vote = speechrecognitiontest(test_chunks[i])
#Result = [word_labels[np.int(pred_ML_RF)], word_labels[np.int(pred_ML_KNN)],
# word_labels[np.int(pred_ML_SVM)],
# word_labels[np.int(pred_ML_Vote)]]
#counter = Counter(Result)
#most_occur = counter.most_common(1)
#print(most_occur)
#wordsresult.append(most_occur)
d1=dtw_calculation(test_chunks[i], target_chunks[i], 16000)
#chunk_comparison(test_chunks[i], target_chunks[i])
distance.append(d1)
elif len(test_chunks) < len(target_chunks):
status = "You read with slow speed read it normal."
print(status)
for i in range(len(test_chunks)):
if (target_chunks[i]):
#pred_ML_RF, pred_ML_KNN, pred_ML_SVM, pred_ML_Vote = speechrecognitiontest(test_chunks[i])
#Result = [word_labels[np.int(pred_ML_RF)], word_labels[np.int(pred_ML_KNN)],
# word_labels[np.int(pred_ML_SVM)],
# word_labels[np.int(pred_ML_Vote)]]
#counter = Counter(Result)
#most_occur = counter.most_common(1)
#print(most_occur[1,1])
#wordsresult.append(most_occur )
#print(Result)
d1=dtw_calculation(test_chunks[i], target_chunks[i], 16000)
#chunk_comparison(test_chunks[i], target_chunks[i])
distance.append(d1)
most_occur = get_large_audio_transcription(Inputfilename)
words = most_occur.split()
return Surah_Result,words,distance,status,chunks_timestamps
#if __name__ == '__main__':
# Inputfilename = "ayat/سورة الكوثر001/108001_01.wav"
# print(Main(Inputfilename))
# Inputfilename = "ayat/سورة الاخلاص001/112001_31.wav"
# print(Main(Inputfilename))
# Inputfilename = "ayat/سورة الفلق003/113003_31.wav"
# print(Main(Inputfilename))
# Inputfilename = "ayat/سورة الناس002/114002_31.wav"
# print(Main(Inputfilename))