From 9a2a87a170e4b5506fbbd04dc80c121fb5ed5c29 Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:58:11 +0530 Subject: [PATCH 01/11] Just writing the title --- docs/how_to/track_objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index c9497945..8477a4a5 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -1 +1 @@ -🚧 Page under construction. +## How To: Track Object From f82f297f2e1f2c79f46acd281bc34f48c8e9fb61 Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:58:55 +0530 Subject: [PATCH 02/11] Update track_objects.md Typo --- docs/how_to/track_objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index 8477a4a5..b5b13caa 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -1 +1 @@ -## How To: Track Object +## How To: Track Objects From b1f207f2ee307aafb28a2b99b75a704ffc70e2ec Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:47:16 +0530 Subject: [PATCH 03/11] Update track_objects.md Added all the subtopics and 'what is tracking' --- docs/how_to/track_objects.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index b5b13caa..d714ec69 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -1 +1,9 @@ ## How To: Track Objects +This is a tutorial to track object in a video using [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). +### What is Tracking? +Tracking is an essential application of computer vision, which is used in mainly in videos or set of images which contain a sequence of events(the set of images could be simply frames of a video). It takes an initial set of detections from a video as per the requirement of the project, provides some unique identification to the detections and tracks as the position changes with time in a video. Supervision provides some powerful tools to track object in videos. This tutorial would cover how to make detections in a video, track those detections and then label them for unique identification using Supervision. Further, it would also cover how to trace the path covered by the detections. +### Step 1:Building a detection pipeline for videos +### Step 2:Tracking the detections +### Step 3:Labels the detections being tracked +### Step 4:Tracing the path of the detections +### Conclusion and further ideas From 9572469f5feedf09b057a5014598d5824161eabe Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:46:49 +0530 Subject: [PATCH 04/11] Final changes left --- docs/how_to/track_objects.md | 73 +++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index d714ec69..e772f134 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -2,8 +2,79 @@ This is a tutorial to track object in a video using [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). ### What is Tracking? Tracking is an essential application of computer vision, which is used in mainly in videos or set of images which contain a sequence of events(the set of images could be simply frames of a video). It takes an initial set of detections from a video as per the requirement of the project, provides some unique identification to the detections and tracks as the position changes with time in a video. Supervision provides some powerful tools to track object in videos. This tutorial would cover how to make detections in a video, track those detections and then label them for unique identification using Supervision. Further, it would also cover how to trace the path covered by the detections. +Before proceeding remember to install Supervision: +''' +pip install supervision +''' +and import it: +''' +import supervision as sv +''' ### Step 1:Building a detection pipeline for videos +First to track objects we need to load the data and make it usable. We are going to do it by generating the frames from the video and using an object detection model, using [sv.get_video_frames_generator()](https://supervision.roboflow.com/utils/video/#get_video_frames_generator). In this tutorial we are going to do it using YOLOv8. +First install ultralytics, import it and initialize the frame_generator. +''' +pip install ultralytics +''' +''' +from ultralytics import YOLO +model = YOLO(...) +frame_generator = sv.get_video_frames_generator(source_path=source_video_path) +''' +Replace '...' with the path of v8 model ### Step 2:Tracking the detections -### Step 3:Labels the detections being tracked +To track the detections we are going to use [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). Further, the detections in each frame are going tbe passed into [sv.Detections.from_ultralytics()](https://supervision.roboflow.com/detection/core/), whose output would be passed on to ByteTrack. +''' +tracker = sv.ByteTrack() + +for frame in frame_generator: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) +''' +### Step 3:Label the detections being tracked +The objects being tracked can be annotated with any [annotator provided by supervision](https://supervision.roboflow.com/annotators/#labelannotator). But, we are going to use label annotator and label the object being tracked with their tracking ids. +''' +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +for frame in frame_generator: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + labels = detections.tracker_id + annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) +''' ### Step 4:Tracing the path of the detections +Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. +''' + +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +trace_annotator = sv.TraceAnnotator() +for frame in frame_generator: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + labels = detections.tracker_id + label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) + trace_annotator.annotate(scene=frame.copy(), detections=detections) +### Step 5:Integrating the previous steps and processing the video +At the end, we want to see the output on our video. In order to do that, we'll be passing the annotations from previous iterations into a callback to be used by [sv.process_video](https://supervision.roboflow.com/utils/video/#process_video). +''' +from ultralytics import YOLO +model = YOLO(...) +frame_generator = sv.get_video_frames_generator(source_path=source_video_path) +tracker = sv.ByteTrack() +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +trace_annotator = sv.TraceAnnotator() +def callback(frame: np.ndarray, index: int) -> np.ndarray: + for frame in frame_generator: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + labels = detections.tracker_id + label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) + trace_annotator.annotate(scene=frame.copy(), detections=detections) +sv.process_video(source_path=VIDEO_PATH, target_path=f"result.mp4", callback=process_frame) +''' +Results with label annotator and trace annotator are show below in sequential frames from a video. ### Conclusion and further ideas +This brings us to the end of the tutorial. The users are strongly encouraged to try out other models for detection and play around with annotators. Check out this [post](https://blog.roboflow.com/yolov8-tracking-and-counting/#object-tracking-with-bytetrack)("Piotr Skalski." Roboflow Blog, Feb 1, 2023. https://blog.roboflow.com/yolov8-tracking-and-counting/) for some more cool stuff. The post was quite useful creation of this tutorial. From 377b3fdda70f22d4b53c574e30390e1eae72f10e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 13:17:01 +0000 Subject: [PATCH 05/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/how_to/track_objects.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index e772f134..d9ae67f0 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -11,7 +11,7 @@ and import it: import supervision as sv ''' ### Step 1:Building a detection pipeline for videos -First to track objects we need to load the data and make it usable. We are going to do it by generating the frames from the video and using an object detection model, using [sv.get_video_frames_generator()](https://supervision.roboflow.com/utils/video/#get_video_frames_generator). In this tutorial we are going to do it using YOLOv8. +First to track objects we need to load the data and make it usable. We are going to do it by generating the frames from the video and using an object detection model, using [sv.get_video_frames_generator()](https://supervision.roboflow.com/utils/video/#get_video_frames_generator). In this tutorial we are going to do it using YOLOv8. First install ultralytics, import it and initialize the frame_generator. ''' pip install ultralytics @@ -35,7 +35,7 @@ for frame in frame_generator: ### Step 3:Label the detections being tracked The objects being tracked can be annotated with any [annotator provided by supervision](https://supervision.roboflow.com/annotators/#labelannotator). But, we are going to use label annotator and label the object being tracked with their tracking ids. ''' -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) for frame in frame_generator: results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) @@ -47,7 +47,7 @@ for frame in frame_generator: Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. ''' -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) trace_annotator = sv.TraceAnnotator() for frame in frame_generator: results = model(frame)[0] @@ -63,7 +63,7 @@ from ultralytics import YOLO model = YOLO(...) frame_generator = sv.get_video_frames_generator(source_path=source_video_path) tracker = sv.ByteTrack() -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) +label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) trace_annotator = sv.TraceAnnotator() def callback(frame: np.ndarray, index: int) -> np.ndarray: for frame in frame_generator: From cd76b79ec5e3e6414f225e6d14d1d8d3e0f1bc1d Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:51:53 +0530 Subject: [PATCH 06/11] Update track_objects.md --- docs/how_to/track_objects.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index d9ae67f0..4d3462d4 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -3,38 +3,38 @@ This is a tutorial to track object in a video using [ByteTrack](https://supervis ### What is Tracking? Tracking is an essential application of computer vision, which is used in mainly in videos or set of images which contain a sequence of events(the set of images could be simply frames of a video). It takes an initial set of detections from a video as per the requirement of the project, provides some unique identification to the detections and tracks as the position changes with time in a video. Supervision provides some powerful tools to track object in videos. This tutorial would cover how to make detections in a video, track those detections and then label them for unique identification using Supervision. Further, it would also cover how to trace the path covered by the detections. Before proceeding remember to install Supervision: -''' +``` pip install supervision -''' +``` and import it: -''' +``` import supervision as sv -''' +``` ### Step 1:Building a detection pipeline for videos First to track objects we need to load the data and make it usable. We are going to do it by generating the frames from the video and using an object detection model, using [sv.get_video_frames_generator()](https://supervision.roboflow.com/utils/video/#get_video_frames_generator). In this tutorial we are going to do it using YOLOv8. First install ultralytics, import it and initialize the frame_generator. -''' +``` pip install ultralytics -''' -''' +``` +``` from ultralytics import YOLO model = YOLO(...) frame_generator = sv.get_video_frames_generator(source_path=source_video_path) -''' +``` Replace '...' with the path of v8 model ### Step 2:Tracking the detections To track the detections we are going to use [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). Further, the detections in each frame are going tbe passed into [sv.Detections.from_ultralytics()](https://supervision.roboflow.com/detection/core/), whose output would be passed on to ByteTrack. -''' +``` tracker = sv.ByteTrack() for frame in frame_generator: results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) detections = tracker.update_with_detections(detections) -''' +``` ### Step 3:Label the detections being tracked The objects being tracked can be annotated with any [annotator provided by supervision](https://supervision.roboflow.com/annotators/#labelannotator). But, we are going to use label annotator and label the object being tracked with their tracking ids. -''' +``` label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) for frame in frame_generator: results = model(frame)[0] @@ -42,10 +42,10 @@ for frame in frame_generator: detections = tracker.update_with_detections(detections) labels = detections.tracker_id annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) -''' +``` ### Step 4:Tracing the path of the detections Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. -''' +``` label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) trace_annotator = sv.TraceAnnotator() @@ -56,9 +56,10 @@ for frame in frame_generator: labels = detections.tracker_id label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) trace_annotator.annotate(scene=frame.copy(), detections=detections) +``` ### Step 5:Integrating the previous steps and processing the video At the end, we want to see the output on our video. In order to do that, we'll be passing the annotations from previous iterations into a callback to be used by [sv.process_video](https://supervision.roboflow.com/utils/video/#process_video). -''' +``` from ultralytics import YOLO model = YOLO(...) frame_generator = sv.get_video_frames_generator(source_path=source_video_path) @@ -74,7 +75,7 @@ def callback(frame: np.ndarray, index: int) -> np.ndarray: label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) trace_annotator.annotate(scene=frame.copy(), detections=detections) sv.process_video(source_path=VIDEO_PATH, target_path=f"result.mp4", callback=process_frame) -''' +``` Results with label annotator and trace annotator are show below in sequential frames from a video. ### Conclusion and further ideas This brings us to the end of the tutorial. The users are strongly encouraged to try out other models for detection and play around with annotators. Check out this [post](https://blog.roboflow.com/yolov8-tracking-and-counting/#object-tracking-with-bytetrack)("Piotr Skalski." Roboflow Blog, Feb 1, 2023. https://blog.roboflow.com/yolov8-tracking-and-counting/) for some more cool stuff. The post was quite useful creation of this tutorial. From 9c96180a74ade12fe76f612220aa52b306438a9d Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:53:58 +0530 Subject: [PATCH 07/11] Update track_objects.md --- docs/how_to/track_objects.md | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index 4d3462d4..8a5e0ca4 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -36,46 +36,33 @@ for frame in frame_generator: The objects being tracked can be annotated with any [annotator provided by supervision](https://supervision.roboflow.com/annotators/#labelannotator). But, we are going to use label annotator and label the object being tracked with their tracking ids. ``` label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) -for frame in frame_generator: - results = model(frame)[0] - detections = sv.Detections.from_ultralytics(results) - detections = tracker.update_with_detections(detections) - labels = detections.tracker_id - annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) + ``` ### Step 4:Tracing the path of the detections -Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. +Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. To use tracce annotator ensure that the same detections remain in all the frames of the video. ``` label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) trace_annotator = sv.TraceAnnotator() -for frame in frame_generator: - results = model(frame)[0] - detections = sv.Detections.from_ultralytics(results) - detections = tracker.update_with_detections(detections) - labels = detections.tracker_id - label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) - trace_annotator.annotate(scene=frame.copy(), detections=detections) ``` ### Step 5:Integrating the previous steps and processing the video At the end, we want to see the output on our video. In order to do that, we'll be passing the annotations from previous iterations into a callback to be used by [sv.process_video](https://supervision.roboflow.com/utils/video/#process_video). ``` from ultralytics import YOLO -model = YOLO(...) +model = YOLO(...) ##We YOLO('yolov8n.pt') frame_generator = sv.get_video_frames_generator(source_path=source_video_path) tracker = sv.ByteTrack() label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) trace_annotator = sv.TraceAnnotator() def callback(frame: np.ndarray, index: int) -> np.ndarray: - for frame in frame_generator: - results = model(frame)[0] - detections = sv.Detections.from_ultralytics(results) - detections = tracker.update_with_detections(detections) - labels = detections.tracker_id - label_annotator.annotate(scene=frame.copy(), detections=detections, labels=labels) - trace_annotator.annotate(scene=frame.copy(), detections=detections) + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + labels = f"{detections.tracker_id}" + label_annotator.annotate(scene=frame, detections=detections, labels=labels) + return trace_annotator.annotate(scene=frame, detections=detections) sv.process_video(source_path=VIDEO_PATH, target_path=f"result.mp4", callback=process_frame) ``` -Results with label annotator and trace annotator are show below in sequential frames from a video. +Check out this [video](https://drive.google.com/file/d/10xaHXz9rpdXkaRni9YzOihhF3S7-Zs4p/view?usp=sharing) to see the end results. ### Conclusion and further ideas This brings us to the end of the tutorial. The users are strongly encouraged to try out other models for detection and play around with annotators. Check out this [post](https://blog.roboflow.com/yolov8-tracking-and-counting/#object-tracking-with-bytetrack)("Piotr Skalski." Roboflow Blog, Feb 1, 2023. https://blog.roboflow.com/yolov8-tracking-and-counting/) for some more cool stuff. The post was quite useful creation of this tutorial. From 47dcb1982942ba7956f18732e244194466430ee2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:24:29 +0000 Subject: [PATCH 08/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/how_to/track_objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index 8a5e0ca4..8a2efbdf 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -63,6 +63,6 @@ def callback(frame: np.ndarray, index: int) -> np.ndarray: return trace_annotator.annotate(scene=frame, detections=detections) sv.process_video(source_path=VIDEO_PATH, target_path=f"result.mp4", callback=process_frame) ``` -Check out this [video](https://drive.google.com/file/d/10xaHXz9rpdXkaRni9YzOihhF3S7-Zs4p/view?usp=sharing) to see the end results. +Check out this [video](https://drive.google.com/file/d/10xaHXz9rpdXkaRni9YzOihhF3S7-Zs4p/view?usp=sharing) to see the end results. ### Conclusion and further ideas This brings us to the end of the tutorial. The users are strongly encouraged to try out other models for detection and play around with annotators. Check out this [post](https://blog.roboflow.com/yolov8-tracking-and-counting/#object-tracking-with-bytetrack)("Piotr Skalski." Roboflow Blog, Feb 1, 2023. https://blog.roboflow.com/yolov8-tracking-and-counting/) for some more cool stuff. The post was quite useful creation of this tutorial. From 3dbbcf79ae3d719bf1577208b09d54c1ff582bbf Mon Sep 17 00:00:00 2001 From: Rajarshi Misra <72802594+Rajarshi-Misra@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:55:41 +0530 Subject: [PATCH 09/11] Final --- supervision/annotators/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 895b8933..7aa4dab0 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -754,7 +754,7 @@ class BlurAnnotator(BaseAnnotator): >>> detections = sv.Detections(...) >>> blur_annotator = sv.BlurAnnotator() - >>> annotated_frame = blur_annotator.annotate( + >>> annotated_frame = circle_annotator.annotate( ... scene=image.copy(), ... detections=detections ... ) From 769f97cd7df130fa614f03cd0048b569623dbb5a Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Sat, 28 Oct 2023 01:12:09 +0200 Subject: [PATCH 10/11] Update object tracking documentation Revise the contents of the 'How to Track Objects' documentation covering the use of supervision for object tracking in video analysis. The revision offers a more detailed guide, including running inference, tracking objects, annotating videos with unique tracking IDs, and annotating videos with traces. The new guide provides a structured walkthrough and tutorial on using Supervision's functionalities, aimed at helping users to annotate videos more effectively and understand the movement patterns and interactions of objects in the videos. --- docs/how_to/track_objects.md | 231 ++++++++++++++++++++++++++--------- 1 file changed, 173 insertions(+), 58 deletions(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index 8a2efbdf..f8b40cc6 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -1,68 +1,183 @@ -## How To: Track Objects -This is a tutorial to track object in a video using [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). -### What is Tracking? -Tracking is an essential application of computer vision, which is used in mainly in videos or set of images which contain a sequence of events(the set of images could be simply frames of a video). It takes an initial set of detections from a video as per the requirement of the project, provides some unique identification to the detections and tracks as the position changes with time in a video. Supervision provides some powerful tools to track object in videos. This tutorial would cover how to make detections in a video, track those detections and then label them for unique identification using Supervision. Further, it would also cover how to trace the path covered by the detections. -Before proceeding remember to install Supervision: -``` -pip install supervision -``` -and import it: -``` -import supervision as sv -``` -### Step 1:Building a detection pipeline for videos -First to track objects we need to load the data and make it usable. We are going to do it by generating the frames from the video and using an object detection model, using [sv.get_video_frames_generator()](https://supervision.roboflow.com/utils/video/#get_video_frames_generator). In this tutorial we are going to do it using YOLOv8. -First install ultralytics, import it and initialize the frame_generator. -``` -pip install ultralytics -``` -``` -from ultralytics import YOLO -model = YOLO(...) -frame_generator = sv.get_video_frames_generator(source_path=source_video_path) -``` -Replace '...' with the path of v8 model -### Step 2:Tracking the detections -To track the detections we are going to use [ByteTrack](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack). Further, the detections in each frame are going tbe passed into [sv.Detections.from_ultralytics()](https://supervision.roboflow.com/detection/core/), whose output would be passed on to ByteTrack. -``` -tracker = sv.ByteTrack() +Utilize Supervision to elevate your video analysis capabilities by effortlessly +[tracking](https://supervision.roboflow.com/trackers/) objects identified by various +object detection and segmentation models. This guide will walk you through the process +of running inference using the [Ultralytics](https://github.com/ultralytics/ultralytics) +YOLOv8 model, subsequently tracking these objects, and annotating the video. -for frame in frame_generator: +To make it easier for you to follow our tutorial download the video we will use as an +example. You can do this using +[`supervision[assets]`](https://supervision.roboflow.com/assets/) extension. + +```python +from supervision.assets import download_assets, VideoAssets + +download_assets(VideoAssets.PEOPLE_WALKING) +``` + + + +## Run Inference + +First, you'll need to obtain predictions from your object detection or segmentation +model. In this tutorial, we are using the YOLOv8 model as an example. However, +Supervision is versatile and compatible with various models. Check this +[link](https://supervision.roboflow.com/how_to/detect_and_annotate/#load-predictions-into-supervision) +for guidance on how to plug in other models. + +We will define a `callback` function, which will process each frame of the video +by obtaining model predictions and then annotating the frame based on these predictions. +This `callback` function will be essential in the subsequent steps of the tutorial, as +it will be modified to include tracking, labeling, and trace annotations. + +```{ .py } +import numpy as np +import supervision as sv +from ultralytics import YOLO + +model = YOLO("yolov8n.pt") +box_annotator = sv.BoundingBoxAnnotator() + +def callback(frame: np.ndarray, _: int) -> np.ndarray: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + return box_annotator.annotate(frame.copy(), detections=detections) + +sv.process_video( + source_path="people-walking.mp4", + target_path="result.mp4", + callback=callback +) +``` + + + +## Tracking + +After running inference and obtaining predictions, the next step is to track the +detected objects throughout the video. Utilizing Supervision’s +[`sv.ByteTrack`](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack) +functionality, each detected object is assigned a unique tracker ID, +enabling the continuous following of the object's motion path across different frames. + +```{ .py hl_lines="6 12" } +import numpy as np +import supervision as sv +from ultralytics import YOLO + +model = YOLO("yolov8n.pt") +tracker = sv.ByteTrack() +box_annotator = sv.BoundingBoxAnnotator() + +def callback(frame: np.ndarray, _: int) -> np.ndarray: results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) detections = tracker.update_with_detections(detections) -``` -### Step 3:Label the detections being tracked -The objects being tracked can be annotated with any [annotator provided by supervision](https://supervision.roboflow.com/annotators/#labelannotator). But, we are going to use label annotator and label the object being tracked with their tracking ids. -``` -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) + return box_annotator.annotate(frame.copy(), detections=detections) -``` -### Step 4:Tracing the path of the detections -Further, the path of the tracked objects can be traced using [TraceAnnotator](https://supervision.roboflow.com/annotators/#traceannotator) of supervision. To use tracce annotator ensure that the same detections remain in all the frames of the video. +sv.process_video( + source_path="people-walking.mp4", + target_path="result.mp4", + callback=callback +) ``` -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) -trace_annotator = sv.TraceAnnotator() -``` -### Step 5:Integrating the previous steps and processing the video -At the end, we want to see the output on our video. In order to do that, we'll be passing the annotations from previous iterations into a callback to be used by [sv.process_video](https://supervision.roboflow.com/utils/video/#process_video). -``` +## Annotate Video with Tracking IDs + +Annotating the video with tracking IDs helps in distinguishing and following each object +distinctly. With the +[`sv.LabelAnnotator`](https://supervision.roboflow.com/annotators/#supervision.annotators.core.LabelAnnotator) +in Supervision, we can overlay the tracker IDs and class labels on the detected objects, +offering a clear visual representation of each object's class and unique identifier. + +```{ .py hl_lines="8 15-19 23-24" } +import numpy as np +import supervision as sv from ultralytics import YOLO -model = YOLO(...) ##We YOLO('yolov8n.pt') -frame_generator = sv.get_video_frames_generator(source_path=source_video_path) + +model = YOLO("yolov8n.pt") tracker = sv.ByteTrack() -label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER) -trace_annotator = sv.TraceAnnotator() -def callback(frame: np.ndarray, index: int) -> np.ndarray: - results = model(frame)[0] - detections = sv.Detections.from_ultralytics(results) - detections = tracker.update_with_detections(detections) - labels = f"{detections.tracker_id}" - label_annotator.annotate(scene=frame, detections=detections, labels=labels) - return trace_annotator.annotate(scene=frame, detections=detections) -sv.process_video(source_path=VIDEO_PATH, target_path=f"result.mp4", callback=process_frame) +box_annotator = sv.BoundingBoxAnnotator() +label_annotator = sv.LabelAnnotator() + +def callback(frame: np.ndarray, _: int) -> np.ndarray: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + + labels = [ + f"#{tracker_id} {results.names[class_id]}" + for class_id, tracker_id + in zip(detections.class_id, detections.tracker_id) + ] + + annotated_frame = box_annotator.annotate( + frame.copy(), detections=detections) + return label_annotator.annotate( + annotated_frame, detections=detections, labels=labels) + +sv.process_video( + source_path="people-walking.mp4", + target_path="result.mp4", + callback=callback +) ``` -Check out this [video](https://drive.google.com/file/d/10xaHXz9rpdXkaRni9YzOihhF3S7-Zs4p/view?usp=sharing) to see the end results. -### Conclusion and further ideas -This brings us to the end of the tutorial. The users are strongly encouraged to try out other models for detection and play around with annotators. Check out this [post](https://blog.roboflow.com/yolov8-tracking-and-counting/#object-tracking-with-bytetrack)("Piotr Skalski." Roboflow Blog, Feb 1, 2023. https://blog.roboflow.com/yolov8-tracking-and-counting/) for some more cool stuff. The post was quite useful creation of this tutorial. + + + +## Annotate Video with Traces + +Adding traces to the video involves overlaying the historical paths of the detected +objects. This feature, powered by the +[`sv.TraceAnnotator`](https://supervision.roboflow.com/annotators/#supervision.annotators.core.TraceAnnotator), +allows for visualizing the trajectories of objects, helping in understanding the +movement patterns and interactions between objects in the video. + +```{ .py hl_lines="9 26-27" } +import numpy as np +import supervision as sv +from ultralytics import YOLO + +model = YOLO("yolov8n.pt") +tracker = sv.ByteTrack() +box_annotator = sv.BoundingBoxAnnotator() +label_annotator = sv.LabelAnnotator() +trace_annotator = sv.TraceAnnotator() + +def callback(frame: np.ndarray, _: int) -> np.ndarray: + results = model(frame)[0] + detections = sv.Detections.from_ultralytics(results) + detections = tracker.update_with_detections(detections) + + labels = [ + f"#{tracker_id} {results.names[class_id]}" + for class_id, tracker_id + in zip(detections.class_id, detections.tracker_id) + ] + + annotated_frame = box_annotator.annotate( + frame.copy(), detections=detections) + annotated_frame = label_annotator.annotate( + annotated_frame, detections=detections, labels=labels) + return trace_annotator.annotate( + annotated_frame, detections=detections) + +sv.process_video( + source_path="people-walking.mp4", + target_path="result.mp4", + callback=callback +) +``` + + + +This structured walkthrough should give a detailed pathway to annotate videos +effectively using Supervision’s various functionalities, including object tracking and +trace annotations. \ No newline at end of file From 21fe102bbd4990957d18efa110df2a35c6db28c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Oct 2023 23:12:25 +0000 Subject: [PATCH 11/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/how_to/track_objects.md | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/docs/how_to/track_objects.md b/docs/how_to/track_objects.md index f8b40cc6..ea48b070 100644 --- a/docs/how_to/track_objects.md +++ b/docs/how_to/track_objects.md @@ -1,11 +1,11 @@ -Utilize Supervision to elevate your video analysis capabilities by effortlessly -[tracking](https://supervision.roboflow.com/trackers/) objects identified by various -object detection and segmentation models. This guide will walk you through the process +Utilize Supervision to elevate your video analysis capabilities by effortlessly +[tracking](https://supervision.roboflow.com/trackers/) objects identified by various +object detection and segmentation models. This guide will walk you through the process of running inference using the [Ultralytics](https://github.com/ultralytics/ultralytics) YOLOv8 model, subsequently tracking these objects, and annotating the video. -To make it easier for you to follow our tutorial download the video we will use as an -example. You can do this using +To make it easier for you to follow our tutorial download the video we will use as an +example. You can do this using [`supervision[assets]`](https://supervision.roboflow.com/assets/) extension. ```python @@ -20,15 +20,15 @@ download_assets(VideoAssets.PEOPLE_WALKING) ## Run Inference -First, you'll need to obtain predictions from your object detection or segmentation -model. In this tutorial, we are using the YOLOv8 model as an example. However, -Supervision is versatile and compatible with various models. Check this -[link](https://supervision.roboflow.com/how_to/detect_and_annotate/#load-predictions-into-supervision) +First, you'll need to obtain predictions from your object detection or segmentation +model. In this tutorial, we are using the YOLOv8 model as an example. However, +Supervision is versatile and compatible with various models. Check this +[link](https://supervision.roboflow.com/how_to/detect_and_annotate/#load-predictions-into-supervision) for guidance on how to plug in other models. We will define a `callback` function, which will process each frame of the video by obtaining model predictions and then annotating the frame based on these predictions. -This `callback` function will be essential in the subsequent steps of the tutorial, as +This `callback` function will be essential in the subsequent steps of the tutorial, as it will be modified to include tracking, labeling, and trace annotations. ```{ .py } @@ -57,10 +57,10 @@ sv.process_video( ## Tracking -After running inference and obtaining predictions, the next step is to track the -detected objects throughout the video. Utilizing Supervision’s -[`sv.ByteTrack`](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack) -functionality, each detected object is assigned a unique tracker ID, +After running inference and obtaining predictions, the next step is to track the +detected objects throughout the video. Utilizing Supervision’s +[`sv.ByteTrack`](https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack) +functionality, each detected object is assigned a unique tracker ID, enabling the continuous following of the object's motion path across different frames. ```{ .py hl_lines="6 12" } @@ -88,8 +88,8 @@ sv.process_video( ## Annotate Video with Tracking IDs Annotating the video with tracking IDs helps in distinguishing and following each object -distinctly. With the -[`sv.LabelAnnotator`](https://supervision.roboflow.com/annotators/#supervision.annotators.core.LabelAnnotator) +distinctly. With the +[`sv.LabelAnnotator`](https://supervision.roboflow.com/annotators/#supervision.annotators.core.LabelAnnotator) in Supervision, we can overlay the tracker IDs and class labels on the detected objects, offering a clear visual representation of each object's class and unique identifier. @@ -107,13 +107,13 @@ def callback(frame: np.ndarray, _: int) -> np.ndarray: results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) detections = tracker.update_with_detections(detections) - + labels = [ f"#{tracker_id} {results.names[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id) ] - + annotated_frame = box_annotator.annotate( frame.copy(), detections=detections) return label_annotator.annotate( @@ -132,10 +132,10 @@ sv.process_video( ## Annotate Video with Traces -Adding traces to the video involves overlaying the historical paths of the detected -objects. This feature, powered by the +Adding traces to the video involves overlaying the historical paths of the detected +objects. This feature, powered by the [`sv.TraceAnnotator`](https://supervision.roboflow.com/annotators/#supervision.annotators.core.TraceAnnotator), -allows for visualizing the trajectories of objects, helping in understanding the +allows for visualizing the trajectories of objects, helping in understanding the movement patterns and interactions between objects in the video. ```{ .py hl_lines="9 26-27" } @@ -153,13 +153,13 @@ def callback(frame: np.ndarray, _: int) -> np.ndarray: results = model(frame)[0] detections = sv.Detections.from_ultralytics(results) detections = tracker.update_with_detections(detections) - + labels = [ f"#{tracker_id} {results.names[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id) ] - + annotated_frame = box_annotator.annotate( frame.copy(), detections=detections) annotated_frame = label_annotator.annotate( @@ -178,6 +178,6 @@ sv.process_video( -This structured walkthrough should give a detailed pathway to annotate videos -effectively using Supervision’s various functionalities, including object tracking and -trace annotations. \ No newline at end of file +This structured walkthrough should give a detailed pathway to annotate videos +effectively using Supervision’s various functionalities, including object tracking and +trace annotations.