-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_organizer.py
65 lines (52 loc) · 2.18 KB
/
file_organizer.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
import os
import time
import shutil
import librosa
import numpy as np
import matplotlib.pyplot as plt
def generate_spectogram_for_g(g):
j = 0
print(g)
for filename in os.listdir("files"):
if filename.endswith(".wav") and g in filename:
start_time = time.time()
song = os.path.join("files", filename)
j = j+1
x, sr = librosa.load(song, sr = None)
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='log')
plt.axis('off')
#check if song file exists
if os.path.isfile(filename.replace(".wav", ".jpg")):
continue
else:
print("saved {}".format(filename))
plt.savefig(os.path.join("files", filename.replace(".wav", ".jpg")), bbox_inches='tight')
plt.cla()
print("--- %s seconds ---" % (time.time() - start_time))
genres = ["classic", 'country', 'edm', 'flamenco', 'hiphop', 'indie', 'jazz', 'kpop', 'latin', 'metal', 'pop', 'rap', 'reggae', 'rock']
#We will use 3200 samples per class, split into 80% training, 10% validation, and 10% testing
samples = 1000
validation_split = 0.1
testing_split = 0.1
training_split = 0.8
path = '9_sec_dataset'
os.chdir(path)
for genre in genres:
print('Processing ' + genre)
temp_list = []
for filename in os.listdir(genre):
if filename.endswith(".jpg"):
temp_list.append(filename)
#randomly shuffle elements in temp_list
np.random.shuffle(temp_list)
validation = temp_list[:int(samples * validation_split)]
testing = temp_list[int(samples * validation_split) : int(samples * validation_split + samples * testing_split)]
training = temp_list[int(samples * validation_split + samples * testing_split) : int(samples * validation_split + samples * testing_split + samples * training_split)]
for image in validation:
shutil.move(os.path.join(genre, image), os.path.join("validation", genre, image))
for image in testing:
shutil.move(os.path.join(genre, image), os.path.join("testing", genre, image))
for image in training:
shutil.move(os.path.join(genre, image), os.path.join("training", genre, image))