Spaces:
Sleeping
Sleeping
File size: 1,799 Bytes
d8f2217 a076cab |
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 |
from transformers import ASTFeatureExtractor, ASTForAudioClassification
import torch
import librosa
import numpy as np
import pandas as pd
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593").to(device)
feature_extractor = ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593")
def detect_siren(audio_path):
audio, sr = librosa.load(audio_path, sr=16000)
if len(audio.shape) == 1:
audio = np.expand_dims(audio, axis=0)
inputs = feature_extractor(audio, sampling_rate=16000, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
return model.config.id2label[predicted_class_idx]
# Example: Analyze multiple files
audio_files = ["/kaggle/input/emergency-vehicle-siren-sounds/sounds/ambulance/sound_1.wav", "/kaggle/input/emergency-vehicle-siren-sounds/sounds/ambulance/sound_10.wav", "/kaggle/input/emergency-vehicle-siren-sounds/sounds/ambulance/sound_100.wav"]
results = []
for file in audio_files:
label = detect_siren(file)
results.append({"file": file, "label": label})
# Save results
df = pd.DataFrame(results)
df.to_csv("detection_results.csv", index=False)
# app.py
import streamlit as st
st.title("π¨ Emergency Vehicle Siren Detection")
df = pd.read_csv("detection_results.csv")
st.dataframe(df)
for index, row in df.iterrows():
if "siren" in row['label'].lower():
st.error(f"π¨ {row['file']} => Siren Detected!")
else:
st.success(f"β
{row['file']} => No Siren.")
|