Prathamesh1420's picture
Update app.py
d4b30d0 verified
raw
history blame
1.73 kB
import cv2
import numpy as np
import torch
import streamlit as st
import streamlit.components.v1 as components
from ultralytics import YOLO
from camera_input_live import camera_input_live
# Load YOLO fire detection model
model_path = "last.pt"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = YOLO(model_path)
model.to(device)
# Streamlit app title
st.title("πŸ”₯ Live Fire Detection with Alarm πŸ”₯")
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
# Load alarm sound from a direct MP3 URL
alarm_url = "https://docs.google.com/uc?export=download&id=16IzsnQDmWkfYSeb_AjOTx79NEgkOpz88" # Replace with a working direct MP3 URL
# JavaScript to control alarm playback
js_code = f"""
<script>
var alarm = new Audio("{alarm_url}");
function playAlarm() {{
alarm.loop = true;
alarm.play();
}}
function stopAlarm() {{
alarm.pause();
alarm.currentTime = 0;
}}
</script>
"""
# Inject JavaScript once
components.html(js_code, height=0)
# Capture live camera input
image = camera_input_live()
if image is not None:
# Convert image to OpenCV format
bytes_data = image.getvalue()
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
# Perform fire detection
results = model(cv2_img)
# Check if fire is detected
fire_present = any(len(result.boxes) > 0 for result in results)
# Show logs in UI & trigger alarm
if fire_present:
st.error("πŸ”₯ Fire Detected! πŸ”₯")
components.html("<script>playAlarm();</script>", height=0)
else:
st.success("βœ… No Fire Detected")
components.html("<script>stopAlarm();</script>", height=0)