Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -152,32 +152,68 @@
|
|
| 152 |
# video_capture.release()
|
| 153 |
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
from PIL import Image
|
| 158 |
|
| 159 |
-
# Load the
|
| 160 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
results = model(image)
|
| 166 |
-
# Check if fire is detected based on the model's output classes
|
| 167 |
-
detected = any('fire' in results.names[label] for label in results.xyxy[0][:, -1].tolist())
|
| 168 |
-
return "Fire Detected" if detected else "No Fire Detected"
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
title="Fire Detection with YOLOv11",
|
| 176 |
-
description="Upload an image to classify whether fire is detected or not."
|
| 177 |
-
)
|
| 178 |
|
| 179 |
-
|
|
|
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
|
|
|
|
| 152 |
# video_capture.release()
|
| 153 |
|
| 154 |
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
import streamlit as st
|
| 158 |
+
from ultralytics import YOLO
|
| 159 |
+
import cv2
|
| 160 |
+
import numpy as np
|
| 161 |
from PIL import Image
|
| 162 |
|
| 163 |
+
# Load the YOLO model (replace "best.pt" with your model path if different)
|
| 164 |
+
model = YOLO("best.pt")
|
| 165 |
+
|
| 166 |
+
# Streamlit app title
|
| 167 |
+
st.title("Fire Detection in Forest")
|
| 168 |
+
|
| 169 |
+
# Sidebar for input options
|
| 170 |
+
input_option = st.sidebar.selectbox("Select Input Method", ["Upload Image", "Use Webcam", "Upload Video"])
|
| 171 |
|
| 172 |
+
if input_option == "Upload Image":
|
| 173 |
+
# Upload Image
|
| 174 |
+
uploaded_file = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
if uploaded_file is not None:
|
| 177 |
+
# Open the uploaded image
|
| 178 |
+
img = Image.open(uploaded_file)
|
| 179 |
+
st.image(img, caption='Uploaded Image', use_column_width=True)
|
| 180 |
+
st.write("Classifying...")
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
# Convert image to a numpy array
|
| 183 |
+
img_np = np.array(img)
|
| 184 |
|
| 185 |
+
# Make predictions using the model
|
| 186 |
+
results = model.predict(source=img_np, conf=0.5)
|
| 187 |
+
|
| 188 |
+
# Variable to check if fire is detected
|
| 189 |
+
fire_detected = False
|
| 190 |
+
|
| 191 |
+
# Draw bounding boxes on the image and check for fire detection
|
| 192 |
+
for result in results:
|
| 193 |
+
boxes = result.boxes.xyxy.cpu().numpy()
|
| 194 |
+
class_ids = result.boxes.cls.cpu().numpy().astype(int)
|
| 195 |
+
|
| 196 |
+
for box, class_id in zip(boxes, class_ids):
|
| 197 |
+
x1, y1, x2, y2 = map(int, box[:4])
|
| 198 |
+
label = result.names[class_id]
|
| 199 |
+
|
| 200 |
+
# Draw bounding box on the image
|
| 201 |
+
color = (0, 255, 0) if label == "fire" else (255, 0, 0) # Green for fire, red otherwise
|
| 202 |
+
img_np = cv2.rectangle(img_np, (x1, y1), (x2, y2), color, 2)
|
| 203 |
+
img_np = cv2.putText(img_np, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
| 204 |
+
|
| 205 |
+
# Check if the detected label is "fire"
|
| 206 |
+
if label.lower() == "fire":
|
| 207 |
+
fire_detected = True
|
| 208 |
+
|
| 209 |
+
# Show the resulting image with bounding boxes
|
| 210 |
+
st.image(img_np, caption='Processed Image', use_column_width=True)
|
| 211 |
+
|
| 212 |
+
# Display message based on fire detection
|
| 213 |
+
if fire_detected:
|
| 214 |
+
st.success("🔥 Fire Detected!")
|
| 215 |
+
else:
|
| 216 |
+
st.warning("No Fire Detected.")
|
| 217 |
|
| 218 |
|
| 219 |
|