File size: 1,442 Bytes
9361c2a
ae71f31
 
4816f88
36f905c
 
d633b20
17b3b22
f57bcb2
ae71f31
df628e6
ae71f31
 
4816f88
 
d633b20
 
 
 
 
 
4816f88
 
f57bcb2
36f905c
f57bcb2
 
ae71f31
f57bcb2
ae71f31
f57bcb2
d633b20
ae71f31
d633b20
ae71f31
ef809ac
ae71f31
 
ef809ac
ae71f31
 
 
 
 
 
ef809ac
ae71f31
f57bcb2
ae71f31
 
ef809ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st
import librosa
import numpy as np
import pandas as pd
import IPython.display as ipd
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import load_model


model = load_model('model/audio_prediction_model')


df = pd.read_csv('UrbanSound8K.csv')

y = np.array(df['class'].tolist())

le = LabelEncoder()
y = to_categorical(le.fit_transform(y))


categories = df.groupby('classID')['class'].unique

def prediction_audio(audiofile, model):

    audio_data, sample_rate = librosa.load(audiofile, res_type='kaiser_fast')
    mel_frequency = np.mean(librosa.feature.melspectrogram(y = audio_data, sr = sample_rate).T, axis=0) # (128,)

    X = mel_frequency.reshape(1, -1) # (1, 128)

    predicted_label = np.argmax(model.predict(X), axis=-1)
    predicted_class = le.inverse_transform(predicted_label)

    return predicted_class

st.title("Gunshot Detection")

# Formulaire pour charger le fichier audio
audio_file = st.file_uploader("Select audio file", type=["wav", "mp3"])

if audio_file is not None:
    # Affichez le fichier audio
    st.audio(audio_file, format='audio/wav')

    # Bouton pour déclencher la prédiction
    if st.button("Predict"):
        # Effectuez la prédiction
        prediction = prediction_audio(audio_file, model)
        
        # Affichez le résultat de la prédiction
        st.write("Result : ", prediction)