supervision
supervision copied to clipboard
How to add class filter in yolov8 tracker and zone counting
Search before asking
- [X] I have searched the Supervision issues and found no similar feature requests.
Question
How to add class filter in yolov8 tracker and zone counting
here is my code
import cv2
from ultralytics import YOLO
import time
import numpy as np
import supervision as sv
# Define the detection zone polygon in normalized coordinates
ZONE_POLYGON = np.array([
[0, 0],
[0.5, 0],
[0.5, 1],
[0, 1]
])
# Load the YOLOv8 model
model = YOLO('app/yolov8n.pt')
# Open the video file or webcam
cap = cv2.VideoCapture("app/traffics=.mp4")
# Initialize the counter for detections within the zone
zone_detections_count = 0
# Get video frame resolution
ret, frame = cap.read()
if not ret:
print("Failed to get frame from video source")
cap.release()
exit()
frame_height, frame_width = frame.shape[:2]
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
# Adjust the zone polygon to the video resolution
zone_polygon = (ZONE_POLYGON * np.array([frame_width, frame_height])).astype(int)
zone = sv.PolygonZone(polygon=zone_polygon, frame_resolution_wh=(frame_width, frame_height))
zone_annotator = sv.PolygonZoneAnnotator(
zone=zone,
color=sv.Color.red(),
thickness=2,
text_thickness=4,
text_scale=2
)
# Initialize FPS calculation
frame_count = 0
start_time = time.time()
# Create a named window and set it to be resizable, then make it full screen
cv2.namedWindow("YOLOv8 Tracking with Zone", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("YOLOv8 Tracking with Zone", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
# Loop through the video frames
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True,)
detections = sv.Detections.from_yolov8(results[0])
detections = detections[detections.class_id == 0]
zone_triggered_detections = zone.trigger(detections=detections)
zone_detections_count += len(zone_triggered_detections)
annotated_frame = results[0].plot()
#cv2.putText(annotated_frame, f"Zone Detections: {zone_detections_count}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
frame_count += 1
elapsed_time = time.time() - start_time
if elapsed_time > 1:
fps = frame_count / elapsed_time
cv2.putText(annotated_frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
frame_count = 0
start_time = time.time()
annotated_frame = zone_annotator.annotate(scene=annotated_frame)
cv2.imshow("YOLOv8 Tracking with Zone", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
I'm getting the following error
C:\Users\RizwanMLE\anaconda3\envs\yolov8\lib\site-packages\supervision\detection\core.py:175: FutureWarning: In the future np.bool
will be defined as the corresponding NumPy scalar.
if isinstance(index, np.ndarray) and index.dtype == np.bool:
Traceback (most recent call last):
File "c:/Users/RizwanMLE/Desktop/freelancing projects/cv/waleed_fyp/app/test2.py", line 157, in np.bool
was a deprecated alias for the builtin bool
. To avoid this error in existing code, use bool
by itself. Doing this will not modify any behavior
and is safe. If you specifically wanted the numpy scalar type, use np.bool_
here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
Additional
No response
Hi @Rizwanali324 :wave:
What versions of supervision are you using? Would it be feasible to upgrade to a newer one?
If you do upgrade, here's the changes you'd need to make in the code:
- We're using
sv.Color.RED
instead ofsv.Color.red()
- We've changed
sv.Detections.from_yolov8
tosv.Detections.from_ultralytics
Let me know how it goes!
Hi @Rizwanali324 👋
What versions of supervision are you using? Would it be feasible to upgrade to a newer one?
If you do upgrade, here's the changes you'd need to make in the code:
- We're using
sv.Color.RED
instead ofsv.Color.red()
- We've changed
sv.Detections.from_yolov8
tosv.Detections.from_ultralytics
Let me know how it goes!
I'm using 0.2.0
which version should i used
That is very old. I suggest the latest one - 0.19.0
.
That is very old. I suggest the latest one -
0.19.0
.
ok. I updated it. one more question is my end goal is to only track and count the user input class and process the frame. which i implement later . I'm trying it . but i searched a lot and watched many videos on Robflow and supervision. its only works on images like only show users input classes and displaying the bounding box. but in CLI all the classes are displayed. will it affect the goal that i want to achieve
Good to hear it worked! Happy to help with your problem as well :slightly_smiling_face:
I see you're already doing detections = detections[detections.class_id == 0]
.
Can you explain in more details, how the "user input class" is created? Is that something the user selects? How much choice do users have?
I don't understand the situation well enough, but upon guessing what you might mean, my intuition says that either a model that differentiates from different classes is needed, or a classification model to further distinguish between results.
Good to hear it worked! Happy to help with your problem as well 🙂
I see you're already doing
detections = detections[detections.class_id == 0]
.Can you explain in more details, how the "user input class" is created? Is that something the user selects? How much choice do users have?
I don't understand the situation well enough, but upon guessing what you might mean, my intuition says that either a model that differentiates from different classes is needed, or a classification model to further distinguish between results.
here is my GUI . u can look it.i added code to select the coco dataset class . whenever user select the class ,class id will auto change/selected.
Good to hear it worked! Happy to help with your problem as well 🙂 I see you're already doing
detections = detections[detections.class_id == 0]
. Can you explain in more details, how the "user input class" is created? Is that something the user selects? How much choice do users have? I don't understand the situation well enough, but upon guessing what you might mean, my intuition says that either a model that differentiates from different classes is needed, or a classification model to further distinguish between results.
here is my GUI . u can look it.i added code to select the coco dataset class . whenever user select the class ,class id will auto change/selected.
here is review paper. its my fyp . https://www.researchgate.net/publication/360555992_An_Effective_Video_Summarization_Framework_Based_on_the_Object_of_Interest_Using_Deep_Learning
I see the method for user selection - thanks. So there's one class at-a-time.
Unfortunately, I won't have time to go through the paper :)
Is the problem that all classes are detected?
If so, I recommend printing out the detections.class_id
to see which are detected and work from there.
Hi @LinasKo and @Rizwanali324 👋🏻 I'm converting this issue into a discussion and putting it in the Q&A section.