-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerateSpectrograms.py
173 lines (145 loc) · 8.59 KB
/
generateSpectrograms.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
#!/usr/bin/env python2.7
import scipy.misc
import argparse
import os
import sys
import audioop
import numpy
import glob
import scipy
import subprocess
import wave
import cPickle
import threading
import shutil
import ntpath
import random
import matplotlib.pyplot as plt
from pyAudioAnalysis import audioFeatureExtraction as aF
from pyAudioAnalysis import audioTrainTest as aT
from pyAudioAnalysis import audioSegmentation as aS
from pyAudioAnalysis import audioVisualization as aV
from pyAudioAnalysis import audioBasicIO
from pyAudioAnalysis import utilities as uT
import scipy.io.wavfile as wavfile
import matplotlib.patches
import Image
import cv2
import matplotlib.cm
def createSpectrogramFile(x, Fs, fileName, stWin, stStep):
specgramOr, TimeAxis, FreqAxis = aF.stSpectogram(x, Fs, round(Fs * stWin), round(Fs * stStep), False)
print specgramOr.shape
if inputs[2]=='full':
print specgramOr
numpy.save(fileName.replace('.png','')+'_spectrogram', specgramOr)
else:
#specgram = scipy.misc.imresize(specgramOr, float(227.0) / float(specgramOr.shape[0]), interp='bilinear')
specgram = cv2.resize(specgramOr,(227, 227), interpolation = cv2.INTER_LINEAR)
im1 = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
scipy.misc.imsave(fileName, im1)
def main(argv):
if argv[2]=='full':
dirName = argv[1]
types = ('*.wav', )
filesList = []
for files in types:
filesList.extend(glob.glob(os.path.join(dirName, files)))
filesList = sorted(filesList)
filesListIrr = []
filesListIrr = sorted(filesListIrr)
stWin = 0.020
stStep = 0.015
for f in filesList:
[Fs, x] = audioBasicIO.readAudioFile(f)
x = audioBasicIO.stereo2mono(x)
createSpectrogramFile(x, Fs, f.replace(".wav",".png"), stWin, stStep)
else:
dirName = argv[1]
dirNameIrrelevant = argv[2]
types = ('*.wav', )
filesList = []
for files in types:
filesList.extend(glob.glob(os.path.join(dirName, files)))
filesList = sorted(filesList)
filesListIrr = []
for files in types:
filesListIrr.extend(glob.glob(os.path.join(dirNameIrrelevant, files)))
filesListIrr = sorted(filesListIrr)
print filesListIrr
WIDTH_SEC = 1.5
stWin = 0.040
stStep = 0.005
WIDTH = WIDTH_SEC / stStep
for f in filesList:
print f
[Fs, x] = audioBasicIO.readAudioFile(f)
x = audioBasicIO.stereo2mono(x)
x = x.astype(float) / x.max()
for i in range(3):
if x.shape[0] > WIDTH_SEC * Fs + 200:
randStartSignal = random.randrange(0, int(x.shape[0] - WIDTH_SEC * Fs - 200) )
x2 = x[randStartSignal : randStartSignal + int ( (WIDTH_SEC + stStep) * Fs) ]
createSpectrogramFile(x2, Fs, f.replace(".wav",".png"), stWin, stStep) # ORIGINAL
if len(dirNameIrrelevant) > 0:
# AUGMENTED
randIrrelevant = random.randrange(0, len(filesListIrr))
[Fs, xnoise] = audioBasicIO.readAudioFile(filesListIrr[randIrrelevant])
xnoise = xnoise.astype(float) / xnoise.max()
randStartNoise = random.randrange(0, xnoise.shape[0] - WIDTH_SEC * Fs - 200)
R = 5; xN = (R * x2.astype(float) + xnoise[randStartNoise : randStartNoise + x2.shape[0]].astype(float)) / float(R+1)
wavfile.write(f.replace(".wav","_rnoise{0:d}1.wav".format(i)), Fs, (16000 * xN).astype('int16'))
createSpectrogramFile(xN, Fs, f.replace(".wav","_rnoise{0:d}1.png".format(i)), stWin, stStep)
randStartNoise = random.randrange(0, xnoise.shape[0] - WIDTH_SEC * Fs - 200)
R = 4; xN = (R * x2.astype(float) + xnoise[randStartNoise : randStartNoise + x2.shape[0]].astype(float)) / float(R+1)
wavfile.write(f.replace(".wav","_rnoise{0:d}2.wav".format(i)), Fs, (16000 * xN).astype('int16'))
createSpectrogramFile(xN, Fs, f.replace(".wav","_rnoise{0:d}2.png".format(i)), stWin, stStep)
randStartNoise = random.randrange(0, xnoise.shape[0] - WIDTH_SEC * Fs - 200)
R = 3; xN = (R * x2.astype(float) + xnoise[randStartNoise : randStartNoise + x2.shape[0]].astype(float)) / float(R+1)
wavfile.write(f.replace(".wav","_rnoise{0:d}3.wav".format(i)), Fs, (16000 * xN).astype('int16'))
createSpectrogramFile(xN, Fs, f.replace(".wav","_rnoise{0:d}3.png".format(i)), stWin, stStep)
#specgramOr, TimeAxis, FreqAxis = aF.stSpectogram(x2, Fs, round(Fs * stWin), round(Fs * stStep), False)
#im2 = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
#plt.subplot(2,1,1)
#plt.imshow(im1)
#plt.subplot(2,1,2)
#plt.imshow(im2)
#plt.show()
'''
if int(specgramOr.shape[0]/2) - WIDTH/2 - int((0.2) / stStep) > 0:
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 - int((0.2) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 - int((0.2) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
print specgram.shape
scipy.misc.imsave(f.replace(".wav","_02A.png"), im)
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 + int((0.2) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 + int((0.2) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
print specgram.shape
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
scipy.misc.imsave(f.replace(".wav","_02B.png"), im)
# ONLY FOR SPEECH (fewer samples). Must comment for music
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 - int((0.1) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 - int((0.1) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
print specgram.shape
scipy.misc.imsave(f.replace(".wav","_01A.png"), im)
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 + int((0.1) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 + int((0.1) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
print specgram.shape
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
scipy.misc.imsave(f.replace(".wav","_01B.png"), im)
if int(specgramOr.shape[0]/2) - WIDTH/2 - int((0.5) / stStep) > 0:
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 - int((0.5) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 - int((0.5) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
print specgram.shape
scipy.misc.imsave(f.replace(".wav","_02A.png"), im)
specgram = specgramOr[int(specgramOr.shape[0]/2) - WIDTH/2 + int((0.5) / stStep):int(specgramOr.shape[0]/2) + WIDTH/2 + int((0.5) / stStep), :]
specgram = scipy.misc.imresize(specgram, float(227.0) / float(specgram.shape[0]), interp='bilinear')
print specgram.shape
im = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255))
scipy.misc.imsave(f.replace(".wav","_02B.png"), im)
'''
if __name__ == '__main__':
inputs = sys.argv
global inputs
main(inputs)