| import numpy as np | |
| import librosa | |
| import joblib | |
| # Load your trained model + label encoder | |
| clf = joblib.load("models/noise_classifier.pkl") | |
| label_encoder = joblib.load("models/label_encoder.pkl") | |
| def classify_noise(audio_path): | |
| y, sr = librosa.load(audio_path, sr=None) | |
| mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) | |
| feature = np.mean(mfcc.T, axis=0).reshape(1, -1) | |
| probs = clf.predict_proba(feature)[0] | |
| top_idx = np.argsort(probs)[::-1][:5] | |
| return [(label_encoder.inverse_transform([i])[0], probs[i]) for i in top_idx] | |