File size: 1,926 Bytes
9d95b96 f5383f6 9d95b96 f5383f6 9d95b96 f55134b 78d5001 9d95b96 f55134b f5383f6 9d95b96 f5383f6 f55134b f5383f6 f55134b f5383f6 f55134b f5383f6 9d95b96 f5383f6 9d95b96 f55134b f5383f6 9d95b96 f55134b 9d95b96 f55134b 9d95b96 141b335 f55134b 9d95b96 f55134b 9d95b96 f5383f6 9d95b96 f5383f6 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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 (must be a direct MP3 URL)
alarm_url = "https://docs.google.com/uc?export=download&id=16IzsnQDmWkfYSeb_AjOTx79NEgkOpz88"
# JavaScript to auto-play alarm when fire is detected
js_code = f"""
<script>
var alarm = new Audio("{alarm_url}");
alarm.loop = true;
function playAlarm() {{
alarm.play().catch(error => {{
console.log("Autoplay failed. User interaction required.");
}});
}}
function stopAlarm() {{
alarm.pause();
alarm.currentTime = 0;
}}
</script>
"""
# Inject JavaScript
components.html(js_code, height=0)
# Capture live camera input
image = camera_input_live()
if image is not None:
# Convert the 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)
fire_present = False # Flag for fire detection
# Check if fire is detected
for result in results:
if len(result.boxes) > 0:
fire_present = True
break # No need to check further
# Display logs & 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)
|