Uh oh!
There was an error while loading. Please reload this page.
forked from despoisj/DeepAudioClassification
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceSpectrogram.py
More file actions
Latest commit
42 lines (34 loc) · 1.28 KB
/
Copy pathsliceSpectrogram.py
File metadata and controls
42 lines (34 loc) · 1.28 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
# Import Pillow:
fromPILimportImage
importos.path
fromconfigimportspectrogramsPath, slicesPath
#Slices all spectrograms
defcreateSlicesFromSpectrograms(desiredSize):
forfilenameinos.listdir(spectrogramsPath):
iffilename.endswith(".png"):
sliceSpectrogram(filename,desiredSize)
#Creates slices from spectrogram
#TODO Improvement - Make sure we don't miss the end of the song
defsliceSpectrogram(filename, desiredSize):
genre=filename.split("_")[0] #Ex. Dubstep_19.png
# Load the full spectrogram
img=Image.open(spectrogramsPath+filename)
#Compute approximate number of 128x128 samples
width, height=img.size
nbSamples=int(width/desiredSize)
width-desiredSize
#Create path if not existing
slicePath=slicesPath+"{}/".format(genre);
ifnotos.path.exists(os.path.dirname(slicePath)):
try:
os.makedirs(os.path.dirname(slicePath))
exceptOSErrorasexc: # Guard against race condition
ifexc.errno!=errno.EEXIST:
raise
#For each sample
foriinrange(nbSamples):
print"Creating slice: ", (i+1), "/", nbSamples, "for", filename
#Extract and save 128x128 sample
startPixel=i*desiredSize
imgTmp=img.crop((startPixel, 1, startPixel+desiredSize, desiredSize+1))
imgTmp.save(slicesPath+"{}/{}_{}.png".format(genre,filename[:-4],i))