Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,40 +9,61 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
cap = cv2.VideoCapture(0)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
return cap
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Handle capture failure gracefully
|
| 31 |
-
exit()
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 44 |
break
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
cv2.destroyAllWindows()
|
|
|
|
| 9 |
# Set the title of the app
|
| 10 |
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
+
# Load the video capture
|
| 13 |
+
videoCap = cv2.VideoCapture(0)
|
| 14 |
|
| 15 |
+
# Function to get class colors
|
| 16 |
+
def getColours(cls_num):
|
| 17 |
+
base_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
|
| 18 |
+
color_index = cls_num % len(base_colors)
|
| 19 |
+
increments = [(1, -2, 1), (-2, 1, -1), (1, -1, 2)]
|
| 20 |
+
color = [base_colors[color_index][i] + increments[color_index][i] *
|
| 21 |
+
(cls_num // len(base_colors)) % 256 for i in range(3)]
|
| 22 |
+
return tuple(color)
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
+
while True:
|
| 26 |
+
ret, frame = videoCap.read()
|
| 27 |
+
if not ret:
|
| 28 |
+
continue
|
| 29 |
+
results = yolo.track(frame, stream=True)
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
+
for result in results:
|
| 33 |
+
# get the classes names
|
| 34 |
+
classes_names = result.names
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# iterate over each box
|
| 37 |
+
for box in result.boxes:
|
| 38 |
+
# check if confidence is greater than 40 percent
|
| 39 |
+
if box.conf[0] > 0.4:
|
| 40 |
+
# get coordinates
|
| 41 |
+
[x1, y1, x2, y2] = box.xyxy[0]
|
| 42 |
+
# convert to int
|
| 43 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 44 |
|
| 45 |
+
# get the class
|
| 46 |
+
cls = int(box.cls[0])
|
| 47 |
+
|
| 48 |
+
# get the class name
|
| 49 |
+
class_name = classes_names[cls]
|
| 50 |
+
|
| 51 |
+
# get the respective colour
|
| 52 |
+
colour = getColours(cls)
|
| 53 |
+
|
| 54 |
+
# draw the rectangle
|
| 55 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), colour, 2)
|
| 56 |
|
| 57 |
+
# put the class name and confidence on the image
|
| 58 |
+
cv2.putText(frame, f'{classes_names[int(box.cls[0])]} {box.conf[0]:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, colour, 2)
|
| 59 |
+
|
| 60 |
+
# show the image
|
| 61 |
+
cv2.imshow('frame', frame)
|
| 62 |
|
| 63 |
+
# break the loop if 'q' is pressed
|
| 64 |
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 65 |
break
|
| 66 |
|
| 67 |
+
# release the video capture and destroy all windows
|
| 68 |
+
videoCap.release()
|
| 69 |
+
cv2.destroyAllWindows()
|