-
Notifications
You must be signed in to change notification settings - Fork 28
/
slice_spectrogram.py
58 lines (56 loc) · 2.32 KB
/
slice_spectrogram.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
import os
import re
from PIL import Image
"""
Slice the spectrogram into multiple 128x128 images which will be the input to the
Convolutional Neural Network.
"""
def slice_spect(verbose=0, mode=None):
if mode=="Train":
if os.path.exists('Train_Sliced_Images'):
return
labels = []
image_folder = "Train_Spectogram_Images"
filenames = [os.path.join(image_folder, f) for f in os.listdir(image_folder)
if f.endswith(".jpg")]
counter = 0
if(verbose > 0):
print "Slicing Spectograms ..."
if not os.path.exists('Train_Sliced_Images'):
os.makedirs('Train_Sliced_Images')
for f in filenames:
genre_variable = re.search('Train_Spectogram_Images/.*_(.+?).jpg', f).group(1)
img = Image.open(f)
subsample_size = 128
width, height = img.size
number_of_samples = width / subsample_size
for i in range(number_of_samples):
start = i*subsample_size
img_temporary = img.crop((start, 0., start + subsample_size, subsample_size))
img_temporary.save("Train_Sliced_Images/"+str(counter)+"_"+genre_variable+".jpg")
counter = counter + 1
return
elif mode=="Test":
if os.path.exists('Test_Sliced_Images'):
return
labels = []
image_folder = "Test_Spectogram_Images"
filenames = [os.path.join(image_folder, f) for f in os.listdir(image_folder)
if f.endswith(".jpg")]
counter = 0
if(verbose > 0):
print "Slicing Spectograms ..."
if not os.path.exists('Test_Sliced_Images'):
os.makedirs('Test_Sliced_Images')
for f in filenames:
song_variable = re.search('Test_Spectogram_Images/(.+?).jpg', f).group(1)
img = Image.open(f)
subsample_size = 128
width, height = img.size
number_of_samples = width / subsample_size
for i in range(number_of_samples):
start = i*subsample_size
img_temporary = img.crop((start, 0., start + subsample_size, subsample_size))
img_temporary.save("Test_Sliced_Images/"+str(counter)+"_"+song_variable+".jpg")
counter = counter + 1
return