-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
347 lines (252 loc) · 9.03 KB
/
preprocess.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
import time
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import os
import re
from PIL import Image
import cv2
from multiprocessing import Pool
root = "data"
output_path = "build/processed"
USE_GRAYSCALE = True
N_JOBS = 6
ISOLATE_VOICE = False
def extrac_voice(y, sr, duration):
S_full, phase = librosa.magphase(librosa.stft(y))
S_filter = librosa.decompose.nn_filter(S_full, aggregate=np.median, metric='cosine',)
S_filter = np.minimum(S_full, S_filter)
margin_i, margin_v = 2, 10
power = 2
mask_v = librosa.util.softmask(S_full - S_filter,
margin_v * S_filter,
power=power)
# Once we have the masks, simply multiply them with the input spectrum
# to separate the components
S_foreground = mask_v * S_full
D_foreground = S_foreground * phase
y_foreground = librosa.istft(D_foreground)
return y_foreground
def audio_to_mel_spectrogram(input_audio_path:str, output_image_path:str, duration=1.75, dpi=1, force=False):
if not force and os.path.exists(output_image_path):
return
# Load the audio file, automatically resampled to
y, sr = librosa.load(
input_audio_path,
#duration=duration,
mono=True,
sr=44100
)
# Trim silent parts
_, index = librosa.effects.trim(y)
y = y[index[0]:index[1]] # Using directly the function's return doesn't work
file_duration = librosa.get_duration(y=y, sr=sr)
if file_duration < duration:
print(f"Audio duration is shorter than {duration} seconds. Skipping spectrogram generation.")
return
# Extract voice
if ISOLATE_VOICE:
y = extrac_voice(y, sr, duration)
chunks = int(file_duration // duration)
duration_in_samples = librosa.time_to_samples(duration, sr=sr)
for i in range(0, chunks):
# cut
y_cut = y[duration_in_samples*i:duration_in_samples*(i+1)]
# Compute the Mel spectrogram
mel_spectrogram = librosa.feature.melspectrogram(y=y_cut, sr=sr)
# Convert to decibels (log scale)
mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)
spectrogram_to_png(mel_spectrogram_db, output_image_path, dpi)
def spectrogram_to_png(mel_spectrogram_db, output_image_path: str, dpi):
assert mel_spectrogram_db.min() >= -80.0 and abs(mel_spectrogram_db.max()) < 1e-3
mel_spectrogram_db = mel_spectrogram_db * (255.0/-80.0)
mel_spectrogram_db = np.maximum(0, mel_spectrogram_db).transpose()
i = Image.fromarray(mel_spectrogram_db.astype(np.uint8), mode = "L")
i.save(output_image_path)
i.close()
kraut_emotions_codes = {
"W": "ANGER",
"L": "BOREDOM",
"E": "DISGUST",
"A": "ANXIETY",
"F": "HAPPINESS",
"T": "SADNESS",
"N": "NEUTRAL"
}
dataset_path = os.path.join(root, "emo-db/")
for key, value in kraut_emotions_codes.items():
folder_name = os.path.join(output_path, value)
os.makedirs(folder_name, exist_ok=True)
os.makedirs(os.path.join(output_path, "SURPRISE"), exist_ok=True)
def job_kraut(audio_file):
pre, ext = os.path.splitext(audio_file)
# in this dataset the emotionCode is written inside the name
emotionCode = pre[5]
emotion = kraut_emotions_codes[emotionCode]
out_file = f"{emotion}/kraut_{pre}.png"
out_path = os.path.join(output_path, out_file)
audio_path = os.path.join(dataset_path, audio_file)
audio_to_mel_spectrogram(
input_audio_path = audio_path,
output_image_path = out_path,
)
with Pool(N_JOBS) as p:
p.map(job_kraut, os.listdir(dataset_path))
emovo_emotions_codes = {
"neu": "NEUTRAL",
"dis": "DISGUST",
"gio": "HAPPINESS",
"pau": "ANXIETY",
"rab": "ANGER",
"sor": "SURPRISE",
"tri": "SADNESS"
}
dataset_path = os.path.join(root, "EMOVO/")
def job_emovo(audio_file):
emotionCode = audio_file[0:3]
if emotionCode not in emovo_emotions_codes:
print("Unknown emotion " + emotionCode)
return
pre, ext = os.path.splitext(audio_file)
out_path = os.path.join(output_path, emovo_emotions_codes[emotionCode], f"emovo_{pre}.png")
audio_path = os.path.join(folderpath, audio_file)
audio_to_mel_spectrogram(
input_audio_path=audio_path,
output_image_path=out_path,
)
for actor_folder in ["m3","m2","m1","f3","f2","f1"]:
folderpath = os.path.join(dataset_path, actor_folder)
print("Analysis of " + folderpath)
with Pool(N_JOBS) as p:
p.map(job_emovo, os.listdir(folderpath))
import gc
emovdb_emotions_codes = {
"bea_Amused" : "HAPPINESS",
"bea_Angry" : "ANGER",
"bea_Disgusted" : "DISGUST",
"bea_Neutral" : "NEUTRAL",
"bea_Sleepy" : "BOREDOM",
"jenie_Amused" : "HAPPINESS",
"jenie_Angry" : "ANGER",
"jenie_Disgusted" : "DISGUST",
"jenie_Neutral" : "NEUTRAL",
"josh_Amused" : "HAPPINESS",
"josh_Neutral" : "NEUTRAL",
"josh_Sleepy" : "BOREDOM",
"sam_Disgusted" : "DISGUST",
"sam_Neutral" : "NEUTRAL",
"sam_Sleepy" : "BOREDOM",
}
dataset_path = os.path.join(root, "EmoV-DB/")
def job_emovdb(audiofilename):
pre, ext = os.path.splitext(audiofilename)
# Generate and save to disk
audio_path = os.path.join(subfolder_name, audiofilename)
output_filename = os.path.join(output_path, f"{emotion}/emovodb_{pre}.png")
audio_to_mel_spectrogram(
input_audio_path=audio_path,
output_image_path=output_filename,
)
for folder_name, emotion in emovdb_emotions_codes.items():
subfolder_name = os.path.join(dataset_path, folder_name)
with Pool(N_JOBS) as p:
p.map(job_emovdb, os.listdir(subfolder_name))
RE_EXTRACT_EMOTION = re.compile(r"\w+_(\w+)_.*_.*\.wav")
# 5 primary emotions: angry, sad, neutral, happy, excited. 5 secondary emotions: anxious, apologetic, pensive, worried, enthusiastic.
jl_emotions_codes = {
"angry": "ANGER",
"anxious": "ANXIETY",
"happy": "HAPPINESS",
"sad": "SADNESS",
"neutral": "NEUTRAL",
"excited": "HAPPINESS", # @TODO DECIDE IF TRUE LOL
"pensive": "BOREDOM",
"enthusiastic": "SURPRISE"
}
dataset_path = os.path.join(root, "jl-corpus/")
def job_jl(audio_file):
pre, ext = os.path.splitext(audio_file)
# in this dataset the emotionCode is written inside the name
emotionCode = RE_EXTRACT_EMOTION.findall(audio_file)[0]
if emotionCode not in jl_emotions_codes:
print("Unknown emotion " + emotionCode)
return
out_path = os.path.join(output_path, jl_emotions_codes[emotionCode], f"jl_{pre}.png")
audio_path = os.path.join(dataset_path, audio_file)
audio_to_mel_spectrogram(
input_audio_path = audio_path,
output_image_path = out_path,
)
with Pool(N_JOBS) as p:
p.map(job_jl, os.listdir(dataset_path))
# anger disgust fear joy neutral sadness surprise
meld_emotions_codes = {
"joy": "HAPPINESS",
"anger": "ANGER",
"disgust": "DISGUST",
"neutral": "NEUTRAL",
"sadness": "SADNESS",
"surprise": "SURPRISE"
}
dataset_path = os.path.join(root, "MELD.proc/")
RE_EXTRACT_EMOTION_MELD = re.compile(r"\d+_(\w+)_.*")
def job_meld(audio_file):
pre, ext = os.path.splitext(audio_file)
# in this dataset the emotionCode is written inside the name
emotionCode = RE_EXTRACT_EMOTION_MELD.findall(audio_file)[0]
if emotionCode not in meld_emotions_codes:
print("Unknown emotion " + emotionCode)
return
out_path = os.path.join(output_path, meld_emotions_codes[emotionCode], f"meld_{pre}.png")
audio_path = os.path.join(dataset_path, audio_file)
audio_to_mel_spectrogram(
input_audio_path = audio_path,
output_image_path = out_path,
)
with Pool(N_JOBS) as p:
p.map(job_meld, os.listdir(dataset_path))
# Emotion (01 = neutral, 02 = calm, 03 = happy, 04 = sad, 05 = angry, 06 = fearful, 07 = disgust, 08 = surprised).
# Emotional intensity (01 = normal, 02 = strong). NOTE: There is no strong intensity for the 'neutral' emotion.
ravdess_emotions_codes = {
"05": "ANGER",
"07": "DISGUST",
"06": "ANXIETY",
"03": "HAPPINESS",
"04": "SADNESS",
"01": "NEUTRAL",
"08": "SURPRISE"
}
dataset_path = os.path.join(root, "RAVDESS/")
RE_EXTRACT_EMOTION_RAVDESS= re.compile(r"\d+-\d+-(\d+)-.*")
def job_ravdess(audio_file):
pre, ext = os.path.splitext(audio_file)
# in this dataset the emotionCode is written inside the name
emotionCode = RE_EXTRACT_EMOTION_RAVDESS.findall(audio_file)[0]
if emotionCode not in ravdess_emotions_codes:
print("Unknown emotion " + emotionCode)
return
out_path = os.path.join(output_path, ravdess_emotions_codes[emotionCode], f"ravdess_{pre}.png")
audio_path = os.path.join(dataset_path, audio_file)
audio_to_mel_spectrogram(
input_audio_path = audio_path,
output_image_path = out_path,
)
#with Pool(N_JOBS) as p:
# p.map(job_ravdess, os.listdir(dataset_path))
count = {}
for foldername in os.listdir(output_path):
folderpath = os.path.join(output_path, foldername)
if not os.path.isdir(folderpath):
continue
count[foldername] = 0
print("Analysis of " + folderpath)
for imagename in os.listdir(folderpath):
imagepath = os.path.join(folderpath, imagename)
pic = cv2.imread(imagepath)
if pic is None:
print("#### CORRUPTED #### " + imagepath)
os.remove(imagepath)
continue
count[foldername] += 1
print(count)