-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
221 lines (177 loc) · 6.97 KB
/
backend.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import warnings
import wave
import sys
import os
import numpy as np
import pylab
import math
import matplotlib
import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile
warnings.filterwarnings("ignore")
def interesting_points_finder(filestring, freq_min, freq_max, pot_min):
nomewav = os.path.basename(filestring)
pathwav = os.path.dirname(filestring)
filename = os.path.splitext(nomewav)[0]
if not os.path.exists(pathwav+"/"+filename+"/"):
os.makedirs(pathwav+"/"+filename+"/")
if os.path.exists(pathwav+"/"+filename+"/Spec"):
print("skipped")
return
win = wave.open(filestring, 'r')
sampFreq = win.getframerate()
windowsize = 0.001 #tamanho da janela
audiolen = int(windowsize * sampFreq)
timelapse = 0.000
time_ms = win.getnframes()*1000/win.getframerate()
timeStamps = []
while int((timelapse+0.001) * sampFreq) < win.getnframes():
win.readframes(int(timelapse * sampFreq))
snd = np.fromstring(win.readframes(audiolen), dtype=np.int16)
n = len(snd)
p = np.fft.fft(snd)
nUniquePts = int(math.ceil((n+1)/2.0))
p = p[0:nUniquePts]
#p = abs(p)
p = p / float(n)
p = p**2
if n % 2 > 0:
p[1:len(p)] = p[1:len(p)] * 2
else:
p[1:len(p) -1] = p[1:len(p) - 1] * 2
freqArray = np.arange(0, nUniquePts, 1.0) * (sampFreq / n);
count = 0
out_range_count = 0
for i in range(0, nUniquePts):
if freqArray[i] > int(freq_min) and freqArray[i] < int(freq_max): #range de Frenquencias
if abs(p[i]) > float(pot_min): #range de decibeis
#print "(time_b: %f), (duration: %f), (p: %f), (freq: %d)" %(timelapse, windowsize, abs(p[i]), freqArray[i])
count = count + 1
else:
if abs(p[i]) > float(pot_min):
out_range_count = out_range_count + 1
if count > 10 and ((count + out_range_count) < (nUniquePts - 2)):
# storing the initial value of frequency
timeStamps.append(float("{0:.4f}".format(timelapse)))
timelapse+= 0.001
frequenciasTemporarias=[]
potenciasTemporarias=[]
win.close()
win = wave.open(filestring, 'rb')
win.close()
timestampsfile = open(pathwav+"/"+filename+"/"+filename+'Timestamps.txt','w')
timestampsfile.write(str(timeStamps))
timestampsfile.close()
def time_stamps_cropper(filestring):
import sys
import os
import wave
if (filestring.find('wav') > 0 or filestring.find('WAV') > 0):
nomewav = os.path.basename(filestring)
pathwav = os.path.dirname(filestring)
filename = os.path.splitext(nomewav)[0]
if os.path.exists(pathwav+"/"+filename+"/Spec"):
print("skipped")
return
fname = open(pathwav+'/'+filename+'/'+filename+'Timestamps.txt').read().split(', ')
fname[0] = fname[0].translate(None, '[')
fname[len(fname)-1] = fname[len(fname)-1].translate(None, ']\n')
timestamps = []
stop = 0
while stop < len(fname):
count = 0
if stop < len(fname)-2:
while int(float(fname[stop])*1000) + 1 == int(float(fname[stop+1])*1000) and stop+1 < len(fname)-1:
count = count + 1
stop = stop + 1
if int(float(fname[stop])*1000) + 1 == int(float(fname[stop+1])*1000) and stop+1 == len(fname)-1:
count = count + 1
stop = stop + 1
if count > 0:
index = int((count)/2)
timestamps.append(float(fname[stop-index]))
else:
timestamps.append(float(fname[stop]))
stop = stop + 1
#print timestamps
for i in range(0, len(timestamps)):
#print timestamps[i]
win = wave.open(filestring, 'rb')
wout = wave.open(pathwav+"/"+filename+"/"+filename+"I"+str(i)+".WAV", 'wb')
timelapse = timestamps[i] - 0.003
if timelapse > 0.0:
win.readframes(int(timelapse * win.getframerate()))
cropedwavframes = win.readframes(int(0.006 * win.getframerate()))
wout.setparams(win.getparams())
wout.writeframes(cropedwavframes)
wout.close()
win.close()
print "Audios das timestamps extraidos"
def raw_specs(filestring):
from scikits.audiolab import wavread
import pylab
import matplotlib.pyplot as plt
import sys
import os
if (filestring.find('wav') > 0 or filestring.find('WAV') > 0):
nomewav = os.path.basename(filestring)
pathwav = os.path.dirname(filestring)
filename = os.path.splitext(nomewav)[0]
maindir = pathwav+"/"+filename+"/"
if os.path.exists(maindir+"Spec"):
print("skipped")
return
for fnamefiles in os.listdir(maindir):
if os.path.isdir(maindir + fnamefiles) or os.stat(maindir+fnamefiles).st_size == 0:
print "not a file."
else:
if fnamefiles.find('wav') > 0 or fnamefiles.find('WAV') > 0:
if not os.path.exists(maindir+"/Spec/"):
os.makedirs(maindir+"/Spec/")
signal, fs, enc = wavread(maindir+fnamefiles);
NFFT = 256 # the length of the windowing segments
Fs = int(300) # the sampling frequency
pylab.figure(num=None, figsize=(4, 8),frameon=False)
Pxx, freqs, bins, im = pylab.specgram(signal, NFFT=NFFT, Fs=Fs, noverlap=int(NFFT-1),cmap=pylab.cm.gist_heat)
if fnamefiles.find('wav') > 0:
figname = maindir+"/Spec/"+os.sep+fnamefiles.replace('wav','png')
else:
figname = maindir+"/Spec/"+os.sep+fnamefiles.replace('WAV','png')
pylab.savefig(figname)
plt.close('all')
print "Spectrogramas gerados."
def crop_specs(filestring):
import sys
import os
import cv2
nomewav = os.path.basename(filestring)
pathwav = os.path.dirname(filestring)
filename = os.path.splitext(nomewav)[0]
maindir = pathwav+"/"+filename+"/Spec/"
if not os.path.exists(maindir):
return
for fname in sorted(os.listdir(maindir)):
if fname.find('png') > 0 and fname[0] != 'c':
figname = maindir+os.sep+fname
img = cv2.imread(figname, 0)
crop_img = img[140:600,50:330]
tdim = crop_img.shape
c = 5.0
tx = int(tdim[0]/c)
ty = int(tdim[1]/c)
imgr = cv2.resize(crop_img,(ty,tx),interpolation=cv2.INTER_AREA)
if not os.path.exists(maindir+"/Crop/"):
os.makedirs(maindir+"/Crop/")
figname = maindir+"/Crop/"+os.sep+"c"+fname
cv2.imwrite(figname,imgr)
print "Crop realizado."
def img2array(image):
vector = []
for line in image:
for column in line:
vector.append("%4.3f"%(float(column[0])/255))
return np.array(vector)
#interesting_points_enconter(sys.argv[1], 15000, 140000, 200.0)
#time_stamps_cropper(sys.argv[1])
#raw_specs(sys.argv[1])
#crop_specs(sys.argv[1])