-
Notifications
You must be signed in to change notification settings - Fork 9
/
analyzeMovieSound.py
195 lines (164 loc) · 5.99 KB
/
analyzeMovieSound.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
import os, sys, shutil, glob, numpy, csv, pickle
import scipy.io.wavfile as wavfile
from . import audioBasicIO
from . import audioTrainTest as aT
from . import audioSegmentation as aS
import matplotlib.pyplot as plt
import scipy.spatial.distance
minDuration = 7;
def classifyFolderWrapper(inputFolder, modelType, modelName, outputMode=False):
if not os.path.isfile(modelName):
raise Exception("Input modelName not found!")
if modelType=='svm':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = aT.loadSVModel(modelName)
elif modelType=='knn':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = aT.loadKNNModel(modelName)
PsAll = numpy.zeros((len(classNames), ))
files = "*.wav"
if os.path.isdir(inputFolder):
strFilePattern = os.path.join(inputFolder, files)
else:
strFilePattern = inputFolder + files
wavFilesList = []
wavFilesList.extend(glob.glob(strFilePattern))
wavFilesList = sorted(wavFilesList)
if len(wavFilesList)==0:
print("No WAV files found!")
return
Results = []
for wavFile in wavFilesList:
[Fs, x] = audioBasicIO.readAudioFile(wavFile)
signalLength = x.shape[0] / float(Fs)
[Result, P, classNames] = aT.fileClassification(wavFile, modelName, modelType)
PsAll += (numpy.array(P) * signalLength)
Result = int(Result)
Results.append(Result)
if outputMode:
print("{0:s}\t{1:s}".format(wavFile,classNames[Result]))
Results = numpy.array(Results)
# print distribution of classes:
[Histogram, _] = numpy.histogram(Results, bins=numpy.arange(len(classNames)+1))
if outputMode:
for i,h in enumerate(Histogram):
print("{0:20s}\t\t{1:d}".format(classNames[i], h))
PsAll = PsAll / numpy.sum(PsAll)
if outputMode:
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title("Classes percentage " + inputFolder.replace('Segments',''))
ax.axis((0, len(classNames)+1, 0, 1))
ax.set_xticks(numpy.array(list(range(len(classNames)+1))))
ax.set_xticklabels([" "] + classNames)
ax.bar(numpy.array(list(range(len(classNames))))+0.5, PsAll)
plt.show()
return classNames, PsAll
def getMusicSegmentsFromFile(inputFile):
modelType = "svm"
modelName = "data/svmMovies8classes"
dirOutput = inputFile[0:-4] + "_musicSegments"
if os.path.exists(dirOutput) and dirOutput!=".":
shutil.rmtree(dirOutput)
os.makedirs(dirOutput)
[Fs, x] = audioBasicIO.readAudioFile(inputFile)
if modelType=='svm':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = aT.loadSVModel(modelName)
elif modelType=='knn':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = aT.loadKNNModel(modelName)
flagsInd, classNames, acc, CM = aS.mtFileClassification(inputFile, modelName, modelType, plotResults = False, gtFile = "")
segs, classes = aS.flags2segs(flagsInd, mtStep)
for i, s in enumerate(segs):
if (classNames[int(classes[i])] == "Music") and (s[1] - s[0] >= minDuration):
strOut = "{0:s}{1:.3f}-{2:.3f}.wav".format(dirOutput+os.sep, s[0], s[1])
wavfile.write( strOut, Fs, x[int(Fs*s[0]):int(Fs*s[1])])
def analyzeDir(dirPath):
for i,f in enumerate(glob.glob(dirPath + os.sep + '*.wav')): # for each WAV file
getMusicSegmentsFromFile(f)
[c, P]= classifyFolderWrapper(f[0:-4] + "_musicSegments", "svm", "data/svmMusicGenre8", False)
if i==0:
print("".ljust(100)+"\t", end=' ')
for C in c:
print(C.ljust(12)+"\t", end=' ')
print()
print(f.ljust(100)+"\t", end=' ')
for p in P:
print("{0:.2f}".format(p).ljust(12)+"\t", end=' ')
print()
def main(argv):
if argv[1]=="--file":
getMusicSegmentsFromFile(argv[2])
classifyFolderWrapper(argv[2][0:-4] + "_musicSegments", "svm", "data/svmMusicGenre8", True)
elif argv[1]=="--dir":
analyzeDir(argv[2])
elif argv[1]=="--sim":
csvFile = argv[2]
f = []
fileNames = []
with open(csvFile, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
for j,row in enumerate(spamreader):
if j>0:
ftemp = []
for i in range(1,9):
ftemp.append(float(row[i]))
f.append(ftemp)
R = row[0]
II = R.find(".wav");
fileNames.append(row[0][0:II])
f = numpy.array(f)
Sim = numpy.zeros((f.shape[0], f.shape[0]))
for i in range(f.shape[0]):
for j in range(f.shape[0]):
Sim[i,j] = scipy.spatial.distance.cdist(numpy.reshape(f[i,:], (f.shape[1],1)).T, numpy.reshape(f[j,:], (f.shape[1],1)).T, 'cosine')
Sim1 = numpy.reshape(Sim, (Sim.shape[0]*Sim.shape[1], 1))
plt.hist(Sim1)
plt.show()
fo = open(csvFile + "_simMatrix", "wb")
pickle.dump(fileNames, fo, protocol = pickle.HIGHEST_PROTOCOL)
pickle.dump(f, fo, protocol = pickle.HIGHEST_PROTOCOL)
pickle.dump(Sim, fo, protocol = pickle.HIGHEST_PROTOCOL)
fo.close()
elif argv[1]=="--loadsim":
try:
fo = open(argv[2], "rb")
except IOError:
print("didn't find file")
return
try:
fileNames = pickle.load(fo)
f = pickle.load(fo)
Sim = pickle.load(fo)
except:
fo.close()
fo.close()
print(fileNames)
Sim1 = numpy.reshape(Sim, (Sim.shape[0]*Sim.shape[1], 1))
plt.hist(Sim1)
plt.show()
elif argv[1]=="--audio-event-dir":
files = "*.wav"
inputFolder = argv[2]
if os.path.isdir(inputFolder):
strFilePattern = os.path.join(inputFolder, files)
else:
strFilePattern = inputFolder + files
wavFilesList = []
wavFilesList.extend(glob.glob(strFilePattern))
wavFilesList = sorted(wavFilesList)
for i,w in enumerate(wavFilesList):
[flagsInd, classesAll, acc, CM] = aS.mtFileClassification(w, "data/svmMovies8classes", "svm", False, '')
histTemp = numpy.zeros( (len(classesAll), ) )
for f in flagsInd:
histTemp[int(f)] += 1.0
histTemp /= histTemp.sum()
if i==0:
print("".ljust(100)+"\t", end=' ')
for C in classesAll:
print(C.ljust(12)+"\t", end=' ')
print()
print(w.ljust(100)+"\t", end=' ')
for h in histTemp:
print("{0:.2f}".format(h).ljust(12)+"\t", end=' ')
print()
return 0
if __name__ == '__main__':
main(sys.argv)