|
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 |
|
|
|
|
|
model_path = "last.pt" |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model = YOLO(model_path) |
|
model.to(device) |
|
|
|
|
|
st.title("π₯ Live Fire Detection with Alarm π₯") |
|
st.subheader("Hold the camera towards potential fire sources to detect in real-time.") |
|
|
|
|
|
alarm_url = "https://soundcloud.com/trending-music-in/sets/soul?utm_source=clipboard&utm_medium=text&utm_campaign=social_sharing" |
|
|
|
|
|
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> |
|
""" |
|
|
|
|
|
components.html(js_code, height=0) |
|
|
|
|
|
image = camera_input_live() |
|
|
|
if image is not None: |
|
|
|
bytes_data = image.getvalue() |
|
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR) |
|
|
|
|
|
results = model(cv2_img) |
|
|
|
fire_present = False |
|
|
|
|
|
for result in results: |
|
boxes = result.boxes |
|
for box in boxes: |
|
b = box.xyxy[0].cpu().numpy().astype(int) |
|
label = f'Fire {box.conf[0]:.2f}' |
|
cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3) |
|
cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2) |
|
fire_present = True |
|
|
|
|
|
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True) |
|
|
|
|
|
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) |
|
|