-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrap-d.py
80 lines (62 loc) · 2.17 KB
/
wrap-d.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
# given a silence classifier, use it to generate audacity files.
# user can adjust classifier as they want
# system input - wavFile long/short outfileFolder
import numpy as np
import os
import glob
import cPickle as pickle
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score
from sklearn.model_selection import ParameterGrid
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from pyAudioAnalysis import audioFeatureExtraction as aF
from pyAudioAnalysis import audioTrainTest as aT
from scipy import stats
import labelTools
import extractFeatures
import sys
stWin = extractFeatures.stWin
stStep = extractFeatures.stStep
inputWav = sys.argv[1]
try:
classModel = sys.argv[2]
outfileFolder = sys.argv[3]
speechModel = sys.argv[4]
except:
print 'System inputs not detected, using defaults'
classModel = 'models/class_short_short_class2.p'
outfileFolder = 'models'
speechModel = 'models/a_classifier_all.p'
clf = pickle.load(open(classModel,'r'))
clfSpeech = pickle.load(open(speechModel,'r'))
classDict = labelTools.classDict
classDictBinary = labelTools.classDictBinary
classDictTernary = labelTools.classDictTernary
thisClassDict = classDictTernary
X_test = extractFeatures.getRawStVectorPerWav(inputWav,stWin,stStep)
X_test = X_test.T
y_class_test = clf.predict(X_test)
y_class_sil = clfSpeech.predict(X_test)
y_out = y_class_test*y_class_sil
flag = 0
for i in xrange(y_out.shape[0]):
if y_out[i]==0:
if flag==1:
end=i
vote = stats.mode(y_out[start:end])[0][0]
y_out[start:end] = stats.mode(y_out[start:end])[0][0]
flag=0
else:
if flag==0:
start=i
flag=1
medianame = extractFeatures.basename(inputWav)
labelTools.writeToStm(y_out,thisClassDict,medianame,outfileFolder+'/'+medianame+'.stm')
labelTools.writeToAudacity(y_out,thisClassDict,outfileFolder+'/'+medianame+'.txt')
pickle.dump(y_out,open(outfileFolder+'/'+medianame+'_y_out.p','w'))
os.system('cp '+inputWav+' '+outfileFolder+'/'+medianame+'.wav')