-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio_fraud_detector.py
496 lines (447 loc) · 16.6 KB
/
audio_fraud_detector.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
import sklearn as sk
import random, os, pickle
import scipy.io.wavfile as wav
from python_speech_features import mfcc, logfbank
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import f1_score, roc_auc_score, confusion_matrix, precision_recall_curve
def create_cga_dataframe():
"""
This module creates a computer generated audio data frame
by extracting features from the audio files and storing
the data
"""
cg_path = "data/computer_generated/*/*.wav"
cg_mfcc = [] # Cepstrum is the information of rate of change in spectral bands
cg_filter_bank = []
cg_rates = []
cg_fraud = []
cg_mfcc_means = []
cg_fbank_means = []
df = pd.DataFrame()
for wave_file in glob.glob(cg_path):
print(wave_file)
rate, sig = wav.read(wave_file)
cg_rates.append(rate)
mfcc_feature = mfcc(sig, rate, nfft=1200)
cg_mfcc.append(mfcc_feature)
fbank_feat = logfbank(sig, rate, nfft=1200)
cg_filter_bank.append(fbank_feat)
cg_mfcc_means.append(np.mean(mfcc_feature))
cg_fbank_means.append(np.mean(fbank_feat))
cg_fraud.append(1)
df['rates'] = cg_rates
df['mfcc'] = cg_mfcc
df['filter_bank'] = cg_filter_bank
df['fraud'] = cg_fraud
df['mfcc_mean'] = cg_mfcc_means
df['fbank_mean'] = cg_fbank_means
csv_loc = "data/computer_generated.csv"
df.to_csv(csv_loc)
def create_aa_dataframe():
"""
This module creates an authentic audio data frame by extracting features
from the audio files and storing the data
"""
path = "data/authentic/*.wav"
auth_mfcc = []
auth_filter_bank = []
auth_rates = []
auth_fraud = []
auth_mfcc_means = []
auth_fbank_means = []
df2 = pd.DataFrame()
for wave_file in glob.glob(path):
rate, sig = wav.read(wave_file)
if len(sig) == 0:
continue
auth_rates.append(rate)
mfcc_feature = mfcc(sig, rate, nfft=1200)
auth_mfcc.append(mfcc_feature)
fbank_feat = logfbank(sig, rate, nfft=1200)
auth_filter_bank.append(fbank_feat)
auth_mfcc_means.append(np.mean(mfcc_feature))
auth_fbank_means.append(np.mean(fbank_feat))
auth_fraud.append(0)
df2['rates'] = auth_rates
df2['mfcc'] = auth_mfcc
df2['filter_bank'] = auth_filter_bank
df2['fraud'] = auth_fraud
df2['mfcc_mean'] = auth_mfcc_means
df2['fbank_mean'] = auth_fbank_means
csv_loc = "data/authentic.csv"
df2.to_csv(csv_loc)
def create_ra_dataframe():
"""
This module creates a computer generated audio data frame
by extracting features from the audio files and storing
the data
"""
r_path = "data/fraud/recorded/*.wav"
r_mfcc = []
r_filter_bank = []
r_rates = []
r_fraud = []
r_mfcc_means = []
r_fbank_means = []
df = pd.DataFrame()
for wave_file in glob.glob(r_path):
rate, sig = wav.read(wave_file)
if len(sig) == 0:
continue
r_rates.append(rate)
mfcc_feature = mfcc(sig, rate, nfft=1200)
r_mfcc.append(mfcc_feature)
fbank_feat = logfbank(sig, rate, nfft=1200)
r_filter_bank.append(fbank_feat)
r_mfcc_means.append(np.mean(mfcc_feature))
r_fbank_means.append(np.mean(fbank_feat))
r_fraud.append(1)
df['rates'] = r_rates
df['mfcc'] = r_mfcc
df['filter_bank'] = r_filter_bank
df['fraud'] = r_fraud
df['mfcc_mean'] = r_mfcc_means
df['fbank_mean'] = r_fbank_means
csv_loc = "data/recorded.csv"
df.to_csv(csv_loc)
def analyze_computer_generated_audio_data(df):
"""
This module is to visualize and analyze the data and features
"""
# Analyze audio rate data
rates = df['computer_generated_rates'].value_counts()
labels = ["16kHz", "48kHz"]
plt.pie(rates, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title("Computer Generated Audio Rate Data")
plt.show()
# Analyze the mel-frequency data
mfcc = df.computer_generated_mfcc
avg_mfcc = []
for data in mfcc:
avg_mfcc.append(np.around(np.average(data), decimals=4))
counts, bins = np.histogram(avg_mfcc)
plt.hist(bins[:-1], bins, weights=counts, color='c', edgecolor='k')
plt.axvline(np.asarray(avg_mfcc).mean(), color='k', linestyle='dashed', linewidth=1)
plt.title("Average MFCC Histogram")
plt.xlabel("MFCC Feature Vector Average")
plt.ylabel("# of Avgs")
plt.text(0, 23, 'Mean: {:.2f}'.format(np.asarray(avg_mfcc).mean()))
plt.show()
# Here is showing an example of MFCC features that are contained in ONE audio file
mfcc_visual = mfcc[0]
plt.plot(mfcc_visual)
plt.title("MFCC Features Per Audio File")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# This is to select only 3 features out of the audio and see what that looks like
plt.plot(mfcc_visual[0])
plt.plot(mfcc_visual[10])
plt.plot(mfcc_visual[20])
plt.title("MFCC Features (3)")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# Analyze filter bank data
fbank = df.computer_generated_filter_bank
avg_fbank = []
for values in fbank:
avg_fbank.append(np.around(np.average(values), decimals=4))
plt.bar(np.arange(len(avg_fbank)), avg_fbank)
plt.title("Filter Bank Averages Per Wave File")
plt.xlabel("Filter-bank Averages")
plt.ylabel("Count")
plt.show()
counts, bins = np.histogram(avg_fbank)
plt.hist(bins[:-1], bins, weights=counts, color='b', edgecolor='k')
plt.title("Filter Bank Frequency Distribution")
plt.xlabel("Filter-bank Averages")
plt.ylabel("Count")
plt.show()
# Here is showing an example of Filter Bank features that are contained in ONE audio file
fbank_visual = fbank[1]
plt.plot(fbank_visual)
plt.title("Filter Bank Features Per Audio File")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# This is to select only 3 features out of the audio and see what that looks like
plt.plot(fbank_visual[0])
plt.plot(fbank_visual[10])
plt.plot(fbank_visual[20])
plt.title("Fbank Features (3)")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# Spectrogram snapshot of an Audio Wave File
sampling_frequency, signal_data = wav.read(df['computer_generated_audio'][0])
plt.subplot(211)
plt.title('Spectrogram of an audio wav file')
plt.plot(signal_data)
plt.xlabel('Sample')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.specgram(signal_data, Fs=sampling_frequency)
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.show()
def analyze_authentic_audio_data(df):
"""
This module is to visualize and analyze the data and features
"""
# Analyze audio rate data
rates = df['authentic_rates'].value_counts()
labels = ["44.1kHz"]
plt.pie(rates, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title("Authentic Audio Rate Data")
plt.show()
# Analyze the mel-frequency data
mfcc = df.authentic_mfcc
avg_mfcc = []
for data in mfcc:
avg_mfcc.append(np.around(np.average(data), decimals=4))
counts, bins = np.histogram(avg_mfcc)
plt.hist(bins[:-1], bins, weights=counts, color='c', edgecolor='k')
plt.axvline(np.asarray(avg_mfcc).mean(), color='k', linestyle='dashed', linewidth=1)
plt.title("Average MFCC Histogram")
plt.xlabel("MFCC Feature Vector Average")
plt.ylabel("# of Avgs")
plt.text(0, 23, 'Mean: {:.2f}'.format(np.asarray(avg_mfcc).mean()))
plt.show()
# Here is showing an example of MFCC features that are contained in ONE audio file
mfcc_visual = mfcc[0]
plt.plot(mfcc_visual)
plt.title("MFCC Features Per Audio File")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# This is to select only 3 features out of the audio and see what that looks like
plt.plot(mfcc_visual[0])
plt.plot(mfcc_visual[5])
plt.plot(mfcc_visual[10])
plt.title("MFCC Features (3)")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# Analyze filter bank data
fbank = df.authentic_filter_bank
avg_fbank = []
for values in fbank:
avg_fbank.append(np.around(np.average(values), decimals=4))
plt.bar(np.arange(len(avg_fbank)), avg_fbank)
plt.title("Filter Bank Averages Per Wave File")
plt.xlabel("Filter-bank Averages")
plt.ylabel("Count")
plt.show()
counts, bins = np.histogram(avg_fbank)
plt.hist(bins[:-1], bins, weights=counts, color='b', edgecolor='k')
plt.title("Filter Bank Frequency Distribution")
plt.xlabel("Filter-bank Averages")
plt.ylabel("Count")
plt.show()
# Here is showing an example of Filter Bank features that are contained in ONE audio file
fbank_visual = fbank[1]
plt.plot(fbank_visual)
plt.title("Filter Bank Features Per Audio File")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# This is to select only 3 features out of the audio and see what that looks like
plt.plot(fbank_visual[0])
plt.plot(fbank_visual[10])
plt.plot(fbank_visual[20])
plt.title("Fbank Features (3)")
plt.xlabel("Features (Per Color)")
plt.ylabel("Frequency")
plt.show()
# Spectrogram snapshot of an Audio Wave File
sampling_frequency, signal_data = wav.read(df['authentic_audio'][0])
plt.subplot(211)
plt.title('Spectrogram of an authentic audio wav file')
plt.plot(signal_data)
plt.xlabel('Sample')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.specgram(signal_data[:, 0], Fs=sampling_frequency)
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.show()
def train_model(cg_df, auth_df, rec_df, features, target):
"""
This module will train the ML module with df inputs to detect whether
an input audio is fraudulent or non-fraudulent
"""
df = pd.concat([cg_df, auth_df, rec_df], sort=False)
X_train, X_test, y_train, y_test = train_test_split(df[features],
df[target], test_size=0.25, random_state=1)
# clf = LogisticRegression()
clf = KNeighborsClassifier()
clf.fit(X_train, y_train.values.ravel())
y_pred = clf.predict(X_test)
# svc = SVC(kernel="linear")
# tree = DecisionTreeClassifier()
# neighbors = KNeighborsClassifier(n_neighbors=8)
# nb = GaussianNB()
# rf = RandomForestClassifier()
# lr = LogisticRegression()
# models = [svc, tree, neighbors, nb, rf, lr]
#
# scores = []
# for model in models:
# model.fit(X_train, y_train.values.ravel())
# y_pred = model.predict(X_test)
#
# scores.append([
# str(model), model.score(X_train, y_train), model.score(X_test, y_test)
# ])
# scores = pd.DataFrame(scores, columns=["model", "train", "test"])
# print("Here are the scores for the models when testing training on the following featues:", features)
# print(scores)
# print()
# print()
#
# # Calculate Confusion Matrix for FRAUD audio
# cm_fraud = confusion_matrix(y_test, y_pred)
# print("KNN Fraudulent Audio Confusion Matrix:")
# print(cm_fraud)
# print()
#
# # Compute Area Under the Receiver Operating Characteristic Curve (FRAUD)
# auc_fraud = roc_auc_score(y_test, y_pred)
# print("KNN ROC AUC: ")
# print(auc_fraud)
# print()
#
# # Compute F-Score (FRAUD)
# fscore_fraud = f1_score(y_test, y_pred)
# print("KNN F-Score: ")
# print(fscore_fraud)
# print()
#
# # Compute Precision Recall Curve
# precision, recall, thresholds = precision_recall_curve(y_test, y_pred)
# pr_auc = sk.metrics.auc(recall, precision)
# fpr, tpr, threshold = sk.metrics.roc_curve(y_test, y_pred)
# roc_auc = sk.metrics.auc(fpr, tpr)
# print("KNN Precision Recall AUC:")
# print(pr_auc)
# # Training Plots
# plt.title("Precision-Recall vs Threshold Chart")
# plt.plot(thresholds, precision[: -1], "b--", label="Precision")
# plt.plot(thresholds, recall[: -1], "r--", label="Recall")
# plt.ylabel("Precision, Recall")
# plt.xlabel("Threshold")
# plt.legend(loc="lower left")
# plt.ylim([0, 1])
# plt.show()
#
# plt.title('Receiver Operating Characteristic')
# plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc)
# plt.legend(loc='lower right')
# plt.plot([0, 1], [0, 1], 'r--')
# plt.xlim([0, 1])
# plt.ylim([0, 1])
# plt.ylabel('True Positive Rate')
# plt.xlabel('False Positive Rate')
# plt.show()
# # Classifications based on feature pairs
# plt.title('MFCC Mean VS Fbank Mean')
# plt.scatter(cg_df['mfcc_mean'], cg_df['fbank_mean'], color='blue', label='Computer Generated')
# plt.scatter(auth_df['mfcc_mean'], auth_df['fbank_mean'], color='red', label='Authentic')
# plt.scatter(rec_df['mfcc_mean'], rec_df['fbank_mean'], color='green', label='Recorded')
# plt.ylabel('Filter Bank')
# plt.xlabel('MFCC')
# plt.legend()
# plt.show()
#
# plt.title('MFCC Mean VS Rates')
# plt.scatter(cg_df['mfcc_mean'], cg_df['rates'], color='blue', label='Computer Generated')
# plt.scatter(auth_df['mfcc_mean'], auth_df['rates'], color='red', label='Authentic')
# plt.scatter(rec_df['mfcc_mean'], rec_df['rates'], color='green', label='Recorded')
# plt.ylabel('Rates')
# plt.xlabel('MFCC')
# plt.legend()
# plt.show()
#
# plt.title('Rates VS Filter Bank')
# plt.scatter(cg_df['rates'], cg_df['fbank_mean'], color='blue', label='Computer Generated')
# plt.scatter(auth_df['rates'], auth_df['fbank_mean'], color='red', label='Authentic')
# plt.scatter(rec_df['rates'], rec_df['fbank_mean'], color='green', label='Recorded')
# plt.ylabel('Filter Bank')
# plt.xlabel('Rates')
# plt.legend()
# plt.show()
print()
return clf
def detect_fraud(input_audio):
features = ["rates", "mfcc_mean", "fbankmean"]
input_audio = input_audio[features]
clf = pd.read_pickle("data/classifiers/fraud_model")
result = clf.predict(input_audio)
if result == 1:
return "FRAUD"
else:
return "AUTHENTIC"
def extract_input_audio_features(audio):
df = pd.DataFrame()
rate, sig = wav.read(audio)
if len(sig) == 0:
return 0
mfcc_feature = mfcc(sig, rate, nfft=1200)
fbank_feat = logfbank(sig, rate, nfft=1200)
df['rates'] = [rate]
df['mfcc_mean'] = np.mean(mfcc_feature)
df['fbank_mean'] = np.mean(fbank_feat)
return df
def make_pickle(obj):
pickle_out = open("data/classifiers/fraud_model", "wb")
pickle.dump(obj, pickle_out)
pickle_out.close()
def send_results_to_hardware(input):
np.savetxt("output.txt", input, fmt='%s', delimiter=',')
def plot_result(data, cg_df, auth_df, rec_df, result="NONE"):
plt.title('Classification For Input Data')
plt.scatter(cg_df['mfcc_mean'], cg_df['fbank_mean'], color='blue')
plt.scatter(auth_df['mfcc_mean'], auth_df['fbank_mean'], color='red', label='Authentic')
plt.scatter(rec_df['mfcc_mean'], rec_df['fbank_mean'], color='blue', label='Fraud')
plt.scatter(data['mfcc_mean'], data['fbank_mean'], color='black', label='Input Data Point')
plt.text(data['mfcc_mean'] + 0.5, data['fbank_mean'], result, bbox=dict(facecolor='white', alpha=0.5))
plt.ylabel('Filter Bank')
plt.xlabel('MFCC')
plt.legend()
plt.show()
if __name__ == "__main__":
print()
cga_data = pd.read_csv("data/computer_generated.csv")[:290]
ra_data = pd.read_csv("data/recorded.csv")[:290]
aa_data = pd.read_csv("data/authentic.csv")[:290]
features = ["rates", "mfcc_mean", "fbank_mean"]
target = ["fraud"]
#
# # Test To Show Working Model
# random_recorded = pd.read_csv("data/recorded.csv")[features][290:].sample(1)
# random_auth = pd.read_csv("data/authentic.csv")[features][290:].sample(1)
# random_cg = pd.read_csv("data/computer_generated.csv")[features][290:].sample(1)
#
# test_data = random_auth
#
model = train_model(cga_data, aa_data, ra_data, features, target)
make_pickle(model)
#
# # input_audio_path = "data/fraud/recorded/*.wav"
# # for i in glob.glob(input_audio_path):
# # print(i)
# # data = extract_input_audio_features(i)
# # output = detect_fraud(i)
# # plot_result(data, cga_data, aa_data, ra_data, output)
#
# # example_audio = random.choice(glob.glob(input_audio_path))
# # test_data = extract_input_audio_features(example_audio)
# output = detect_fraud(test_data)
# plot_result(test_data, cga_data, aa_data, ra_data, output)