-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_extractor.py
More file actions
53 lines (44 loc) · 1.87 KB
/
feature_extractor.py
File metadata and controls
53 lines (44 loc) · 1.87 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
import os
from flask import Flask, request, render_template, redirect, send_from_directory
from werkzeug.utils import secure_filename
from pyAudioAnalysis import audioBasicIO
from pyAudioAnalysis import audioFeatureExtraction
from pyAudioAnalysis import audioBasicIO
import numpy as np
import csv
from sklearn.externals import joblib
from pydub import AudioSegment
import sys
def extract_all(filepath="./",filename="./features.csv"):
filenames = [os.path.join(filepath,name) for name in os.listdir(filepath) if not name == '.DS_Store' and name.__contains__(".wav")]
only_names = [name for name in os.listdir(filepath) if not name == '.DS_Store' and name.__contains__(".wav")]
for i in filenames:
features= featureExtraction(i)
features = np.asarray(features).reshape(len(features), -1)
with open(filename, 'a') as f:
w_f = csv.writer(f)
line_to_write = [i.replace(filepath,"")[1:]]
#print np.shape(features)
for k in range(len(features)):
for j in range(0,34):
line_to_write.append(features[k,j])
w_f.writerow(line_to_write)
def featureExtraction(file_path):
print file_path
[Fs, x] = audioBasicIO.readAudioFile(file_path)
#print x
features = audioFeatureExtraction.stFeatureExtraction(x, Fs, 0.05 * Fs, 0.025 * Fs)
if (len(features) == 0 or features.shape[1] < 50):
features = 'none'
else:
#features = np.mean(features, axis=1)
features = np.asarray(features).reshape(len(features), -1).transpose()
return features
if __name__ == "__main__":
if sys.argv[1]==None:
extract_all()
elif sys.argv[1]!=None and sys.argv[2]==None:
extract_all(sys.argv[1])
else:
print "Starting Extraction"
extract_all(sys.argv[1], sys.argv[2])