Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
-
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
import torch
|
5 |
import streamlit as st
|
6 |
-
import
|
7 |
from ultralytics import YOLO
|
8 |
from camera_input_live import camera_input_live
|
9 |
|
@@ -14,31 +13,33 @@ model = YOLO(model_path)
|
|
14 |
model.to(device)
|
15 |
|
16 |
# Streamlit app title
|
17 |
-
st.title("Live Fire Detection with
|
18 |
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
fire_detected = False # Track fire detection state
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
<audio id="fireAlarm" src="alarm.mp3"></audio>
|
27 |
<script>
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
function stopAlarm() {
|
35 |
-
var alarm = document.getElementById("fireAlarm");
|
36 |
alarm.pause();
|
37 |
alarm.currentTime = 0;
|
38 |
-
}
|
39 |
</script>
|
40 |
"""
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if image is not None:
|
43 |
# Convert the image to OpenCV format
|
44 |
bytes_data = image.getvalue()
|
@@ -47,7 +48,7 @@ if image is not None:
|
|
47 |
# Perform fire detection
|
48 |
results = model(cv2_img)
|
49 |
|
50 |
-
fire_present = False #
|
51 |
|
52 |
# Draw bounding boxes for detected fires
|
53 |
for result in results:
|
@@ -57,18 +58,15 @@ if image is not None:
|
|
57 |
label = f'Fire {box.conf[0]:.2f}'
|
58 |
cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
|
59 |
cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
60 |
-
fire_present = True
|
61 |
|
62 |
# Display the annotated image
|
63 |
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
|
64 |
|
65 |
-
# Display logs
|
66 |
if fire_present:
|
67 |
st.error("🔥 Fire Detected! 🔥")
|
68 |
-
|
69 |
-
fire_detected = True
|
70 |
else:
|
71 |
st.success("✅ No Fire Detected")
|
72 |
-
|
73 |
-
st.markdown(alarm_html + "<script>stopAlarm();</script>", unsafe_allow_html=True)
|
74 |
-
fire_detected = False
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
import streamlit as st
|
5 |
+
import streamlit.components.v1 as components
|
6 |
from ultralytics import YOLO
|
7 |
from camera_input_live import camera_input_live
|
8 |
|
|
|
13 |
model.to(device)
|
14 |
|
15 |
# Streamlit app title
|
16 |
+
st.title("🔥 Live Fire Detection with Alarm 🔥")
|
17 |
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
|
18 |
|
19 |
+
# Load alarm sound from a public URL
|
20 |
+
alarm_url = "https://huggingface.co/spaces/Prathamesh1420/Fire_detection_streamlit/blob/main/alarm.mp3" # Replace with your hosted alarm
|
|
|
21 |
|
22 |
+
# JavaScript to control alarm playback
|
23 |
+
js_code = f"""
|
|
|
24 |
<script>
|
25 |
+
var alarm = new Audio("{alarm_url}");
|
26 |
+
function playAlarm() {{
|
27 |
+
alarm.loop = true;
|
28 |
+
alarm.play();
|
29 |
+
}}
|
30 |
+
function stopAlarm() {{
|
|
|
|
|
31 |
alarm.pause();
|
32 |
alarm.currentTime = 0;
|
33 |
+
}}
|
34 |
</script>
|
35 |
"""
|
36 |
|
37 |
+
# Display the JavaScript once
|
38 |
+
components.html(js_code, height=0)
|
39 |
+
|
40 |
+
# Capture live camera input
|
41 |
+
image = camera_input_live()
|
42 |
+
|
43 |
if image is not None:
|
44 |
# Convert the image to OpenCV format
|
45 |
bytes_data = image.getvalue()
|
|
|
48 |
# Perform fire detection
|
49 |
results = model(cv2_img)
|
50 |
|
51 |
+
fire_present = False # Flag for fire detection
|
52 |
|
53 |
# Draw bounding boxes for detected fires
|
54 |
for result in results:
|
|
|
58 |
label = f'Fire {box.conf[0]:.2f}'
|
59 |
cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
|
60 |
cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
61 |
+
fire_present = True
|
62 |
|
63 |
# Display the annotated image
|
64 |
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
|
65 |
|
66 |
+
# Display logs & trigger alarm using JavaScript
|
67 |
if fire_present:
|
68 |
st.error("🔥 Fire Detected! 🔥")
|
69 |
+
components.html("<script>playAlarm();</script>", height=0)
|
|
|
70 |
else:
|
71 |
st.success("✅ No Fire Detected")
|
72 |
+
components.html("<script>stopAlarm();</script>", height=0)
|
|
|
|