fix(pre_commit): 🎨 auto format pre-commit hooks
This commit is contained in:
parent
607d94dad9
commit
019c41bf1a
|
|
@ -1,216 +1,216 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How to Display Bounding Boxes on Objects in a Video.\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 a virtual environment with pip."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "shellscript"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!/bin/bash\n",
|
||||
"!python -m venv venv\n",
|
||||
"!source venv/bin/activate\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. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "powershell"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!open output.mp4"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": [
|
||||
"# How to Display Bounding Boxes on Objects in a Video.\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 a virtual environment with pip."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "shellscript"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!/bin/bash\n",
|
||||
"!python -m venv venv\n",
|
||||
"!source venv/bin/activate\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. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"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. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "powershell"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!open output.mp4"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,140 +1,140 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Tracking Objects in a Video\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. 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 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. First, let's get our deppendencies installed. \n",
|
||||
"\n",
|
||||
"## Install Dependencies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "powershell"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!/bin/bash\n",
|
||||
"!python -m venv venv\n",
|
||||
"!source venv/bin/activate\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 to help us hit the ground running. The videos 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": [
|
||||
"# Tracking Objects\n",
|
||||
"\n",
|
||||
"Now that we have our video installed, let's get to work on tracking objects. We'll pull in a model from roboflow Inference to detect people in our video. We'll then 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`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"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 the yolov8X model from roboflow inference\n",
|
||||
" model = get_roboflow_model('yolov8n-640')\n",
|
||||
"\n",
|
||||
" # get video info from the video path \n",
|
||||
" video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n",
|
||||
"\n",
|
||||
" # create a trace and label annotator, with dynamic video info\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",
|
||||
" # get frames iterable from video and loop over them\n",
|
||||
" frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n",
|
||||
"\n",
|
||||
" # create a video sink context manager to write the annotated frames to\n",
|
||||
" with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n",
|
||||
" for frame in frame_generator:\n",
|
||||
"\n",
|
||||
" # run inference on the frame\n",
|
||||
" result = model.infer(frame)[0]\n",
|
||||
"\n",
|
||||
" # convert the detections to a supervision detections object\n",
|
||||
" detections = sv.Detections.from_inference(result)\n",
|
||||
"\n",
|
||||
" # update detections with tracker ids\n",
|
||||
" tracked_detections = byte_tracker.update_with_detections(detections)\n",
|
||||
"\n",
|
||||
" # create label text for 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 the video sink\n",
|
||||
" sink.write_frame(frame=annotated_frame)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": [
|
||||
"# Tracking Objects in a Video\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. 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 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. First, let's get our deppendencies installed. \n",
|
||||
"\n",
|
||||
"## Install Dependencies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "powershell"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!/bin/bash\n",
|
||||
"!python -m venv venv\n",
|
||||
"!source venv/bin/activate\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 to help us hit the ground running. The videos 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": [
|
||||
"# Tracking Objects\n",
|
||||
"\n",
|
||||
"Now that we have our video installed, let's get to work on tracking objects. We'll pull in a model from roboflow Inference to detect people in our video. We'll then 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`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"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 the yolov8X model from roboflow inference\n",
|
||||
" model = get_roboflow_model('yolov8n-640')\n",
|
||||
"\n",
|
||||
" # get video info from the video path \n",
|
||||
" video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)\n",
|
||||
"\n",
|
||||
" # create a trace and label annotator, with dynamic video info\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",
|
||||
" # get frames iterable from video and loop over them\n",
|
||||
" frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\n",
|
||||
"\n",
|
||||
" # create a video sink context manager to write the annotated frames to\n",
|
||||
" with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n",
|
||||
" for frame in frame_generator:\n",
|
||||
"\n",
|
||||
" # run inference on the frame\n",
|
||||
" result = model.infer(frame)[0]\n",
|
||||
"\n",
|
||||
" # convert the detections to a supervision detections object\n",
|
||||
" detections = sv.Detections.from_inference(result)\n",
|
||||
"\n",
|
||||
" # update detections with tracker ids\n",
|
||||
" tracked_detections = byte_tracker.update_with_detections(detections)\n",
|
||||
"\n",
|
||||
" # create label text for 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 the video sink\n",
|
||||
" sink.write_frame(frame=annotated_frame)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue