Spaces:
Sleeping
Sleeping
| import sys | |
| import json | |
| import os | |
| try: | |
| import librosa | |
| import librosa.display | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| HAS_LIBS = True | |
| except ImportError: | |
| HAS_LIBS = False | |
| def extract_features(audio_path, output_dir=".tmp/"): | |
| """ | |
| Extrai MFCC e Espectrograma de Mel do áudio. | |
| """ | |
| if not HAS_LIBS: | |
| return {"error": "Bibliotecas librosa/numpy não instaladas."} | |
| # Carrega áudio | |
| y, sr = librosa.load(audio_path) | |
| # Mel Spectrogram | |
| S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128) | |
| S_dB = librosa.power_to_db(S, ref=np.max) | |
| # MFCC | |
| mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40) | |
| # Salva imagem do espectrograma para o dashboard | |
| base_name = os.path.splitext(os.path.basename(audio_path))[0] | |
| spec_filename = base_name + "_spec.png" | |
| spec_path = os.path.join(output_dir, spec_filename) | |
| plt.figure(figsize=(10, 4), facecolor='none') | |
| ax = plt.gca() | |
| ax.set_axis_off() | |
| librosa.display.specshow(S_dB, sr=sr, cmap='magma') | |
| plt.tight_layout(pad=0) | |
| plt.savefig(spec_path, transparent=True, bbox_inches='tight', pad_inches=0) | |
| plt.close() | |
| return { | |
| "audio_info": { | |
| "duration": librosa.get_duration(y=y, sr=sr), | |
| "sample_rate": sr | |
| }, | |
| "spectrogram_path": spec_path, | |
| "mfcc_shape": mfccs.shape | |
| } | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Uso: python feature_extractor.py <audio_path>") | |
| else: | |
| print(json.dumps(extract_features(sys.argv[1]), indent=2)) | |