Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,56 +9,32 @@ model = YOLO("best.pt") # Ensure the path to your model is correct
|
|
| 9 |
# Set the title of the app
|
| 10 |
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
# Open a connection to the webcam (0 is the default camera)
|
| 18 |
-
cap = cv2.VideoCapture(0)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
if not ret:
|
| 23 |
-
st.error("Failed to capture video.")
|
| 24 |
-
break
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
if class_id == 0: # Replace 0 with the actual class ID for fire if different
|
| 43 |
-
fire_detected = True
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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()
|
|
|
|
| 9 |
# Set the title of the app
|
| 10 |
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
+
# Define color ranges for fire detection
|
| 13 |
+
lower_red = np.array([0, 100, 100])
|
| 14 |
+
upper_red = np.array([10, 255, 255])
|
| 15 |
|
| 16 |
+
# Create a video capture object
|
| 17 |
+
cap = cv2.VideoCapture(0) # 0 for default camera, change as needed
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
while True:
|
| 20 |
+
ret, frame = cap.read()
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Convert BGR to HSV color space
|
| 23 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 24 |
|
| 25 |
+
# Create a mask for the fire color range
|
| 26 |
+
mask = cv2.inRange(hsv, lower_red, upper_red)
|
| 27 |
|
| 28 |
+
# Apply the mask to the original frame
|
| 29 |
+
res = cv2.bitwise_and(frame, frame, mask=mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Display the resulting frame
|
| 32 |
+
cv2.imshow('Fire Detection', res)
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Exit if 'q' is pressed
|
| 35 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 36 |
+
break
|
| 37 |
|
| 38 |
+
# Release the capture and close windows
|
| 39 |
+
cap.release()
|
| 40 |
+
cv2.destroyAllWindows() 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|