ArchiMathur commited on
Commit
421a39a
·
verified ·
1 Parent(s): 6bc77b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -20
app.py CHANGED
@@ -152,32 +152,68 @@
152
  # video_capture.release()
153
 
154
 
155
- import torch
156
- import gradio as gr
 
 
 
 
157
  from PIL import Image
158
 
159
- # Load the YOLOv11 model
160
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', source='local') # replace 'yolov5' with 'yolo11' if needed
 
 
 
 
 
 
161
 
162
- # Define the prediction function
163
- def detect_fire(image):
164
- # Run the YOLO model on the uploaded image
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
- # Create and launch the Gradio interface
171
- interface = gr.Interface(
172
- fn=detect_fire,
173
- inputs=gr.Image(type='pil', label="Upload Image"),
174
- outputs="text",
175
- title="Fire Detection with YOLOv11",
176
- description="Upload an image to classify whether fire is detected or not."
177
- )
178
 
179
- interface.launch()
 
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