|
'''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 |
|
from ultralytics import YOLO |
|
from camera_input_live import camera_input_live |
|
|
|
|
|
pygame.mixer.init() |
|
alarm_sound = "alarm.mp3" |
|
|
|
|
|
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 Camera") |
|
st.subheader("Hold the camera towards potential fire sources to detect in real-time.") |
|
|
|
|
|
image = camera_input_live() |
|
fire_detected = False |
|
|
|
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! 🔥") |
|
if not fire_detected: |
|
pygame.mixer.music.load(alarm_sound) |
|
pygame.mixer.music.play(-1) |
|
fire_detected = True |
|
else: |
|
st.success("✅ No Fire Detected") |
|
if fire_detected: |
|
pygame.mixer.music.stop() |
|
fire_detected = False |
|
|