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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -46
app.py CHANGED
@@ -156,64 +156,37 @@
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
 
 
156
 
157
  import streamlit as st
158
  from ultralytics import YOLO
 
159
  import numpy as np
160
  from PIL import Image
161
 
162
+ # Load the YOLO model (use the path to your 'best.pt' file)
163
+ model = YOLO("best.pt")
164
 
 
165
  st.title("Fire Detection in Forest")
166
 
167
+ # Upload Image
168
+ uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
169
 
170
+ if uploaded_file is not None:
171
+ # Open the uploaded image
172
+ img = Image.open(uploaded_file)
173
+ st.image(img, caption="Uploaded Image", use_column_width=True)
174
 
175
+ # Convert image to a numpy array
176
+ img_np = np.array(img)
 
 
 
177
 
178
+ # Make predictions
179
+ results = model.predict(source=img_np, imgsz=640, conf=0.5)
180
 
181
+ # Check if fire is detected
182
+ fire_detected = any("fire" in results.names[int(cls)] for cls in results[0].boxes.cls)
183
 
184
+ # Display results
185
+ if fire_detected:
186
+ st.success("🔥 Fire Detected!")
187
+ else:
188
+ st.warning("No Fire Detected.")
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
 
192