Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
-
from
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
# Set the title of the app
|
| 9 |
-
st.title("Fire Detection App")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Convert the image to a numpy array
|
| 24 |
-
img_np = np.array(img)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
|
| 32 |
-
# Draw bounding boxes on the
|
|
|
|
| 33 |
for result in results:
|
| 34 |
boxes = result.boxes.xyxy # Bounding boxes
|
| 35 |
for box in boxes:
|
| 36 |
x1, y1, x2, y2 = box[:4].astype(int)
|
| 37 |
-
|
| 38 |
|
| 39 |
# Check if the detected class is "fire" (adjust based on your model's class mapping)
|
| 40 |
class_id = int(box[5]) # Assuming class ID is at the 6th position
|
| 41 |
if class_id == 0: # Replace 0 with the actual class ID for fire if different
|
| 42 |
fire_detected = True
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
|
| 47 |
# Display message based on fire detection
|
| 48 |
if fire_detected:
|
|
@@ -50,3 +51,14 @@ if input_option == "Upload Image":
|
|
| 50 |
else:
|
| 51 |
st.warning("No Fire Detected.")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
# Load the YOLO model
|
| 7 |
+
model = YOLO("best.pt") # Ensure the path to your model is correct
|
| 8 |
+
|
| 9 |
# Set the title of the app
|
| 10 |
+
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
+
# Create a placeholder for the video stream
|
| 13 |
+
video_placeholder = st.empty()
|
| 14 |
|
| 15 |
+
# Function to perform live fire detection
|
| 16 |
+
def detect_fire():
|
| 17 |
+
# Open a connection to the webcam (0 is the default camera)
|
| 18 |
+
cap = cv2.VideoCapture(0)
|
| 19 |
|
| 20 |
+
while True:
|
| 21 |
+
ret, frame = cap.read()
|
| 22 |
+
if not ret:
|
| 23 |
+
st.error("Failed to capture video.")
|
| 24 |
+
break
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Convert the frame to RGB (as YOLO expects RGB input)
|
| 27 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 28 |
|
| 29 |
+
# Make predictions on the current frame
|
| 30 |
+
results = model.predict(source=rgb_frame, conf=0.5)
|
| 31 |
|
| 32 |
+
# Draw bounding boxes on the frame
|
| 33 |
+
fire_detected = False
|
| 34 |
for result in results:
|
| 35 |
boxes = result.boxes.xyxy # Bounding boxes
|
| 36 |
for box in boxes:
|
| 37 |
x1, y1, x2, y2 = box[:4].astype(int)
|
| 38 |
+
rgb_frame = cv2.rectangle(rgb_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 39 |
|
| 40 |
# Check if the detected class is "fire" (adjust based on your model's class mapping)
|
| 41 |
class_id = int(box[5]) # Assuming class ID is at the 6th position
|
| 42 |
if class_id == 0: # Replace 0 with the actual class ID for fire if different
|
| 43 |
fire_detected = True
|
| 44 |
|
| 45 |
+
# Display the frame in the Streamlit app
|
| 46 |
+
video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
|
| 47 |
|
| 48 |
# Display message based on fire detection
|
| 49 |
if fire_detected:
|
|
|
|
| 51 |
else:
|
| 52 |
st.warning("No Fire Detected.")
|
| 53 |
|
| 54 |
+
# Break the loop if the user presses 'q'
|
| 55 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 56 |
+
break
|
| 57 |
+
|
| 58 |
+
# Release the webcam and close windows
|
| 59 |
+
cap.release()
|
| 60 |
+
cv2.destroyAllWindows()
|
| 61 |
+
|
| 62 |
+
# Start the detection process
|
| 63 |
+
if st.button("Start Live Detection"):
|
| 64 |
+
detect_fire()
|