-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_our_dataset.py
172 lines (144 loc) · 6.33 KB
/
process_our_dataset.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 8 23:53:53 2018
@author: selem
"""
import array
import os
import numpy as np
from preprocess import plot_audio_signal,open_audio_file,split_audio_file,get_labels,save_our_data_as_numpy_array,big_numpy_files_path
from pydub import AudioSegment
our_data_set_path = "D:/datasets/ourdataset"
my_data_set_path = "D:/datasets/mydataset"
#DELETE IMAGES FROM DATASET TO LEAVE ONLY AUDIOFILES
def delete_images(path):
labels = os.listdir(path)
for label in labels:
for file in os.listdir(path + '/' + label):
if file[-4:] == 'jpeg':
os.remove(path + '/' + label + '/' +file)
if file[-3:] == 'jpg':
os.remove(path + '/' + label + '/' +file)
if file[-3:] == 'JPG':
os.remove(path + '/' + label + '/' +file)
if file[-3:] == 'png':
os.remove(path + '/' + label + '/' +file)
#Print all names of audiofiles of each user
def print_user_files(path):
labels = os.listdir(path)
for label in labels:
print('======')
print(label)
print('======')
for file in os.listdir(path + '/' + label):
print(file)
def get_size_in_mega_bytes(path):
size_in_bytes = os.stat(path).st_size
#divide by 1024^2 to get in MB
return size_in_bytes/(1024*1024)
def add_noise(path,label,file_name,index):
file_path = path + '/' + label + '/' + file_name
dest = path + '/' + label + '/' + label + '_noise' + str(index) +'.mp3'
sound_file = open_audio_file(file_path)
sound_numpy = np.array(sound_file.get_array_of_samples())
#Generate White noise
upperbound = int(np.std(sound_numpy)/20)
wn = np.random.randint(low=0, high=upperbound, size=len(sound_numpy))
sound_numpy_wn = sound_numpy + wn
# print(sound_file.array_type)
new_sound_array = array.array(sound_file.array_type, sound_numpy_wn)
new_sound = sound_file._spawn(new_sound_array)
newfile = new_sound.export(format='mp3',out_f=dest)
def skip_n_seconds(path,label,file_name,index,n):
n_seconds = n * 1000
file_path = path + '/' + label + '/' + file_name
sound = open_audio_file(file_path)
dest = path + '/' + label + '/' + 'sliced_' + str(index) +'.mp3'
new_sound = sound[n_seconds:]
newfile = new_sound.export(format='mp3',out_f=dest)
def speed_up_audio(path,label,file_name,index,speed):
file_path = path + '/' + label + '/' + file_name
sound = open_audio_file(file_path)
dest = path + '/' + label + '/' + 'speedx' +str(speed)+ '_' + str(index) +'.mp3'
new_sound = sound.speedup(playback_speed=speed, chunk_size=150, crossfade=25)
newfile = new_sound.export(format='mp3',out_f=dest)
def fade_audio(path,label,file_name,index,fade):
file_path = path + '/' + label + '/' + file_name
sound = open_audio_file(file_path)
dest = path + '/' + label + '/' + 'faded_' + str(index) +'.mp3'
new_sound = sound.fade(to_gain=fade,start=650,end=5000)
newfile = new_sound.export(format='mp3',out_f=dest)
def convert_to_mp3(path,label,file_name,index):
file_path = path + '/' + label + '/' + file_name
sound = open_audio_file(file_path)
dest = path + '/' + label + '/' + 'converted_' + str(index) +'.mp3'
newfile = sound.export(format='mp3',out_f=dest)
os.remove(file_path)
def compress_audio_files(path):
labels = get_labels(path)
for label in labels:
print("Compressing... ",label)
audiofiles = []
for audiofile in os.listdir(path + '/' + label):
#current_path = path + '/' + label + '/' +audiofile
audiofiles.extend([audiofile])
i=1
for audiofile in audiofiles:
# file_size = get_size_in_mega_bytes(path+"/"+label+"/"+audiofile)
# if file_size > 0.45:
convert_to_mp3(path=path,label=label,file_name=audiofile,index=i)
i+=1
def create_extra_data(path):
# compress_audio_files(path)
labels = get_labels(path)
for label in labels:
print("Creating exta data for:",label)
audiofiles = []
for audiofile in os.listdir(path + '/' + label):
#current_path = path + '/' + label + '/' +audiofile
audiofiles.extend([audiofile])
i=1
for audiofile in audiofiles:
add_noise(path=path,label=label,file_name=audiofile,index=i)
skip_n_seconds(path=path,label=label,file_name=audiofile,index=i,n=2)
speed_up_audio(path=path,label=label,file_name=audiofile,index=i,speed=1.2)
i+=1
#save_our_data_as_numpy_array(max_len = 80, max_len2 = 40,origin_path=my_data_set_path,destination_path=big_numpy_files_path)
#labels = get_labels(big_numpy_files_path)
#for label in labels:
# x = np.load(big_numpy_files_path + '/' + label)
# if x.shape[0]<150:
# print(label, "Shape:",x.shape[0])
#path = our_data_set_path
#labels = get_labels(path)
#for label in labels:
# print("Creating exta data for:",label)
# audiofiles = []
# for audiofile in os.listdir(path + '/' + label):
# #current_path = path + '/' + label + '/' +audiofile
# audiofiles.extend([audiofile])
# index = np.random.randint(low=2,high=3)
# audiofiles = audiofiles[::index]
# i=1
# for audiofile in audiofiles:
# f = np.random.randint(low=0,high=2)
# fade_audio(path=path,label=label,file_name=audiofile,index=i,fade=f)
# i+=1
#path = our_data_set_path
#labels = get_labels(path)
#for label in labels:
## print("Creating exta data for:",label)
# audiofiles = []
# for audiofile in os.listdir(path + '/' + label):
# #current_path = path + '/' + label + '/' +audiofile
# audiofiles.extend([audiofile])
# i=20
# for audiofile in audiofiles:
# if audiofile[-10:-5] == 'noise':
# skip_n_seconds(path=path,label=label,file_name=audiofile,index=i,n=2)
# elif audiofile[:6] == 'sliced':
# speed_up_audio(path=path,label=label,file_name=audiofile,index=i,speed=1.25)
# elif audiofile[:6] == 'speedx':
# add_noise(path=path,label=label,file_name=audiofile,index=i)
# i+=1
#print_user_files(our_data_set_path)