Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,36 @@ import librosa
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Diccionario de etiquetas
|
| 7 |
-
labels = ['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def classify_audio(audio_file):
|
| 10 |
# Carga el modelo
|
| 11 |
-
model = tf.keras.models.load_model('my_model.h5')
|
| 12 |
|
| 13 |
# Preprocesa el audio
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Realiza la predicci贸n
|
| 20 |
-
prediction = model.predict(
|
| 21 |
predicted_label_index = np.argmax(prediction)
|
| 22 |
|
| 23 |
# Devuelve la etiqueta predicha
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Diccionario de etiquetas
|
| 7 |
+
labels = ['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']
|
| 8 |
+
|
| 9 |
+
def extract_features(file_name):
|
| 10 |
+
try:
|
| 11 |
+
audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
|
| 12 |
+
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
|
| 13 |
+
mfccsscaled = np.mean(mfccs.T,axis=0)
|
| 14 |
+
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print("Error encountered while parsing file: ", file_name)
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
return mfccsscaled
|
| 20 |
|
| 21 |
def classify_audio(audio_file):
|
| 22 |
# Carga el modelo
|
| 23 |
+
model = tf.keras.models.load_model('my_model.h5')
|
| 24 |
|
| 25 |
# Preprocesa el audio
|
| 26 |
+
features = extract_features(audio_file)
|
| 27 |
+
if features is None:
|
| 28 |
+
return "Error al procesar el audio" # Manejo de error
|
| 29 |
+
|
| 30 |
+
features = features.reshape(1, -1) # Redimensiona a (1, 40)
|
| 31 |
+
|
| 32 |
+
# Si tu modelo necesita 3 dimensiones, redimensiona a (1, 40, 1)
|
| 33 |
+
# features = features.reshape(1, 40, 1)
|
| 34 |
+
|
| 35 |
# Realiza la predicci贸n
|
| 36 |
+
prediction = model.predict(features)
|
| 37 |
predicted_label_index = np.argmax(prediction)
|
| 38 |
|
| 39 |
# Devuelve la etiqueta predicha
|