fire / main.py
ArchiMathur's picture
Update main.py
604d721 verified
raw
history blame
2.42 kB
import streamlit as st
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image
# Load the YOLO model (replace with your model path)
model = YOLO("D:\\streamlit\\my_streamlit_app\\fire\\best.pt")
# Use your YOLO model file here
st.title("Fire Detection in Forest")
# Sidebar for input options
input_option = st.sidebar.selectbox("Select Input Method", ["Upload Image", "Use Webcam"])
if input_option == "Upload Image":
# Upload Image
uploaded_file = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, caption='User Image')
st.write("Classifying...")
# Convert image to numpy array
img_np = np.array(img)
# Make predictions
results = model.predict(source=img_np, conf=0.5)
# Draw bounding boxes on the image
for result in results:
boxes = result.boxes.xyxy
for box in boxes:
x1, y1, x2, y2 = box[:4].astype(int)
img_np = cv2.rectangle(img_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Show the resulting image
st.image(img_np, caption='Detected Fire', use_column_width=True)
elif input_option == "Use Webcam":
st.write("Starting webcam for live detection...")
# Start video capture
camera = cv2.VideoCapture(0) # 0 is the default camera
# Create a placeholder for the video feed
video_placeholder = st.empty()
# Main loop for live detection
while True:
ret, frame = camera.read()
if not ret:
st.write("Failed to capture image")
break
# Make predictions
results = model.predict(source=frame, conf=0.5)
# Draw bounding boxes on the frame
for result in results:
boxes = result.boxes.xyxy
for box in boxes:
x1, y1, x2, y2 = box[:4].astype(int)
frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Convert frame to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the frame in the Streamlit app
video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
# Break loop on user command
if st.button("Stop Detection"):
break
# Release the camera
camera.release()