Update app.py
Browse files
app.py
CHANGED
|
@@ -16,8 +16,8 @@ model.to(device)
|
|
| 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
|
| 20 |
-
alarm_url = "https://
|
| 21 |
|
| 22 |
# JavaScript to control alarm playback
|
| 23 |
js_code = f"""
|
|
@@ -34,36 +34,24 @@ js_code = f"""
|
|
| 34 |
</script>
|
| 35 |
"""
|
| 36 |
|
| 37 |
-
#
|
| 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
|
| 45 |
bytes_data = image.getvalue()
|
| 46 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
| 47 |
|
| 48 |
# Perform fire detection
|
| 49 |
results = model(cv2_img)
|
| 50 |
-
|
| 51 |
-
fire_present = False # Flag for fire detection
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
for result in results
|
| 55 |
-
boxes = result.boxes
|
| 56 |
-
for box in boxes:
|
| 57 |
-
b = box.xyxy[0].cpu().numpy().astype(int)
|
| 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 |
-
#
|
| 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)
|
|
|
|
| 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 direct MP3 URL
|
| 20 |
+
alarm_url = "https://www.soundjay.com/button/beep-07.wav" # Replace with a working direct MP3 URL
|
| 21 |
|
| 22 |
# JavaScript to control alarm playback
|
| 23 |
js_code = f"""
|
|
|
|
| 34 |
</script>
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
# Inject 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 image to OpenCV format
|
| 45 |
bytes_data = image.getvalue()
|
| 46 |
cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
|
| 47 |
|
| 48 |
# Perform fire detection
|
| 49 |
results = model(cv2_img)
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Check if fire is detected
|
| 52 |
+
fire_present = any(len(result.boxes) > 0 for result in results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Show logs in UI & trigger alarm
|
|
|
|
|
|
|
|
|
|
| 55 |
if fire_present:
|
| 56 |
st.error("🔥 Fire Detected! 🔥")
|
| 57 |
components.html("<script>playAlarm();</script>", height=0)
|