From 01e7330e6e1ba15fb10a7a500afa9f9d691ec033 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:57:31 +0000 Subject: [PATCH] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20form?= =?UTF-8?q?at=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotate-video-with-detections.ipynb | 406 +++++++++--------- .../download-supervision-assets.ipynb | 156 +++---- docs/notebooks/object-tracking.ipynb | 300 ++++++------- 3 files changed, 431 insertions(+), 431 deletions(-) diff --git a/docs/notebooks/annotate-video-with-detections.ipynb b/docs/notebooks/annotate-video-with-detections.ipynb index 846e7a58..e3c9ac9f 100644 --- a/docs/notebooks/annotate-video-with-detections.ipynb +++ b/docs/notebooks/annotate-video-with-detections.ipynb @@ -1,205 +1,205 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Annotate Video with Detections\n", - "\n", - "---\n", - "\n", - "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/develop/docs/notebooks/annotate-video-with-detections.ipynb)\n", - "\n", - "One of the most common requirements of computer vision applications is detecting objects in images and displaying bounding boxes around those objects. In this cookbook we'll walk through the steps on how to utilize the open source Roboflow ecosystem to accomplish this task on a video. Let's dive in! \n", - "\n", - "## Installing Dependencies \n", - "\n", - "In this cookbook we'll be utilizing the open source packages [Inference](https://inference.roboflow.com/) and [Supervision](https://supervision.roboflow.com/latest/) to accomplish our goals. Let's get those installed in our notebook with pip." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "\n", - "!pip install -q inference \"supervision[assets]\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Download a Video Asset\n", - "\n", - "First, let's download a video that we can detect objects in. Supervision comes with a great utility called Assets to help us hit the ground running. Wehn we run this script, the video is saved in our local directory and can be accessed with the variable `path_to_video`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from supervision.assets import download_assets, VideoAssets\n", - "\n", - "# Download a supervision video asset \n", - "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Detecting Objects\n", - "\n", - "For this example, the objects in the video that we'd like to detect are people. In order to display bounding boxes around the people in the video, we first need a way to detect them. We'll be using the open source [Inference](https://github.com/roboflow/inference) package for this task. Inference allows us to quickly use thousands of models, including fine tuned models from [Roboflow Universe](https://universe.roboflow.com/), with a few lines of code. We'll also utilize a few utilities for working with our video data from the [Supervision](https://github.com/roboflow/supervision) package." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import supervision as sv\n", - "from supervision.assets import download_assets, VideoAssets\n", - "from inference.models.utils import get_roboflow_model\n", - "\n", - "\n", - "if __name__ == \"__main__\":\n", - " # Download the video asset from Supervision assets.\n", - " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", - "\n", - " # Load a yolov8 nano model from roboflow.\n", - " model = get_roboflow_model(\"yolov8n-640\")\n", - "\n", - " # Create a frame generator and video info object from supervision utilities.\n", - " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", - "\n", - " # Yield a single frame from the generator.\n", - " frame = next(frame_generator)\n", - "\n", - " # Run inference on our frame.\n", - " result = model.infer(frame)[0]\n", - "\n", - " # Parse result into detections data model.\n", - " detections = sv.Detections.from_inference(result)\n", - "\n", - " # Display the detections data model.\n", - " print(detections)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "First, we load our model using the method `get_roboflow_model()`. Notice how we pass in a `model_id`? We're using an [alias](https://inference.roboflow.com/reference_pages/model_aliases/) here. This is where we can pass in other models from Roboflow Universe like this [rock, paper, scissors](https://universe.roboflow.com/roboflow-58fyf/rock-paper-scissors-sxsw) model utilizing our roboflow api key. \n", - "\n", - "```\n", - "model = get_roboflow_mode(\n", - " model_id=\"rock-paper-scissors-sxsw/11\", \n", - " api_key=\"roboflow_private_api_key\"\n", - ")\n", - "```\n", - "\n", - "If you don't have an api key, you can [create an free Roboflow account](https://app.roboflow.com/login). This model wouldn't be much help with detecting people, but it's a nice exercise to see how our code becomes model agnostic!\n", - "\n", - "We then create a `frame_generator` object and yeild a single frame for inference using `next()`. We pass our frame to `model.infer()` to run inference, then pass that data into a little helpfer function called `sv.Detections.from_inference()` to parse it. Lastly we print our detections to show we are in fact detecting a few people in the frame! \n", - "\n", - "## Saving Bounding Boxes to the Video\n", - "\n", - "Now comes the fun part. Let's wrap up our code by utilizing `Annotators` and a `VideoSink` to draw bounding boxes and save the resulting video respectively. Take a peak at the final code example below. This might can take up to a minute to run, since we're processing a full video. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import supervision as sv\n", - "from supervision.assets import download_assets, VideoAssets\n", - "from inference.models.utils import get_roboflow_model\n", - "\n", - "\n", - "if __name__ == \"__main__\":\n", - " # Download the video asset from Supervision assets.\n", - " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", - "\n", - " # Load a yolov8 nano model from roboflow.\n", - " model = get_roboflow_model(\"yolov8n-640\")\n", - "\n", - " # Initalize the bounding box frame annotator.\n", - " box_annotator = sv.BoundingBoxAnnotator()\n", - "\n", - " # Create a frame generator object from video path.\n", - " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", - "\n", - " # Create a video info object from video path.\n", - " video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n", - "\n", - " # Use a VideoSink context manager for saving frames of a video.\n", - " with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n", - "\n", - " # Iterate through frames yielded from the frame_generator.\n", - " for frame in frame_generator:\n", - "\n", - " # Run inference on our frame.\n", - " result = model.infer(frame)[0]\n", - "\n", - " # Parse the result into the detections data model.\n", - " detections = sv.Detections.from_inference(result)\n", - "\n", - " # Apply bounding box to detections on a copy of the frame.\n", - " annotated_frame = box_annotator.annotate(\n", - " scene=frame.copy(), \n", - " detections=detections\n", - " )\n", - "\n", - " # Write the annotated frame to the video sink.\n", - " sink.write_frame(frame=annotated_frame)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " Notice that we create a `box_annoator` variable by initalizing a [BoundingBoxAnnotator](https://supervision.roboflow.com/latest/annotators/#boundingboxannotator). We can change the color and thickness, but for simplicity we keep the defaults. There are a ton of easy to use [annotators](https://supervision.roboflow.com/latest/annotators/) available in the Supervision package other than a bounding box that are fun to play with. Next, we create a `video_info` variable to pass information about the video to our `VideoSink`. The `VideoSink` is a cool little context manager that allows us to `write_frames()` to a video ouput file. Prior to writing the frame, we annotate a copy of it utilizing `box_annotator.annotate()`. Let's take a look at the resulting video. It will be installed locally and is called `output.mp4`. When run, you will see bounding boxes around the detections. Pretty awesome! " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We only scratched the surface of all of the customizable Annotators and additional features that Supervision and Inference have to offer. Stay tuned for more cookbooks on how to take advantge of them in your computer vision applications. Happy building! " - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Annotate Video with Detections\n", + "\n", + "---\n", + "\n", + "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/develop/docs/notebooks/annotate-video-with-detections.ipynb)\n", + "\n", + "One of the most common requirements of computer vision applications is detecting objects in images and displaying bounding boxes around those objects. In this cookbook we'll walk through the steps on how to utilize the open source Roboflow ecosystem to accomplish this task on a video. Let's dive in! \n", + "\n", + "## Installing Dependencies \n", + "\n", + "In this cookbook we'll be utilizing the open source packages [Inference](https://inference.roboflow.com/) and [Supervision](https://supervision.roboflow.com/latest/) to accomplish our goals. Let's get those installed in our notebook with pip." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "\n", + "!pip install -q inference \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Download a Video Asset\n", + "\n", + "First, let's download a video that we can detect objects in. Supervision comes with a great utility called Assets to help us hit the ground running. Wehn we run this script, the video is saved in our local directory and can be accessed with the variable `path_to_video`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "\n", + "# Download a supervision video asset \n", + "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Detecting Objects\n", + "\n", + "For this example, the objects in the video that we'd like to detect are people. In order to display bounding boxes around the people in the video, we first need a way to detect them. We'll be using the open source [Inference](https://github.com/roboflow/inference) package for this task. Inference allows us to quickly use thousands of models, including fine tuned models from [Roboflow Universe](https://universe.roboflow.com/), with a few lines of code. We'll also utilize a few utilities for working with our video data from the [Supervision](https://github.com/roboflow/supervision) package." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import supervision as sv\n", + "from supervision.assets import download_assets, VideoAssets\n", + "from inference.models.utils import get_roboflow_model\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " # Download the video asset from Supervision assets.\n", + " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", + "\n", + " # Load a yolov8 nano model from roboflow.\n", + " model = get_roboflow_model(\"yolov8n-640\")\n", + "\n", + " # Create a frame generator and video info object from supervision utilities.\n", + " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", + "\n", + " # Yield a single frame from the generator.\n", + " frame = next(frame_generator)\n", + "\n", + " # Run inference on our frame.\n", + " result = model.infer(frame)[0]\n", + "\n", + " # Parse result into detections data model.\n", + " detections = sv.Detections.from_inference(result)\n", + "\n", + " # Display the detections data model.\n", + " print(detections)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First, we load our model using the method `get_roboflow_model()`. Notice how we pass in a `model_id`? We're using an [alias](https://inference.roboflow.com/reference_pages/model_aliases/) here. This is where we can pass in other models from Roboflow Universe like this [rock, paper, scissors](https://universe.roboflow.com/roboflow-58fyf/rock-paper-scissors-sxsw) model utilizing our roboflow api key. \n", + "\n", + "```\n", + "model = get_roboflow_mode(\n", + " model_id=\"rock-paper-scissors-sxsw/11\", \n", + " api_key=\"roboflow_private_api_key\"\n", + ")\n", + "```\n", + "\n", + "If you don't have an api key, you can [create an free Roboflow account](https://app.roboflow.com/login). This model wouldn't be much help with detecting people, but it's a nice exercise to see how our code becomes model agnostic!\n", + "\n", + "We then create a `frame_generator` object and yeild a single frame for inference using `next()`. We pass our frame to `model.infer()` to run inference, then pass that data into a little helpfer function called `sv.Detections.from_inference()` to parse it. Lastly we print our detections to show we are in fact detecting a few people in the frame! \n", + "\n", + "## Saving Bounding Boxes to the Video\n", + "\n", + "Now comes the fun part. Let's wrap up our code by utilizing `Annotators` and a `VideoSink` to draw bounding boxes and save the resulting video respectively. Take a peak at the final code example below. This might can take up to a minute to run, since we're processing a full video. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import supervision as sv\n", + "from supervision.assets import download_assets, VideoAssets\n", + "from inference.models.utils import get_roboflow_model\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " # Download the video asset from Supervision assets.\n", + " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", + "\n", + " # Load a yolov8 nano model from roboflow.\n", + " model = get_roboflow_model(\"yolov8n-640\")\n", + "\n", + " # Initalize the bounding box frame annotator.\n", + " box_annotator = sv.BoundingBoxAnnotator()\n", + "\n", + " # Create a frame generator object from video path.\n", + " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", + "\n", + " # Create a video info object from video path.\n", + " video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n", + "\n", + " # Use a VideoSink context manager for saving frames of a video.\n", + " with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n", + "\n", + " # Iterate through frames yielded from the frame_generator.\n", + " for frame in frame_generator:\n", + "\n", + " # Run inference on our frame.\n", + " result = model.infer(frame)[0]\n", + "\n", + " # Parse the result into the detections data model.\n", + " detections = sv.Detections.from_inference(result)\n", + "\n", + " # Apply bounding box to detections on a copy of the frame.\n", + " annotated_frame = box_annotator.annotate(\n", + " scene=frame.copy(), \n", + " detections=detections\n", + " )\n", + "\n", + " # Write the annotated frame to the video sink.\n", + " sink.write_frame(frame=annotated_frame)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " Notice that we create a `box_annoator` variable by initalizing a [BoundingBoxAnnotator](https://supervision.roboflow.com/latest/annotators/#boundingboxannotator). We can change the color and thickness, but for simplicity we keep the defaults. There are a ton of easy to use [annotators](https://supervision.roboflow.com/latest/annotators/) available in the Supervision package other than a bounding box that are fun to play with. Next, we create a `video_info` variable to pass information about the video to our `VideoSink`. The `VideoSink` is a cool little context manager that allows us to `write_frames()` to a video ouput file. Prior to writing the frame, we annotate a copy of it utilizing `box_annotator.annotate()`. Let's take a look at the resulting video. It will be installed locally and is called `output.mp4`. When run, you will see bounding boxes around the detections. Pretty awesome! " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We only scratched the surface of all of the customizable Annotators and additional features that Supervision and Inference have to offer. Stay tuned for more cookbooks on how to take advantge of them in your computer vision applications. Happy building! " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 } diff --git a/docs/notebooks/download-supervision-assets.ipynb b/docs/notebooks/download-supervision-assets.ipynb index 4d8cd97b..4c0f5c67 100644 --- a/docs/notebooks/download-supervision-assets.ipynb +++ b/docs/notebooks/download-supervision-assets.ipynb @@ -1,80 +1,80 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Download Supervision Assets\n", - "\n", - "---\n", - "\n", - "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/develop/docs/notebooks/download-supervision-assets.ipynb)\n", - "\n", - "When experimenting with interesting and useful features of the Supervision package, it's important to have some sort of image or video data to experiment with. Luckily for us, Supervision ships with [Assets](https://supervision.roboflow.com/latest/assets/)! Assets is a collection of videos that you can utilize to start experimenting with the various features Supervision has to offer. Let's take a look at how to use this resource.\n", - "\n", - "## Install Dependencies" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!pip install -q \"supervision[assets]\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Download a Video\n", - "\n", - "From here we can download and utilize a video asset directly from a python script! Note below that we're utilizing the method `download_assets` to download the `VideoAssets.SUBWAY` video to our local directory. This method returns the file path, so we can then utilize this path for additional experimentation. From here, you will see a video asset to experiment with in your local directory. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from supervision.assets import download_assets, VideoAssets\n", - "\n", - "path_to_video = download_assets(VideoAssets.SUBWAY)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We're now equipt with a video asset from Supervision to run some experiments on! For more information on available video assets, visit the [Supervision API Reference](https://supervision.roboflow.com/latest/assets/#videoassets). Happy building!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Download Supervision Assets\n", + "\n", + "---\n", + "\n", + "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/develop/docs/notebooks/download-supervision-assets.ipynb)\n", + "\n", + "When experimenting with interesting and useful features of the Supervision package, it's important to have some sort of image or video data to experiment with. Luckily for us, Supervision ships with [Assets](https://supervision.roboflow.com/latest/assets/)! Assets is a collection of videos that you can utilize to start experimenting with the various features Supervision has to offer. Let's take a look at how to use this resource.\n", + "\n", + "## Install Dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "!pip install -q \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Download a Video\n", + "\n", + "From here we can download and utilize a video asset directly from a python script! Note below that we're utilizing the method `download_assets` to download the `VideoAssets.SUBWAY` video to our local directory. This method returns the file path, so we can then utilize this path for additional experimentation. From here, you will see a video asset to experiment with in your local directory. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "\n", + "path_to_video = download_assets(VideoAssets.SUBWAY)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We're now equipt with a video asset from Supervision to run some experiments on! For more information on available video assets, visit the [Supervision API Reference](https://supervision.roboflow.com/latest/assets/#videoassets). Happy building!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 } diff --git a/docs/notebooks/object-tracking.ipynb b/docs/notebooks/object-tracking.ipynb index dd113499..76eec8b8 100644 --- a/docs/notebooks/object-tracking.ipynb +++ b/docs/notebooks/object-tracking.ipynb @@ -1,152 +1,152 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Object Tracking\n", - "\n", - "---\n", - "\n", - "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/main/docs/notebooks/object-tracking.ipynb)\n", - "\n", - "In some cases, it's important for us to track objects across multiple frames of a video. For example, we may need to figure out the direction a vehicle is moving, or count objects in a frame. Some Supervision [Annotators](https://supervision.roboflow.com/latest/annotators/) and Tools like [LineZone](https://supervision.roboflow.com/latest/detection/tools/line_zone/) require tracking to be setup. In this cookbook, we'll cover how to get a tracker up and running for use in your computer vision applications.\n", - "\n", - "## What is a Tracker?\n", - "\n", - "Trackers are a piece of code that identifies objects across frames and assigns them a unique `tracker_id`. There are a few popular trackers at the time of writing this including ByteTrack and Bot-SORT. Supervision makes using trackers a breeze and comes with ByteTrack built-in. But first, let's get our deppendencies installed. \n", - "\n", - "## Install Dependencies" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "powershell" - } - }, - "outputs": [], - "source": [ - "!pip install -q inference \"supervision[assets]\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Download a Video Asset\n", - "\n", - "Now that we have our enviornment setup, lets download a video that we can detect objects in. Supervision comes with a great utility to help us hit the ground running. We can use the below snippet to he video is save a video asset in our local directory. It can also be accessed with the variable `path_to_video` for additional application logic." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from supervision.assets import download_assets, VideoAssets\n", - "\n", - "# Download a supervision video asset \n", - "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tracking Objects\n", - "\n", - "Now that we have our video installed, let's get to work on tracking objects. We'll first pull in a model from roboflow Inference to detect people in our video. Then let's create a `byte_tracker` object that we'll pass our detections to. This will give us a `tracker_id`. We'll then utilize that tracker id to label our detections with a `label_annotator` to display the tracker id. Finally, we'll use a utility called `VideoSink` to save the annotated frames to a video. Let's dive in to the code." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import supervision as sv\n", - "from supervision.assets import download_assets, VideoAssets\n", - "from inference.models.utils import get_roboflow_model\n", - "\n", - "\n", - "if __name__ == '__main__':\n", - "\n", - " # Install our video from supervision assets.\n", - " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", - "\n", - " # Load a pre trained yolov8 nano model from Roboflow Inference.\n", - " model = get_roboflow_model('yolov8n-640')\n", - "\n", - " # Create a video info object from the video path.\n", - " video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n", - "\n", - " # Create a label annotator for labeling detections with our tracker_id.\n", - " label = sv.LabelAnnotator()\n", - "\n", - " # Create a ByteTrack object to track detections.\n", - " byte_tracker = sv.ByteTrack(frame_rate=video_info.fps)\n", - "\n", - " # Create a frame generator from video path for iteration of frames.\n", - " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", - "\n", - " # Create a video sink context manager to save resulting video.\n", - " with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n", - "\n", - " # Iterate over frames of the video.\n", - " for frame in frame_generator:\n", - "\n", - " # Run inference on the frame by passing it to our model.\n", - " result = model.infer(frame)[0]\n", - "\n", - " # Convert model results to a supervision detection object.\n", - " detections = sv.Detections.from_inference(result)\n", - "\n", - " # Update detections with tracker ids fro byte_tracker.\n", - " tracked_detections = byte_tracker.update_with_detections(detections)\n", - "\n", - " # Create labels with tracker_id for label annotator.\n", - " labels = [ f\"{tracker_id}\" for tracker_id in tracked_detections.tracker_id ]\n", - "\n", - " # Apply label annotator to frame.\n", - " annotated_frame = label.annotate(scene=frame.copy(), detections=tracked_detections, labels=labels)\n", - "\n", - " # Save the annotated frame to an output video.\n", - " sink.write_frame(frame=annotated_frame)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# View the Video\n", - "\n", - "Let's take a look at our resulting video. It will be created in your current directory with the name `output.mp4` Notice how even with a little flicker, we can see the `tracker_id` on the people walking in the video. With trackers under your belt, there are now a wide variety of use cases you can solve for! Happy building! " - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Object Tracking\n", + "\n", + "---\n", + "\n", + "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow/supervision/blob/main/docs/notebooks/object-tracking.ipynb)\n", + "\n", + "In some cases, it's important for us to track objects across multiple frames of a video. For example, we may need to figure out the direction a vehicle is moving, or count objects in a frame. Some Supervision [Annotators](https://supervision.roboflow.com/latest/annotators/) and Tools like [LineZone](https://supervision.roboflow.com/latest/detection/tools/line_zone/) require tracking to be setup. In this cookbook, we'll cover how to get a tracker up and running for use in your computer vision applications.\n", + "\n", + "## What is a Tracker?\n", + "\n", + "Trackers are a piece of code that identifies objects across frames and assigns them a unique `tracker_id`. There are a few popular trackers at the time of writing this including ByteTrack and Bot-SORT. Supervision makes using trackers a breeze and comes with ByteTrack built-in. But first, let's get our deppendencies installed. \n", + "\n", + "## Install Dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "powershell" + } + }, + "outputs": [], + "source": [ + "!pip install -q inference \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Download a Video Asset\n", + "\n", + "Now that we have our enviornment setup, lets download a video that we can detect objects in. Supervision comes with a great utility to help us hit the ground running. We can use the below snippet to he video is save a video asset in our local directory. It can also be accessed with the variable `path_to_video` for additional application logic." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "\n", + "# Download a supervision video asset \n", + "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tracking Objects\n", + "\n", + "Now that we have our video installed, let's get to work on tracking objects. We'll first pull in a model from roboflow Inference to detect people in our video. Then let's create a `byte_tracker` object that we'll pass our detections to. This will give us a `tracker_id`. We'll then utilize that tracker id to label our detections with a `label_annotator` to display the tracker id. Finally, we'll use a utility called `VideoSink` to save the annotated frames to a video. Let's dive in to the code." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import supervision as sv\n", + "from supervision.assets import download_assets, VideoAssets\n", + "from inference.models.utils import get_roboflow_model\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + "\n", + " # Install our video from supervision assets.\n", + " PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)\n", + "\n", + " # Load a pre trained yolov8 nano model from Roboflow Inference.\n", + " model = get_roboflow_model('yolov8n-640')\n", + "\n", + " # Create a video info object from the video path.\n", + " video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n", + "\n", + " # Create a label annotator for labeling detections with our tracker_id.\n", + " label = sv.LabelAnnotator()\n", + "\n", + " # Create a ByteTrack object to track detections.\n", + " byte_tracker = sv.ByteTrack(frame_rate=video_info.fps)\n", + "\n", + " # Create a frame generator from video path for iteration of frames.\n", + " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n", + "\n", + " # Create a video sink context manager to save resulting video.\n", + " with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n", + "\n", + " # Iterate over frames of the video.\n", + " for frame in frame_generator:\n", + "\n", + " # Run inference on the frame by passing it to our model.\n", + " result = model.infer(frame)[0]\n", + "\n", + " # Convert model results to a supervision detection object.\n", + " detections = sv.Detections.from_inference(result)\n", + "\n", + " # Update detections with tracker ids fro byte_tracker.\n", + " tracked_detections = byte_tracker.update_with_detections(detections)\n", + "\n", + " # Create labels with tracker_id for label annotator.\n", + " labels = [ f\"{tracker_id}\" for tracker_id in tracked_detections.tracker_id ]\n", + "\n", + " # Apply label annotator to frame.\n", + " annotated_frame = label.annotate(scene=frame.copy(), detections=tracked_detections, labels=labels)\n", + "\n", + " # Save the annotated frame to an output video.\n", + " sink.write_frame(frame=annotated_frame)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# View the Video\n", + "\n", + "Let's take a look at our resulting video. It will be created in your current directory with the name `output.mp4` Notice how even with a little flicker, we can see the `tracker_id` on the people walking in the video. With trackers under your belt, there are now a wide variety of use cases you can solve for! Happy building! " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 }