Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
<<<<<<< HEAD
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
from keras import layers, models
|
|
@@ -112,126 +111,4 @@ def stretch(data, rate=0.8):
|
|
| 112 |
return librosa.effects.time_stretch(data, rate=rate)
|
| 113 |
|
| 114 |
def pitch(data, sample_rate, pitch_factor=0.7):
|
| 115 |
-
return librosa.effects.pitch_shift(data, sr=sample_rate, n_steps=pitch_factor)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
=======
|
| 120 |
-
import librosa
|
| 121 |
-
import numpy as np
|
| 122 |
-
from keras import layers, models
|
| 123 |
-
|
| 124 |
-
def create_cnn_model(input_shape):
|
| 125 |
-
model = models.Sequential()
|
| 126 |
-
|
| 127 |
-
# First Convolutional Layer
|
| 128 |
-
model.add(layers.Conv1D(32, 3, activation='relu', input_shape=input_shape))
|
| 129 |
-
model.add(layers.MaxPooling1D(pool_size=2))
|
| 130 |
-
|
| 131 |
-
# Second Convolutional Layer
|
| 132 |
-
model.add(layers.Conv1D(64, 3, activation='relu'))
|
| 133 |
-
model.add(layers.MaxPooling1D(pool_size=2))
|
| 134 |
-
|
| 135 |
-
# Flatten layer
|
| 136 |
-
model.add(layers.Flatten())
|
| 137 |
-
|
| 138 |
-
# Dense layers
|
| 139 |
-
model.add(layers.Dense(128, activation='relu', input_shape=input_shape))
|
| 140 |
-
model.add(layers.Dense(256, activation='relu', input_shape=input_shape))
|
| 141 |
-
model.add(layers.Dense(512, activation='relu', input_shape=input_shape))
|
| 142 |
-
model.add(layers.Dense(512, activation='relu', input_shape=input_shape))
|
| 143 |
-
model.add(layers.Dense(256, activation='relu', input_shape=input_shape))
|
| 144 |
-
model.add(layers.Dense(128, activation='relu', input_shape=input_shape))
|
| 145 |
-
|
| 146 |
-
# Output layer
|
| 147 |
-
model.add(layers.Dense(1, activation='sigmoid'))
|
| 148 |
-
|
| 149 |
-
return model
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def get_features(path, duration=6):
|
| 153 |
-
try:
|
| 154 |
-
# Load audio file with specific duration and offset to handle silent parts
|
| 155 |
-
data, sample_rate = librosa.load(path, duration=2.5, offset=0.6)
|
| 156 |
-
except Exception as e:
|
| 157 |
-
print(f"Error loading {path}: {e}")
|
| 158 |
-
return None # Skip the file if there's an error
|
| 159 |
-
|
| 160 |
-
# Without augmentation
|
| 161 |
-
res1 = extract_features(data, sample_rate)
|
| 162 |
-
result = np.array(res1)
|
| 163 |
-
|
| 164 |
-
# With noise
|
| 165 |
-
noise_data = noise(data)
|
| 166 |
-
res2 = extract_features(noise_data, sample_rate)
|
| 167 |
-
result = np.vstack((result, res2))
|
| 168 |
-
|
| 169 |
-
# Stretching and pitching
|
| 170 |
-
new_data = stretch(data)
|
| 171 |
-
data_stretch_pitch = pitch(new_data, sample_rate)
|
| 172 |
-
res3 = extract_features(data_stretch_pitch, sample_rate)
|
| 173 |
-
result = np.vstack((result, res3))
|
| 174 |
-
|
| 175 |
-
return result
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
def extract_features(data, sample_rate, target_shape=40):
|
| 179 |
-
result = np.array([])
|
| 180 |
-
|
| 181 |
-
# ZCR
|
| 182 |
-
zcr = librosa.feature.zero_crossing_rate(y=data)
|
| 183 |
-
zcr = np.mean(zcr.T, axis=0)
|
| 184 |
-
zcr = pad_or_trim(zcr, target_shape)
|
| 185 |
-
result = np.hstack((result, zcr))
|
| 186 |
-
|
| 187 |
-
# Chroma_stft
|
| 188 |
-
stft = np.abs(librosa.stft(data))
|
| 189 |
-
chroma_stft = librosa.feature.chroma_stft(S=stft, sr=sample_rate)
|
| 190 |
-
chroma_stft = np.mean(chroma_stft.T, axis=0)
|
| 191 |
-
chroma_stft = pad_or_trim(chroma_stft, target_shape)
|
| 192 |
-
result = np.hstack((result, chroma_stft))
|
| 193 |
-
|
| 194 |
-
# MFCC
|
| 195 |
-
mfcc = librosa.feature.mfcc(y=data, sr=sample_rate, n_mfcc=13)
|
| 196 |
-
mfcc = np.mean(mfcc.T, axis=0)
|
| 197 |
-
mfcc = pad_or_trim(mfcc, target_shape)
|
| 198 |
-
result = np.hstack((result, mfcc))
|
| 199 |
-
|
| 200 |
-
# Root Mean Square Value
|
| 201 |
-
rms = librosa.feature.rms(y=data)
|
| 202 |
-
rms = np.mean(rms.T, axis=0)
|
| 203 |
-
rms = pad_or_trim(rms, target_shape)
|
| 204 |
-
result = np.hstack((result, rms))
|
| 205 |
-
|
| 206 |
-
# MelSpectrogram
|
| 207 |
-
mel = librosa.feature.melspectrogram(y=data, sr=sample_rate)
|
| 208 |
-
mel = np.mean(mel.T, axis=0)
|
| 209 |
-
mel = pad_or_trim(mel, target_shape)
|
| 210 |
-
result = np.hstack((result, mel))
|
| 211 |
-
|
| 212 |
-
return result
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
def pad_or_trim(feature, target_shape):
|
| 216 |
-
"""Pad or trim feature array to ensure a consistent shape."""
|
| 217 |
-
if len(feature) > target_shape:
|
| 218 |
-
feature = feature[:target_shape]
|
| 219 |
-
elif len(feature) < target_shape:
|
| 220 |
-
feature = np.pad(feature, (0, target_shape - len(feature)), mode='constant')
|
| 221 |
-
return feature
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
def noise(data, noise_factor=0.005):
|
| 225 |
-
noise_amp = noise_factor * np.random.uniform() * np.amax(data)
|
| 226 |
-
data = data + noise_amp * np.random.normal(size=data.shape[0])
|
| 227 |
-
return data
|
| 228 |
-
|
| 229 |
-
def stretch(data, rate=0.8):
|
| 230 |
-
return librosa.effects.time_stretch(data, rate=rate)
|
| 231 |
-
|
| 232 |
-
def pitch(data, sample_rate, pitch_factor=0.7):
|
| 233 |
-
return librosa.effects.pitch_shift(data, sr=sample_rate, n_steps=pitch_factor)
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
>>>>>>> f3090616676ed6b7fcf9d16589c788e1843b194c
|
|
|
|
|
|
|
| 1 |
import librosa
|
| 2 |
import numpy as np
|
| 3 |
from keras import layers, models
|
|
|
|
| 111 |
return librosa.effects.time_stretch(data, rate=rate)
|
| 112 |
|
| 113 |
def pitch(data, sample_rate, pitch_factor=0.7):
|
| 114 |
+
return librosa.effects.pitch_shift(data, sr=sample_rate, n_steps=pitch_factor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|