|
import streamlit as st |
|
import os |
|
import numpy as np |
|
import librosa |
|
import joblib |
|
from pydub import AudioSegment |
|
|
|
|
|
def predict_anomalies(sample_audio_file, model_filename='isolation_forest_model_advanced.pkl'): |
|
|
|
clf = joblib.load(model_filename) |
|
|
|
|
|
audio, sample_rate = librosa.load(sample_audio_file, sr=None) |
|
mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=13) |
|
spectral_contrast = librosa.feature.spectral_contrast(y=audio, sr=sample_rate) |
|
feature = np.hstack((mfccs.mean(axis=1), spectral_contrast.mean(axis=1))) |
|
|
|
|
|
feature = feature.reshape(1, -1) |
|
|
|
|
|
prediction = clf.predict(feature) |
|
|
|
|
|
return prediction[0] |
|
|
|
|
|
sample_files = { |
|
"Sample 1": "good28.wav", |
|
"Sample 2": "fault35.wav", |
|
"Sample 3": "fault31.wav", |
|
"Sample 4": "good30.wav", |
|
"Sample 5": "fault30.wav", |
|
"Sample 6": "fault32.wav", |
|
"Sample 7": "good29.wav", |
|
} |
|
|
|
|
|
st.title("Anomaly Detection on Sound Data") |
|
|
|
|
|
st.write("This app uses an Isolation Forest Model to Detect Anomalies in .wav audio files. Isolation Forest is an unsupervised machine learning algorithm that is particularly effective for anomaly detection in high-dimensional data. It works by isolating anomalies from the majority of normal data points.") |
|
|
|
|
|
selected_sample = st.sidebar.selectbox("Select a Sample .wav File from the dropdown", list(sample_files.keys())) |
|
|
|
|
|
sample_audio_file = sample_files[selected_sample] |
|
|
|
|
|
predict_button = st.button("Click to Predict Anomalies ") |
|
|
|
if predict_button: |
|
|
|
result = predict_anomalies(sample_audio_file) |
|
|
|
if result == 1: |
|
st.error("Anomaly Detected in Audio file") |
|
else: |
|
st.success("Normal Audio file") |
|
|
|
|
|
audio = AudioSegment.from_wav(sample_audio_file) |
|
audio.export("temp.wav", format="wav") |
|
st.audio("temp.wav", format="audio/wav") |
|
|
|
|
|
st.sidebar.markdown( |
|
""" |
|
**How to Use:** Select a sample .wav file, click the "Predict" button to check for anomalies, and then you can listen to the audio by clicking the play Button. |
|
""" |
|
) |
|
|