-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
executable file
·58 lines (52 loc) · 1.7 KB
/
Copy pathpreprocess.py
File metadata and controls
executable file
·58 lines (52 loc) · 1.7 KB
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
#!/usr/bin/env python
import librosa.display, glob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import csv, pickle
import os, sys, subprocess
import threading
from queue import Queue
import util
import time
def process(item_id, q, oq):
filename = "songs/XC{}.mp3".format(item_id, q, oq)
mfccs = None
try:
data, sample_rate = librosa.load(filename)
# Extract Mel Frequency Cepstral Coefficients
mfccs = np.mean(librosa.feature.mfcc(y=data, sr=sample_rate, n_mfcc=40).T, axis=0)
except Exception as err:
print("Error parsing {}: {}".format(filename, err))
q.get()
oq.put((item_id, mfccs))
def main():
threads = []
metadata = util.get_metadata()
q = Queue(maxsize=7)
oq = Queue()
X = []
for item in metadata[1:]:
q.put(1)
time.sleep(0.1)
t = threading.Thread(target=process, args=(item[0], q, oq))
t.start()
threads.append(t)
print("{}/{}".format(oq.qsize(), len(metadata) - 1), end='\r', flush=True)
for thread in threads:
print("{}/{}".format(oq.qsize(), len(metadata) - 1), end='\r', flush=True)
thread.join()
features = dict()
while not oq.empty():
item_id, feature = oq.get()
features[item_id] = feature
for item in metadata[1:]:
if item[0] in features and features[item[0]] is not None:
X.append(features[item[0]])
else:
# Note: Items with missing features will need to be removed from metadata.csv to preserve (X,Y) order
print("Error getting features for {}".format(item[0]))
with open("features", "wb") as f:
pickle.dump(X, f)
if __name__ == "__main__":
main()