File size: 5,918 Bytes
452a698 eae54a4 26663dd eae54a4 26663dd eae54a4 26663dd 45b9fd5 26663dd eae54a4 45b9fd5 eae54a4 45b9fd5 eae54a4 26663dd 78b0fae eae54a4 26663dd eae54a4 45b9fd5 26663dd 452a698 9d95b96 452a698 226383e 452a698 226383e 452a698 9d95b96 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
'''import cv2
import numpy as np
import torch
import streamlit as st
from ultralytics import YOLO
from camera_input_live import camera_input_live
# Load YOLO fire detection model
model_path = "last.pt"
if not torch.cuda.is_available():
device = "cpu"
else:
device = "cuda"
model = YOLO(model_path)
model.to(device)
# Streamlit app title
st.title("Live Fire Detection with Camera")
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
# 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)
# Draw bounding boxes for detected fires
for result in results:
boxes = result.boxes
for box in boxes:
b = box.xyxy[0].cpu().numpy().astype(int)
c = int(box.cls[0])
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)
# Display the annotated image
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
'''
'''import cv2
import numpy as np
import torch
import streamlit as st
import pygame
import os
from ultralytics import YOLO
from camera_input_live import camera_input_live
# Set environment variable to use dummy audio on Hugging Face Spaces
os.environ["SDL_AUDIODRIVER"] = "dummy"
# Initialize Pygame mixer for audio alarm
pygame.mixer.init()
alarm_sound = "alarm.mp3" # Ensure you have an alarm.mp3 file in your directory
# 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 Camera")
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
# Capture live camera input
image = camera_input_live()
fire_detected = False # Track fire detection state
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 # Temporary flag for fire detection in this frame
# Draw bounding boxes for detected fires
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 # Fire detected
# Display the annotated image
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
# Display logs
if fire_present:
st.error("🔥 Fire Detected! 🔥")
if not fire_detected: # Play alarm if not already playing
pygame.mixer.music.load(alarm_sound)
pygame.mixer.music.play(-1) # Loop indefinitely
fire_detected = True # Update fire status
else:
st.success("✅ No Fire Detected")
if fire_detected:
pygame.mixer.music.stop() # Stop alarm
fire_detected = False
'''
import cv2
import numpy as np
import torch
import streamlit as st
import os
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 Camera")
st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
# Capture live camera input
image = camera_input_live()
fire_detected = False # Track fire detection state
# HTML & JS for Alarm Sound
alarm_html = """
<audio id="fireAlarm" src="alarm.mp3"></audio>
<script>
function playAlarm() {
var alarm = document.getElementById("fireAlarm");
if (alarm.paused) {
alarm.play();
}
}
function stopAlarm() {
var alarm = document.getElementById("fireAlarm");
alarm.pause();
alarm.currentTime = 0;
}
</script>
"""
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 # Temporary flag for fire detection in this frame
# Draw bounding boxes for detected fires
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 # Fire detected
# Display the annotated image
st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)
# Display logs and trigger alarm
if fire_present:
st.error("🔥 Fire Detected! 🔥")
st.markdown(alarm_html + "<script>playAlarm();</script>", unsafe_allow_html=True)
fire_detected = True
else:
st.success("✅ No Fire Detected")
if fire_detected:
st.markdown(alarm_html + "<script>stopAlarm();</script>", unsafe_allow_html=True)
fire_detected = False
|