-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio_data.py
208 lines (161 loc) · 5.78 KB
/
audio_data.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
import glob, os, pickle
import scipy.io.wavfile as wav
import pandas as pd
import numpy as np
from python_speech_features import mfcc, logfbank
from pydub import AudioSegment
from scipy.signal import stft
from sklearn.preprocessing import LabelEncoder
from audio_fraud_detector import detect_fraud
from speakerrecognition import identify_speaker
def create_data(data_dir):
recorded_dir = os.path.join(data_dir, "fraud/recorded")
recorded_dir = os.path.realpath(recorded_dir)
auth_dir = os.path.join(data_dir, "authentic")
auth_dir = os.path.realpath(auth_dir)
original_dir = os.path.join(data_dir, "original")
data_dir = os.path.realpath(data_dir)
path = os.path.realpath(original_dir)
files = os.listdir(os.fsencode(path))
# iterate over wav files
for file in files:
file = file.decode("utf-8")
filename = get_filename(file)
file = path + "/" + file
# full numbers in the 1000s means seconds
if filename.endswith("wav"):
start = 0
end = 6000
num_index = get_num_index(filename)
dot_index = get_dot_index(filename)
person = filename[:num_index]
new_filename = filename[:dot_index]
file = open(file, "rb")
sound = AudioSegment.from_wav(file)
while end <= len(sound):
new_file = sound[start:end]
new_filename += str(int((start/1000))) + "-" + str(int((end/1000))) + "seconds.wav"
new_file.export(data_dir + "/" + person + "/" + new_filename, format="wav")
if is_fraud(filename)[0]:
new_file.export(recorded_dir + "/" + new_filename, format="wav")
else:
new_file.export(auth_dir + "/" + new_filename, format="wav")
start = end
end += 6000
new_filename = filename[:dot_index]
file.close()
def get_num_index(string):
for i in range(len(string)):
if string[i].isdigit():
return i
return -1
def get_dot_index(string):
return string.index(".")
def get_person_dir(person, data_dir):
return os.path.join(data_dir,person)
def is_fraud(filename):
if "record" in filename:
return True, 1
elif "cg" in filename:
return True, 2
else:
return False, 0
def get_filename(file, person):
if person == "yesha":
lastchar_index = file.index(person) + len(person) - 1 + 2
else:
lastchar_index = file.index(person[-1]) + 2
return file[lastchar_index:]
def make_pickle(name, obj):
pickle_out = open(name, "wb")
pickle.dump(obj, pickle_out)
pickle_out.close()
def create_dataframe(data_dir):
rows = []
mono = None
for index, person in enumerate(os.listdir(data_dir)):
if person in ["authentic", "original", "fraud", "audio_data.csv", "classifiers"]:
continue
person_dir = get_person_dir(person, data_dir)
path = person_dir + "/*.wav"
for audio_file in glob.glob(path):
print(audio_file)
row = {}
filename = get_filename(audio_file, person)
row["filename"] = filename
row["file"] = audio_file
row["person"] = person
row["speaker_num"] = index
audio_file = open(audio_file, "rb")
rate, stereo = wavf.read(audio_file)
if len(stereo) == 0:
continue
if not isinstance(stereo[0], np.ndarray):
mono = stereo
else:
mono = stereo[:, 0]
row["rate"] = rate
f, t, Zxx = stft(mono, rate, nperseg=200)
temp = pd.DataFrame(Zxx.T).abs().mean().values
count = 1
for val in temp:
row[count] = val
count += 1
fbank_mean = np.mean(logfbank(stereo, rate, nfft=1200))
row["fbankmean"] = fbank_mean
mfcc_mean = np.mean(mfcc(stereo, rate, nfft=1200))
row["mfcc_mean"] = mfcc_mean
fraud_value, fraud_type = is_fraud(filename)
if fraud_value:
if fraud_type == 1:
row["recorded"] = 1
row["computer_generated"] = 0
else:
row["recorded"] = 0
row["computer_generated"] = 1
row["fraud"] = 1
row["authentic"] = 0
else:
row["authentic"] = 1
row["fraud"] = 0
row["computer_generated"] = 0
row["recorded"] = 0
rows.append(row)
df = pd.DataFrame(rows)
encoder = LabelEncoder()
df["speaker_num"] = encoder.fit_transform(df['speaker_num'])
df = df.reindex(sorted(df.columns), axis=1)
return df
def get_data():
csv = "data/audio_data.csv"
df = pd.read_csv(csv)
return df
def create_csv(df, filename):
df.to_csv(filename)
def process_audio(file):
df = extract_input_audio_features(file)
fraudulent = detect_fraud(df)
speaker = identify_speaker(df)
result = speaker + " , " + fraudulent
return result
def extract_input_audio_features(audio):
row = {}
rate, stereo = wav.read(audio)
row['rates'] = [rate]
if not isinstance(stereo[0], np.ndarray):
mono = stereo
else:
mono = stereo[:, 0]
row["rate"] = [rate]
f, t, Zxx = stft(mono, rate, nperseg=200)
temp = pd.DataFrame(Zxx.T).abs().mean().values
count = 1
for val in temp:
row[count] = [val]
count += 1
fbank_feat = logfbank(stereo, rate, nfft=1200)
row['fbankmean'] = np.mean(fbank_feat)
mfcc_feature = mfcc(stereo, rate, nfft=1200)
row['mfcc_mean'] = np.mean(mfcc_feature)
df = pd.DataFrame(row)
return df