From 4bfbe3c8f7a9f40d0356619cc6b0633b49272f74 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Wed, 7 Feb 2024 14:52:43 -0500 Subject: [PATCH 01/20] Add readme for cookbooks directory --- cookbooks/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cookbooks/README.md diff --git a/cookbooks/README.md b/cookbooks/README.md new file mode 100644 index 00000000..21d54785 --- /dev/null +++ b/cookbooks/README.md @@ -0,0 +1,19 @@ +# Supervision Cookbooks + +## About + +The Roboflow Cookbook is an open-source collection of examples and guides for building with the [Roboflow Ecosystem](https://roboflow.com/). + +Cookbooks are meant to be common and small scoped computer vision tasks that can allow a user to quickly hit the ground running. Learning is the main goal and purpose. + +Some examples require a Roboflow API key. You can [create a free account here](https://app.roboflow.com/login). + +## Other Resources + +Other than Cookbooks, Roboflow offers other learning resources including: + +- A set of open source tools found on [Github](https://github.com/roboflow). +- A [blog](https://blog.roboflow.com/) detailing interesting tutorials, computer vision news, and product updates. +- A [Youtube channel](https://www.youtube.com/roboflow) with end to end commputer vision projects. +- A [forum](https://discuss.roboflow.com/) for sharing feedback, discussions, and other questions. +- The worlds largest [open source collection](https://universe.roboflow.com/) of images, datasets, and fine-tuned models. From 9e95e1f0b3ca476c6d82e881c5ef3b0cd7bf7209 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Wed, 7 Feb 2024 14:56:08 -0500 Subject: [PATCH 02/20] Add supervision assets cookbook, correct assets documentation --- ...pervision-assets-for-experimentation.ipynb | 105 ++++++++++++++++++ docs/assets.md | 2 +- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb diff --git a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb new file mode 100644 index 00000000..6033df6d --- /dev/null +++ b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How To Use Supervision Assets for Experimentation\n", + "\n", + "When experimenting with interesting and useful features of the Supervision package, its 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", + "## Installation Methods\n", + "\n", + "To start utilizing assets let's install them with pip.\n", + "\n", + "### Install with pip\n", + "\n", + "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "#!/bin/bash\n", + "!python -m venv venv\n", + "!source venv/bin/activate" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "\n", + "!pip install \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "from IPython.display import Video\n", + "\n", + "path_to_video = download_assets(VideoAssets.SUBWAY)\n", + "Video(path_to_video)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble 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/assets.md b/docs/assets.md index 0cf10316..6114c35e 100644 --- a/docs/assets.md +++ b/docs/assets.md @@ -16,7 +16,7 @@ as an extra within the Supervision package. !!! example "pip install" ```bash - pip install supervision[assets] + pip install "supervision[assets]" ```
From 3150f5a67256c54c5523b355f4eb934e8b996dc3 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Wed, 7 Feb 2024 18:55:24 -0500 Subject: [PATCH 03/20] Add rough draft of bounding box displays --- ...to-display-bounding-boxes-on-a-video.ipynb | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb diff --git a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb new file mode 100644 index 00000000..2f69a715 --- /dev/null +++ b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb @@ -0,0 +1,283 @@ +{ + "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 acomplish this task on a video. Let's dive in! \n", + "\n", + "## Download A Video\n", + "\n", + "First, let's download a video that we can detect objects in. Let's use `curl` to install a video of people walking that Roboflow hosts. We'll then disply the video with the `Video()` function imported from `IPython.display` to see what we're working with." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " % Total % Received % Xferd Average Speed Time Time Time Current\n", + " Dload Upload Total Spent Left Speed\n", + "100 7428k 100 7428k 0 0 5429k 0 0:00:01 0:00:01 --:--:-- 5430k\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "!curl -O https://media.roboflow.com/supervision/video-examples/people-walking.mp4\n", + "\n", + "from IPython.display import Video\n", + "Video(filename=\"people-walking.mp4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Detecting Objects\n", + "\n", + "In order to display bounding boxes on objects, we first need a way to detect those objects. 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/), in a few lines of code. Along with this, we'll be utilizing some utilities for working with our video data from the [Supervision](https://github.com/roboflow/supervision) package. Let's install both packages in a virtual environment with `pip`." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: inference in /opt/homebrew/lib/python3.11/site-packages (0.9.9)\n", + "Requirement already satisfied: supervision in /opt/homebrew/lib/python3.11/site-packages (0.18.0)\n", + "Requirement already satisfied: APScheduler<=3.10.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.10.1)\n", + "Requirement already satisfied: cython<=3.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.0.0)\n", + "Requirement already satisfied: python-dotenv<=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.0.1)\n", + "Requirement already satisfied: fastapi<1.0,>=0.100 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.109.2)\n", + "Requirement already satisfied: numpy<=1.25.2 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.25.2)\n", + "Requirement already satisfied: opencv-python<=4.8.0.76 in /opt/homebrew/lib/python3.11/site-packages (from inference) (4.8.0.76)\n", + "Requirement already satisfied: piexif<=1.1.3 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.1.3)\n", + "Requirement already satisfied: pillow<11.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (10.2.0)\n", + "Requirement already satisfied: prometheus-fastapi-instrumentator<=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (6.0.0)\n", + "Requirement already satisfied: redis<6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (5.0.1)\n", + "Requirement already satisfied: requests>=2.26.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.31.0)\n", + "Requirement already satisfied: rich<=13.5.2 in /opt/homebrew/lib/python3.11/site-packages (from inference) (13.5.2)\n", + "Requirement already satisfied: shapely<=2.0.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.0.1)\n", + "Requirement already satisfied: pybase64<2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.3.2)\n", + "Requirement already satisfied: scikit-image>=0.19.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.22.0)\n", + "Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.0.0)\n", + "Requirement already satisfied: wheel>=0.38.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.42.0)\n", + "Requirement already satisfied: setuptools>=65.5.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (69.0.3)\n", + "Requirement already satisfied: pytest-asyncio<=0.21.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.21.1)\n", + "Requirement already satisfied: networkx>=3.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.2.1)\n", + "Requirement already satisfied: pydantic>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.6.1)\n", + "Requirement already satisfied: onnxruntime<=1.15.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.15.1)\n", + "Requirement already satisfied: GPUtil==1.4.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.4.0)\n", + "Requirement already satisfied: inference-cli==0.9.9 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.9.9)\n", + "Requirement already satisfied: docker==6.1.3 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (6.1.3)\n", + "Requirement already satisfied: typer==0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.9.0)\n", + "Requirement already satisfied: skypilot==0.4.1 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.4.1)\n", + "Requirement already satisfied: inference-sdk>=0.9.8 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.9.9)\n", + "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (6.0.1)\n", + "Requirement already satisfied: tqdm>=4.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (4.66.1)\n", + "Requirement already satisfied: packaging>=14.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (23.2)\n", + "Requirement already satisfied: urllib3>=1.26.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (1.26.18)\n", + "Requirement already satisfied: websocket-client>=0.32.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (1.7.0)\n", + "Requirement already satisfied: cachetools in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (5.3.2)\n", + "Requirement already satisfied: click>=7.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (8.1.7)\n", + "Requirement already satisfied: colorama<0.4.5 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (0.4.4)\n", + "Requirement already satisfied: cryptography in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (42.0.2)\n", + "Requirement already satisfied: jinja2>=3.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.1.3)\n", + "Requirement already satisfied: jsonschema in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (4.21.1)\n", + "Requirement already satisfied: pandas>=1.3.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (2.2.0)\n", + "Requirement already satisfied: pendulum in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.0.0)\n", + "Requirement already satisfied: PrettyTable>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.9.0)\n", + "Requirement already satisfied: tabulate in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (0.9.0)\n", + "Requirement already satisfied: typing-extensions in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (4.9.0)\n", + "Requirement already satisfied: filelock>=3.6.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.13.1)\n", + "Requirement already satisfied: psutil in /Users/nick/Library/Python/3.11/lib/python/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (5.9.8)\n", + "Requirement already satisfied: pulp in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (2.8.0)\n", + "Requirement already satisfied: awscli>=1.27.10 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.32.37)\n", + "Requirement already satisfied: botocore>=1.29.10 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.34.37)\n", + "Requirement already satisfied: boto3>=1.26.1 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.34.37)\n", + "Requirement already satisfied: defusedxml<0.8.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (0.7.1)\n", + "Requirement already satisfied: matplotlib>=3.6.0 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (3.8.2)\n", + "Requirement already satisfied: opencv-python-headless>=4.5.5.64 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (4.9.0.80)\n", + "Requirement already satisfied: scipy<2.0.0,>=1.10.0 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (1.12.0)\n", + "Requirement already satisfied: six>=1.4.0 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from APScheduler<=3.10.1->inference) (1.16.0)\n", + "Requirement already satisfied: pytz in /opt/homebrew/lib/python3.11/site-packages (from APScheduler<=3.10.1->inference) (2024.1)\n", + "Requirement already satisfied: tzlocal!=3.*,>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from APScheduler<=3.10.1->inference) (5.2)\n", + "Requirement already satisfied: starlette<0.37.0,>=0.36.3 in /opt/homebrew/lib/python3.11/site-packages (from fastapi<1.0,>=0.100->inference) (0.36.3)\n", + "Requirement already satisfied: contourpy>=1.0.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (1.2.0)\n", + "Requirement already satisfied: cycler>=0.10 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (0.12.1)\n", + "Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (4.48.1)\n", + "Requirement already satisfied: kiwisolver>=1.3.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (1.4.5)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (3.1.1)\n", + "Requirement already satisfied: python-dateutil>=2.7 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from matplotlib>=3.6.0->supervision) (2.8.2)\n", + "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (15.0.1)\n", + "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (23.5.26)\n", + "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (4.25.2)\n", + "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (1.12)\n", + "Requirement already satisfied: prometheus-client<1.0.0,>=0.8.0 in /opt/homebrew/lib/python3.11/site-packages (from prometheus-fastapi-instrumentator<=6.0.0->inference) (0.19.0)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=2.0->inference) (0.6.0)\n", + "Requirement already satisfied: pydantic-core==2.16.2 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=2.0->inference) (2.16.2)\n", + "Requirement already satisfied: pytest>=7.0.0 in /opt/homebrew/lib/python3.11/site-packages (from pytest-asyncio<=0.21.1->inference) (8.0.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (3.6)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (2024.2.2)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/homebrew/lib/python3.11/site-packages (from rich<=13.5.2->inference) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from rich<=13.5.2->inference) (2.17.2)\n", + "Requirement already satisfied: imageio>=2.27 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (2.33.1)\n", + "Requirement already satisfied: tifffile>=2022.8.12 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (2024.1.30)\n", + "Requirement already satisfied: lazy_loader>=0.3 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (0.3)\n", + "Requirement already satisfied: dataclasses-json>=0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.6.4)\n", + "Requirement already satisfied: aiohttp>=3.9.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (3.9.3)\n", + "Requirement already satisfied: backoff>=2.2.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (2.2.1)\n", + "Requirement already satisfied: aioresponses>=0.7.6 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.7.6)\n", + "Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich<=13.5.2->inference) (0.1.2)\n", + "Requirement already satisfied: iniconfig in /opt/homebrew/lib/python3.11/site-packages (from pytest>=7.0.0->pytest-asyncio<=0.21.1->inference) (2.0.0)\n", + "Requirement already satisfied: pluggy<2.0,>=1.3.0 in /opt/homebrew/lib/python3.11/site-packages (from pytest>=7.0.0->pytest-asyncio<=0.21.1->inference) (1.4.0)\n", + "Requirement already satisfied: anyio<5,>=3.4.0 in /opt/homebrew/lib/python3.11/site-packages (from starlette<0.37.0,>=0.36.3->fastapi<1.0,>=0.100->inference) (4.2.0)\n", + "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime<=1.15.1->inference) (10.0)\n", + "Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->onnxruntime<=1.15.1->inference) (1.3.0)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.3.1)\n", + "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (23.2.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.4.1)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (6.0.5)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.9.4)\n", + "Requirement already satisfied: sniffio>=1.1 in /opt/homebrew/lib/python3.11/site-packages (from anyio<5,>=3.4.0->starlette<0.37.0,>=0.36.3->fastapi<1.0,>=0.100->inference) (1.3.0)\n", + "Requirement already satisfied: docutils<0.17,>=0.10 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.16)\n", + "Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.10.0)\n", + "Requirement already satisfied: rsa<4.8,>=3.1.2 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (4.7.2)\n", + "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from botocore>=1.29.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (1.0.1)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (3.20.2)\n", + "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.9.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from jinja2>=3.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.1.5)\n", + "Requirement already satisfied: tzdata>=2022.7 in /opt/homebrew/lib/python3.11/site-packages (from pandas>=1.3.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (2023.4)\n", + "Requirement already satisfied: wcwidth in /Users/nick/Library/Python/3.11/lib/python/site-packages (from PrettyTable>=2.0.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.2.13)\n", + "Requirement already satisfied: cffi>=1.12 in /opt/homebrew/lib/python3.11/site-packages (from cryptography->skypilot==0.4.1->inference-cli==0.9.9->inference) (1.16.0)\n", + "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (2023.12.1)\n", + "Requirement already satisfied: referencing>=0.28.4 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.33.0)\n", + "Requirement already satisfied: rpds-py>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.17.1)\n", + "Requirement already satisfied: time-machine>=2.6.0 in /opt/homebrew/lib/python3.11/site-packages (from pendulum->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.13.0)\n", + "Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from cffi>=1.12->cryptography->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.21)\n", + "Requirement already satisfied: pyasn1>=0.1.3 in /opt/homebrew/lib/python3.11/site-packages (from rsa<4.8,>=3.1.2->awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.5.1)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in /opt/homebrew/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.0.0)\n" + ] + } + ], + "source": [ + "#!/bin/bash\n", + "!python -m venv venv\n", + "!source venv/bin/activate\n", + "!pip install inference supervision" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we've got our packages installed. Let's talk through the code to display bounding boxes on objects. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/opt/homebrew/lib/python3.11/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py:65: UserWarning: Specified provider 'CUDAExecutionProvider' is not in available provider names.Available providers: 'CPUExecutionProvider'\n", + " warnings.warn(\n" + ] + } + ], + "source": [ + "import supervision as sv\n", + "from inference.models.utils import get_roboflow_model\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + "\n", + " model = get_roboflow_model(\"yolov8n-640\")\n", + " video_name = \"people-walking.mp4\"\n", + " frame_generator = sv.get_video_frames_generator(video_name)\n", + " video_info = sv.VideoInfo.from_video_path(video_name)\n", + "\n", + " box_annotator = sv.BoundingBoxAnnotator()\n", + "\n", + " with sv.VideoSink(target_path=\"output.mp4\", video_info=video_info) as sink:\n", + " for frame in frame_generator:\n", + " result = model.infer(frame)[0]\n", + " detections = sv.Detections.from_inference(result)\n", + " annotated_frame = box_annotator.annotate(scene=frame.copy(), detections=detections)\n", + " sink.write_frame(frame=annotated_frame)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "vscode": { + "languageId": "powershell" + } + }, + "outputs": [], + "source": [ + "!open output.mp4" + ] + } + ], + "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 +} From 7f5198406302f826b74e7f49e3df32d1933c1bc8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:22:51 +0000 Subject: [PATCH 04/20] =?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 --- cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb | 9 +++++---- ...w-to-use-supervision-assets-for-experimentation.ipynb | 1 - 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb index 2f69a715..8f23854c 100644 --- a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb +++ b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb @@ -51,6 +51,7 @@ "!curl -O https://media.roboflow.com/supervision/video-examples/people-walking.mp4\n", "\n", "from IPython.display import Video\n", + "\n", "Video(filename=\"people-walking.mp4\")" ] }, @@ -228,7 +229,6 @@ "\n", "\n", "if __name__ == \"__main__\":\n", - "\n", " model = get_roboflow_model(\"yolov8n-640\")\n", " video_name = \"people-walking.mp4\"\n", " frame_generator = sv.get_video_frames_generator(video_name)\n", @@ -240,9 +240,10 @@ " for frame in frame_generator:\n", " result = model.infer(frame)[0]\n", " detections = sv.Detections.from_inference(result)\n", - " annotated_frame = box_annotator.annotate(scene=frame.copy(), detections=detections)\n", - " sink.write_frame(frame=annotated_frame)\n", - "\n" + " annotated_frame = box_annotator.annotate(\n", + " scene=frame.copy(), detections=detections\n", + " )\n", + " sink.write_frame(frame=annotated_frame)" ] }, { diff --git a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb index 6033df6d..aa395984 100644 --- a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb +++ b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb @@ -49,7 +49,6 @@ }, "outputs": [], "source": [ - "\n", "!pip install \"supervision[assets]\"" ] }, From 4685a0a2ff2b12d591d57076393ae6d4e988f38c Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 11:38:14 -0500 Subject: [PATCH 05/20] Update bounding box conflict --- ...to-display-bounding-boxes-on-a-video.ipynb | 292 +++++++----------- 1 file changed, 110 insertions(+), 182 deletions(-) diff --git a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb index 8f23854c..c5898d23 100644 --- a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb +++ b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb @@ -8,51 +8,50 @@ "\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 acomplish this task on a video. Let's dive in! \n", "\n", - "## Download A Video\n", + "## Installing Deppendencies\n", "\n", - "First, let's download a video that we can detect objects in. Let's use `curl` to install a video of people walking that Roboflow hosts. We'll then disply the video with the `Video()` function imported from `IPython.display` to see what we're working with." + "In this cookbook we'll be utilizing two open source packages. Let's get those installed in a virtual environment with pip." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "vscode": { "languageId": "shellscript" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 7428k 100 7428k 0 0 5429k 0 0:00:01 0:00:01 --:--:-- 5430k\n" - ] - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "!curl -O https://media.roboflow.com/supervision/video-examples/people-walking.mp4\n", + "#!/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. We'll then disply the video with the `Video()` function imported from `IPython.display` to take a look at the video's contents." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", "from IPython.display import Video\n", "\n", - "Video(filename=\"people-walking.mp4\")" + "# Download a supervision video of people walking to current directory\n", + "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)\n", + "\n", + "# Display the video in this notebook\n", + "Video(path_to_video)" ] }, { @@ -61,194 +60,116 @@ "source": [ "## Detecting Objects\n", "\n", - "In order to display bounding boxes on objects, we first need a way to detect those objects. 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/), in a few lines of code. Along with this, we'll be utilizing some utilities for working with our video data from the [Supervision](https://github.com/roboflow/supervision) package. Let's install both packages in a virtual environment with `pip`." + "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": 2, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: inference in /opt/homebrew/lib/python3.11/site-packages (0.9.9)\n", - "Requirement already satisfied: supervision in /opt/homebrew/lib/python3.11/site-packages (0.18.0)\n", - "Requirement already satisfied: APScheduler<=3.10.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.10.1)\n", - "Requirement already satisfied: cython<=3.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.0.0)\n", - "Requirement already satisfied: python-dotenv<=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.0.1)\n", - "Requirement already satisfied: fastapi<1.0,>=0.100 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.109.2)\n", - "Requirement already satisfied: numpy<=1.25.2 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.25.2)\n", - "Requirement already satisfied: opencv-python<=4.8.0.76 in /opt/homebrew/lib/python3.11/site-packages (from inference) (4.8.0.76)\n", - "Requirement already satisfied: piexif<=1.1.3 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.1.3)\n", - "Requirement already satisfied: pillow<11.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (10.2.0)\n", - "Requirement already satisfied: prometheus-fastapi-instrumentator<=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (6.0.0)\n", - "Requirement already satisfied: redis<6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (5.0.1)\n", - "Requirement already satisfied: requests>=2.26.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.31.0)\n", - "Requirement already satisfied: rich<=13.5.2 in /opt/homebrew/lib/python3.11/site-packages (from inference) (13.5.2)\n", - "Requirement already satisfied: shapely<=2.0.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.0.1)\n", - "Requirement already satisfied: pybase64<2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.3.2)\n", - "Requirement already satisfied: scikit-image>=0.19.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.22.0)\n", - "Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.0.0)\n", - "Requirement already satisfied: wheel>=0.38.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.42.0)\n", - "Requirement already satisfied: setuptools>=65.5.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (69.0.3)\n", - "Requirement already satisfied: pytest-asyncio<=0.21.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.21.1)\n", - "Requirement already satisfied: networkx>=3.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (3.2.1)\n", - "Requirement already satisfied: pydantic>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (2.6.1)\n", - "Requirement already satisfied: onnxruntime<=1.15.1 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.15.1)\n", - "Requirement already satisfied: GPUtil==1.4.0 in /opt/homebrew/lib/python3.11/site-packages (from inference) (1.4.0)\n", - "Requirement already satisfied: inference-cli==0.9.9 in /opt/homebrew/lib/python3.11/site-packages (from inference) (0.9.9)\n", - "Requirement already satisfied: docker==6.1.3 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (6.1.3)\n", - "Requirement already satisfied: typer==0.9.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.9.0)\n", - "Requirement already satisfied: skypilot==0.4.1 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.4.1)\n", - "Requirement already satisfied: inference-sdk>=0.9.8 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (0.9.9)\n", - "Requirement already satisfied: PyYAML>=6.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (6.0.1)\n", - "Requirement already satisfied: tqdm>=4.0.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-cli==0.9.9->inference) (4.66.1)\n", - "Requirement already satisfied: packaging>=14.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (23.2)\n", - "Requirement already satisfied: urllib3>=1.26.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (1.26.18)\n", - "Requirement already satisfied: websocket-client>=0.32.0 in /opt/homebrew/lib/python3.11/site-packages (from docker==6.1.3->inference-cli==0.9.9->inference) (1.7.0)\n", - "Requirement already satisfied: cachetools in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (5.3.2)\n", - "Requirement already satisfied: click>=7.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (8.1.7)\n", - "Requirement already satisfied: colorama<0.4.5 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (0.4.4)\n", - "Requirement already satisfied: cryptography in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (42.0.2)\n", - "Requirement already satisfied: jinja2>=3.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.1.3)\n", - "Requirement already satisfied: jsonschema in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (4.21.1)\n", - "Requirement already satisfied: pandas>=1.3.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (2.2.0)\n", - "Requirement already satisfied: pendulum in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.0.0)\n", - "Requirement already satisfied: PrettyTable>=2.0.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.9.0)\n", - "Requirement already satisfied: tabulate in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (0.9.0)\n", - "Requirement already satisfied: typing-extensions in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (4.9.0)\n", - "Requirement already satisfied: filelock>=3.6.0 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (3.13.1)\n", - "Requirement already satisfied: psutil in /Users/nick/Library/Python/3.11/lib/python/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (5.9.8)\n", - "Requirement already satisfied: pulp in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (2.8.0)\n", - "Requirement already satisfied: awscli>=1.27.10 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.32.37)\n", - "Requirement already satisfied: botocore>=1.29.10 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.34.37)\n", - "Requirement already satisfied: boto3>=1.26.1 in /opt/homebrew/lib/python3.11/site-packages (from skypilot==0.4.1->inference-cli==0.9.9->inference) (1.34.37)\n", - "Requirement already satisfied: defusedxml<0.8.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (0.7.1)\n", - "Requirement already satisfied: matplotlib>=3.6.0 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (3.8.2)\n", - "Requirement already satisfied: opencv-python-headless>=4.5.5.64 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (4.9.0.80)\n", - "Requirement already satisfied: scipy<2.0.0,>=1.10.0 in /opt/homebrew/lib/python3.11/site-packages (from supervision) (1.12.0)\n", - "Requirement already satisfied: six>=1.4.0 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from APScheduler<=3.10.1->inference) (1.16.0)\n", - "Requirement already satisfied: pytz in /opt/homebrew/lib/python3.11/site-packages (from APScheduler<=3.10.1->inference) (2024.1)\n", - "Requirement already satisfied: tzlocal!=3.*,>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from APScheduler<=3.10.1->inference) (5.2)\n", - "Requirement already satisfied: starlette<0.37.0,>=0.36.3 in /opt/homebrew/lib/python3.11/site-packages (from fastapi<1.0,>=0.100->inference) (0.36.3)\n", - "Requirement already satisfied: contourpy>=1.0.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (1.2.0)\n", - "Requirement already satisfied: cycler>=0.10 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (0.12.1)\n", - "Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (4.48.1)\n", - "Requirement already satisfied: kiwisolver>=1.3.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (1.4.5)\n", - "Requirement already satisfied: pyparsing>=2.3.1 in /opt/homebrew/lib/python3.11/site-packages (from matplotlib>=3.6.0->supervision) (3.1.1)\n", - "Requirement already satisfied: python-dateutil>=2.7 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from matplotlib>=3.6.0->supervision) (2.8.2)\n", - "Requirement already satisfied: coloredlogs in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (15.0.1)\n", - "Requirement already satisfied: flatbuffers in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (23.5.26)\n", - "Requirement already satisfied: protobuf in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (4.25.2)\n", - "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from onnxruntime<=1.15.1->inference) (1.12)\n", - "Requirement already satisfied: prometheus-client<1.0.0,>=0.8.0 in /opt/homebrew/lib/python3.11/site-packages (from prometheus-fastapi-instrumentator<=6.0.0->inference) (0.19.0)\n", - "Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=2.0->inference) (0.6.0)\n", - "Requirement already satisfied: pydantic-core==2.16.2 in /opt/homebrew/lib/python3.11/site-packages (from pydantic>=2.0->inference) (2.16.2)\n", - "Requirement already satisfied: pytest>=7.0.0 in /opt/homebrew/lib/python3.11/site-packages (from pytest-asyncio<=0.21.1->inference) (8.0.0)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (3.3.2)\n", - "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (3.6)\n", - "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->inference) (2024.2.2)\n", - "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/homebrew/lib/python3.11/site-packages (from rich<=13.5.2->inference) (3.0.0)\n", - "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/nick/Library/Python/3.11/lib/python/site-packages (from rich<=13.5.2->inference) (2.17.2)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (2.33.1)\n", - "Requirement already satisfied: tifffile>=2022.8.12 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (2024.1.30)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/homebrew/lib/python3.11/site-packages (from scikit-image>=0.19.0->inference) (0.3)\n", - "Requirement already satisfied: dataclasses-json>=0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.6.4)\n", - "Requirement already satisfied: aiohttp>=3.9.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (3.9.3)\n", - "Requirement already satisfied: backoff>=2.2.0 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (2.2.1)\n", - "Requirement already satisfied: aioresponses>=0.7.6 in /opt/homebrew/lib/python3.11/site-packages (from inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.7.6)\n", - "Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich<=13.5.2->inference) (0.1.2)\n", - "Requirement already satisfied: iniconfig in /opt/homebrew/lib/python3.11/site-packages (from pytest>=7.0.0->pytest-asyncio<=0.21.1->inference) (2.0.0)\n", - "Requirement already satisfied: pluggy<2.0,>=1.3.0 in /opt/homebrew/lib/python3.11/site-packages (from pytest>=7.0.0->pytest-asyncio<=0.21.1->inference) (1.4.0)\n", - "Requirement already satisfied: anyio<5,>=3.4.0 in /opt/homebrew/lib/python3.11/site-packages (from starlette<0.37.0,>=0.36.3->fastapi<1.0,>=0.100->inference) (4.2.0)\n", - "Requirement already satisfied: humanfriendly>=9.1 in /opt/homebrew/lib/python3.11/site-packages (from coloredlogs->onnxruntime<=1.15.1->inference) (10.0)\n", - "Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->onnxruntime<=1.15.1->inference) (1.3.0)\n", - "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.3.1)\n", - "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (23.2.0)\n", - "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.4.1)\n", - "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (6.0.5)\n", - "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp>=3.9.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.9.4)\n", - "Requirement already satisfied: sniffio>=1.1 in /opt/homebrew/lib/python3.11/site-packages (from anyio<5,>=3.4.0->starlette<0.37.0,>=0.36.3->fastapi<1.0,>=0.100->inference) (1.3.0)\n", - "Requirement already satisfied: docutils<0.17,>=0.10 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.16)\n", - "Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.10.0)\n", - "Requirement already satisfied: rsa<4.8,>=3.1.2 in /opt/homebrew/lib/python3.11/site-packages (from awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (4.7.2)\n", - "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from botocore>=1.29.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (1.0.1)\n", - "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (3.20.2)\n", - "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (0.9.0)\n", - "Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from jinja2>=3.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.1.5)\n", - "Requirement already satisfied: tzdata>=2022.7 in /opt/homebrew/lib/python3.11/site-packages (from pandas>=1.3.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (2023.4)\n", - "Requirement already satisfied: wcwidth in /Users/nick/Library/Python/3.11/lib/python/site-packages (from PrettyTable>=2.0.0->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.2.13)\n", - "Requirement already satisfied: cffi>=1.12 in /opt/homebrew/lib/python3.11/site-packages (from cryptography->skypilot==0.4.1->inference-cli==0.9.9->inference) (1.16.0)\n", - "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (2023.12.1)\n", - "Requirement already satisfied: referencing>=0.28.4 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.33.0)\n", - "Requirement already satisfied: rpds-py>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from jsonschema->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.17.1)\n", - "Requirement already satisfied: time-machine>=2.6.0 in /opt/homebrew/lib/python3.11/site-packages (from pendulum->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.13.0)\n", - "Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from cffi>=1.12->cryptography->skypilot==0.4.1->inference-cli==0.9.9->inference) (2.21)\n", - "Requirement already satisfied: pyasn1>=0.1.3 in /opt/homebrew/lib/python3.11/site-packages (from rsa<4.8,>=3.1.2->awscli>=1.27.10->skypilot==0.4.1->inference-cli==0.9.9->inference) (0.5.1)\n", - "Requirement already satisfied: mypy-extensions>=0.3.0 in /opt/homebrew/lib/python3.11/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json>=0.6.0->inference-sdk>=0.9.8->inference-cli==0.9.9->inference) (1.0.0)\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "#!/bin/bash\n", - "!python -m venv venv\n", - "!source venv/bin/activate\n", - "!pip install inference supervision" + "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": [ - "Now that we've got our packages installed. Let's talk through the code to display bounding boxes on objects. " + "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", + "```python\n", + "model = get_roboflow_mode(model_id=\"rock-paper-scissors-sxsw/11\", api_key=\"roboflow_private_api_key\" )\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 inferenece then pass that data into a little helpfer function called `sv.Detections.from_inference()`. Lastly we print our detections so show we are in fact detecting people in the frame! \n", + "\n", + "## Adding the Bounding Boxes and Saving the results\n", + "\n", + "Now comes the fun part. Let's wrap up our code by utilizing `Annotators` and `VideoSink` to draw bounding boxes and save the resulting video. Take a peak at the final code example below. " ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/opt/homebrew/lib/python3.11/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py:65: UserWarning: Specified provider 'CUDAExecutionProvider' is not in available provider names.Available providers: 'CPUExecutionProvider'\n", - " warnings.warn(\n" - ] - } - ], + "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", - " model = get_roboflow_model(\"yolov8n-640\")\n", - " video_name = \"people-walking.mp4\"\n", - " frame_generator = sv.get_video_frames_generator(video_name)\n", - " video_info = sv.VideoInfo.from_video_path(video_name)\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 and video info object from supervision utilities.\n", + " frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)\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", + " # Iterate through frames yielded from the frame_generator.\n", " for frame in frame_generator:\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", + " # Apply bounding box to detections on a copy of the frame.\n", " annotated_frame = box_annotator.annotate(\n", " scene=frame.copy(), detections=detections\n", " )\n", + "\n", + " # Write the 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. 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. Prior to writing the frame, we annotate a copy of the frame utilizing `box_annotator.annotate()`. Let's take a look at the resulting video. " + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "vscode": { "languageId": "powershell" @@ -258,6 +179,13 @@ "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": { From a015fc176e5ca45c7e476ca37a8af4562e8b11d9 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 12:18:09 -0500 Subject: [PATCH 06/20] Fix object detection conflicts --- cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb diff --git a/cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb b/cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb new file mode 100644 index 00000000..e69de29b From 1ac497a3e28b6b8766daa1f469e852e7013fc56f Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 10:56:02 -0500 Subject: [PATCH 07/20] Finished bounding box annotation cookbook --- cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb diff --git a/cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb b/cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb new file mode 100644 index 00000000..e69de29b From efd06ac77dcfd796f329a31f2bb4bd0da6fe0786 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Feb 2024 17:22:00 +0000 Subject: [PATCH 08/20] =?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 --- ...to-display-bounding-boxes-on-a-video.ipynb | 2 +- ...pervision-assets-for-experimentation.ipynb | 204 +++++++++--------- 2 files changed, 103 insertions(+), 103 deletions(-) diff --git a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb index c5898d23..f24f248a 100644 --- a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb +++ b/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb @@ -86,7 +86,7 @@ "\n", " # Yield a single frame from the generator.\n", " frame = next(frame_generator)\n", - " \n", + "\n", " # Run inference on our frame.\n", " result = model.infer(frame)[0]\n", "\n", diff --git a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb index aa395984..37a2e6f3 100644 --- a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb +++ b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb @@ -1,104 +1,104 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# How To Use Supervision Assets for Experimentation\n", - "\n", - "When experimenting with interesting and useful features of the Supervision package, its 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", - "## Installation Methods\n", - "\n", - "To start utilizing assets let's install them with pip.\n", - "\n", - "### Install with pip\n", - "\n", - "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "#!/bin/bash\n", - "!python -m venv venv\n", - "!source venv/bin/activate" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!pip install \"supervision[assets]\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from supervision.assets import download_assets, VideoAssets\n", - "from IPython.display import Video\n", - "\n", - "path_to_video = download_assets(VideoAssets.SUBWAY)\n", - "Video(path_to_video)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble 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": [ + "# How To Use Supervision Assets for Experimentation\n", + "\n", + "When experimenting with interesting and useful features of the Supervision package, its 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", + "## Installation Methods\n", + "\n", + "To start utilizing assets let's install them with pip.\n", + "\n", + "### Install with pip\n", + "\n", + "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "#!/bin/bash\n", + "!python -m venv venv\n", + "!source venv/bin/activate" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "!pip install \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "from IPython.display import Video\n", + "\n", + "path_to_video = download_assets(VideoAssets.SUBWAY)\n", + "Video(path_to_video)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble 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 } From 0b7b8bb439ac2dce14b12f9bb7aec72e914528ca Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 14:13:14 -0500 Subject: [PATCH 09/20] Move assets cookbook to docs, Update theme/cookbooks --- ...pervision-assets-for-experimentation.ipynb | 104 ++++++++++++++++++ docs/theme/cookbooks.html | 3 + 2 files changed, 107 insertions(+) create mode 100644 docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb diff --git a/docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb b/docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb new file mode 100644 index 00000000..37a2e6f3 --- /dev/null +++ b/docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How To Use Supervision Assets for Experimentation\n", + "\n", + "When experimenting with interesting and useful features of the Supervision package, its 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", + "## Installation Methods\n", + "\n", + "To start utilizing assets let's install them with pip.\n", + "\n", + "### Install with pip\n", + "\n", + "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "#!/bin/bash\n", + "!python -m venv venv\n", + "!source venv/bin/activate" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "!pip install \"supervision[assets]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from supervision.assets import download_assets, VideoAssets\n", + "from IPython.display import Video\n", + "\n", + "path_to_video = download_assets(VideoAssets.SUBWAY)\n", + "Video(path_to_video)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble 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/theme/cookbooks.html b/docs/theme/cookbooks.html index 0e6f9688..a96b65cc 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -13,6 +13,9 @@

+
+

+
{% endblock %} From 48af41377fbc1329fd40388da845ca70d81e34ba Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 8 Feb 2024 20:32:37 +0100 Subject: [PATCH 10/20] move notebook inside the grid --- docs/theme/cookbooks.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/theme/cookbooks.html b/docs/theme/cookbooks.html index a96b65cc..3ebf1344 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -12,8 +12,6 @@

-
-

From e3adcbeec19a53a861928e8e7fd18de8d950d3f0 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 15:30:32 -0500 Subject: [PATCH 11/20] Move cookbooks to new webpage and configure html file --- ...to-detect-objects-in-a-specific-zone.ipynb | 0 ...pervision-assets-for-experimentation.ipynb | 104 ------------------ ...w-to-utilize-oriented-bounding-boxes.ipynb | 0 ...to-display-bounding-boxes-on-a-video.ipynb | 0 docs/theme/cookbooks.html | 3 +- 5 files changed, 2 insertions(+), 105 deletions(-) delete mode 100644 cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb delete mode 100644 cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb delete mode 100644 cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb rename {cookbooks => docs/notebooks}/how-to-display-bounding-boxes-on-a-video.ipynb (100%) diff --git a/cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb b/cookbooks/how-to-detect-objects-in-a-specific-zone.ipynb deleted file mode 100644 index e69de29b..00000000 diff --git a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb b/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb deleted file mode 100644 index 37a2e6f3..00000000 --- a/cookbooks/how-to-use-supervision-assets-for-experimentation.ipynb +++ /dev/null @@ -1,104 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# How To Use Supervision Assets for Experimentation\n", - "\n", - "When experimenting with interesting and useful features of the Supervision package, its 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", - "## Installation Methods\n", - "\n", - "To start utilizing assets let's install them with pip.\n", - "\n", - "### Install with pip\n", - "\n", - "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "#!/bin/bash\n", - "!python -m venv venv\n", - "!source venv/bin/activate" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!pip install \"supervision[assets]\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from supervision.assets import download_assets, VideoAssets\n", - "from IPython.display import Video\n", - "\n", - "path_to_video = download_assets(VideoAssets.SUBWAY)\n", - "Video(path_to_video)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble 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/cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb b/cookbooks/how-to-utilize-oriented-bounding-boxes.ipynb deleted file mode 100644 index e69de29b..00000000 diff --git a/cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb b/docs/notebooks/how-to-display-bounding-boxes-on-a-video.ipynb similarity index 100% rename from cookbooks/how-to-display-bounding-boxes-on-a-video.ipynb rename to docs/notebooks/how-to-display-bounding-boxes-on-a-video.ipynb diff --git a/docs/theme/cookbooks.html b/docs/theme/cookbooks.html index 3ebf1344..430a8cc8 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -12,7 +12,8 @@

-

+

+

From 7b4cf2f0f6158bd0d4210f3f3a565f191a5d1634 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 19:51:08 -0500 Subject: [PATCH 12/20] Reviewed supervision assets cookbook and renamed --- ...ntation.ipynb => supervision-assets.ipynb} | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) rename docs/notebooks/{how-to-use-supervision-assets-for-experimentation.ipynb => supervision-assets.ipynb} (74%) diff --git a/docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb b/docs/notebooks/supervision-assets.ipynb similarity index 74% rename from docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb rename to docs/notebooks/supervision-assets.ipynb index 37a2e6f3..e9f8256e 100644 --- a/docs/notebooks/how-to-use-supervision-assets-for-experimentation.ipynb +++ b/docs/notebooks/supervision-assets.ipynb @@ -4,17 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# How To Use Supervision Assets for Experimentation\n", + "# Experimenting with Supervision Assets\n", "\n", "When experimenting with interesting and useful features of the Supervision package, its 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", - "## Installation Methods\n", + "## Install with Pip\n", "\n", - "To start utilizing assets let's install them with pip.\n", - "\n", - "### Install with pip\n", - "\n", - "To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. " + "To start utilizing Supervision assets let's install them with pip. To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. A quick reminder that if you're running these commands in a terminal, first remove the \"!\". " ] }, { @@ -27,7 +23,6 @@ }, "outputs": [], "source": [ - "#!/bin/bash\n", "!python -m venv venv\n", "!source venv/bin/activate" ] @@ -49,14 +44,16 @@ }, "outputs": [], "source": [ - "!pip install \"supervision[assets]\"" + "!pip install -q \"supervision[assets]\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "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. Let's use the `Video` method from `Ipython.display` to show the video in our notebook." + "## 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. " ] }, { @@ -66,17 +63,16 @@ "outputs": [], "source": [ "from supervision.assets import download_assets, VideoAssets\n", - "from IPython.display import Video\n", "\n", - "path_to_video = download_assets(VideoAssets.SUBWAY)\n", - "Video(path_to_video)" + "# Download a video asset from the supervision package.\n", + "path_to_video = download_assets(VideoAssets.SUBWAY)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We're now equipted with a video asset from Supervision to run some experiements! For more information on availble video assets, visit the [Supervision API Reference](https://supervision.roboflow.com/latest/assets/#videoassets). Happy building!" + "We're now equipt with a video asset from Supervision to run some experiements on! For more information on available video assets, visit the [Supervision API Reference](https://supervision.roboflow.com/latest/assets/#videoassets). Happy building!" ] } ], From f340c49a70ba085869d5999429652df304a17e0a Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 20:30:57 -0500 Subject: [PATCH 13/20] Update bounding box cookbook name, Fix grammar and spelling --- ...-on-a-video.ipynb => bounding-boxes.ipynb} | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) rename docs/notebooks/{how-to-display-bounding-boxes-on-a-video.ipynb => bounding-boxes.ipynb} (76%) diff --git a/docs/notebooks/how-to-display-bounding-boxes-on-a-video.ipynb b/docs/notebooks/bounding-boxes.ipynb similarity index 76% rename from docs/notebooks/how-to-display-bounding-boxes-on-a-video.ipynb rename to docs/notebooks/bounding-boxes.ipynb index f24f248a..3ddb84df 100644 --- a/docs/notebooks/how-to-display-bounding-boxes-on-a-video.ipynb +++ b/docs/notebooks/bounding-boxes.ipynb @@ -6,11 +6,11 @@ "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 acomplish this task on a video. Let's dive in! \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 Deppendencies\n", + "## Installing Dependencies \n", "\n", - "In this cookbook we'll be utilizing two open source packages. Let's get those installed in a virtual environment with pip." + "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." ] }, { @@ -35,7 +35,7 @@ "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. We'll then disply the video with the `Video()` function imported from `IPython.display` to take a look at the video's contents." + "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`." ] }, { @@ -45,13 +45,9 @@ "outputs": [], "source": [ "from supervision.assets import download_assets, VideoAssets\n", - "from IPython.display import Video\n", "\n", - "# Download a supervision video of people walking to current directory\n", - "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)\n", - "\n", - "# Display the video in this notebook\n", - "Video(path_to_video)" + "# Download a supervision video asset \n", + "path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)" ] }, { @@ -101,19 +97,22 @@ "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", + "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", - "```python\n", - "model = get_roboflow_mode(model_id=\"rock-paper-scissors-sxsw/11\", api_key=\"roboflow_private_api_key\" )\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 inferenece then pass that data into a little helpfer function called `sv.Detections.from_inference()`. Lastly we print our detections so show we are in fact detecting people in the frame! \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", - "## Adding the Bounding Boxes and Saving the results\n", + "## Saving Bounding Boxes to the Video\n", "\n", - "Now comes the fun part. Let's wrap up our code by utilizing `Annotators` and `VideoSink` to draw bounding boxes and save the resulting video. Take a peak at the final code example below. " + "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. " ] }, { @@ -134,29 +133,34 @@ " # Load a yolov8 nano model from roboflow.\n", " model = get_roboflow_model(\"yolov8n-640\")\n", "\n", - " # Initalize the bounding box frame annotator\n", + " # Initalize the bounding box frame annotator.\n", " box_annotator = sv.BoundingBoxAnnotator()\n", "\n", - " # Create a frame generator and video info object from supervision utilities.\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", + " # 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 result into detections data model.\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(), detections=detections\n", + " scene=frame.copy(), \n", + " detections=detections\n", " )\n", "\n", - " # Write the frame to the video sink.\n", + " # Write the annotated frame to the video sink.\n", " sink.write_frame(frame=annotated_frame)" ] }, @@ -164,7 +168,7 @@ "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. 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. Prior to writing the frame, we annotate a copy of the frame utilizing `box_annotator.annotate()`. Let's take a look at the resulting video. " + " 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. " ] }, { @@ -204,7 +208,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.9.7" } }, "nbformat": 4, From aacdbe76f0dca387c48eed53538a280c76a15029 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Thu, 8 Feb 2024 22:55:12 -0500 Subject: [PATCH 14/20] Add html entries for cookbooks, add tracking cookbook --- docs/notebooks/tracking.ipynb | 140 ++++++++++++++++++++++++++++++++++ docs/theme/cookbooks.html | 5 +- 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 docs/notebooks/tracking.ipynb diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/tracking.ipynb new file mode 100644 index 00000000..69892669 --- /dev/null +++ b/docs/notebooks/tracking.ipynb @@ -0,0 +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 +} diff --git a/docs/theme/cookbooks.html b/docs/theme/cookbooks.html index 430a8cc8..7e7566a7 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -12,8 +12,9 @@

-

-

+

+

+

From 019c41bf1a87ef826722949d8f4c6c9580da572c 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 04:37:54 +0000 Subject: [PATCH 15/20] =?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/notebooks/bounding-boxes.ipynb | 428 ++++++++++++++-------------- docs/notebooks/tracking.ipynb | 276 +++++++++--------- 2 files changed, 352 insertions(+), 352 deletions(-) diff --git a/docs/notebooks/bounding-boxes.ipynb b/docs/notebooks/bounding-boxes.ipynb index 3ddb84df..e509b897 100644 --- a/docs/notebooks/bounding-boxes.ipynb +++ b/docs/notebooks/bounding-boxes.ipynb @@ -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 } diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/tracking.ipynb index 69892669..8d3ebb37 100644 --- a/docs/notebooks/tracking.ipynb +++ b/docs/notebooks/tracking.ipynb @@ -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 } From 9ca7d3eb751009838c0289b5ba3f3b8b121d2c9e Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Fri, 9 Feb 2024 09:21:14 -0500 Subject: [PATCH 16/20] Finish and fix grammar of tracking cookbook --- docs/notebooks/tracking.ipynb | 299 ++++++++++++++++++---------------- 1 file changed, 161 insertions(+), 138 deletions(-) diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/tracking.ipynb index 8d3ebb37..041aebf3 100644 --- a/docs/notebooks/tracking.ipynb +++ b/docs/notebooks/tracking.ipynb @@ -1,140 +1,163 @@ { - "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, or count objects in a frame. Some Supvervision [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": [ + "!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", + "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. Notice how even with 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! " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "!open output.mp4" + ] + } + ], + "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 } From 122ca82cad92560557d66b62baeb795633e0b452 Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Fri, 9 Feb 2024 09:27:07 -0500 Subject: [PATCH 17/20] Update cookbooks html to match new format -> no desc, cap tags --- docs/theme/cookbooks.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/theme/cookbooks.html b/docs/theme/cookbooks.html index 7e7566a7..03c0465c 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -11,10 +11,10 @@

Supervision Cookbooks

-

-

-

-

+

+

+

+

From 57c71094989172886b2556181dadf6f4e494753d 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 14:30:00 +0000 Subject: [PATCH 18/20] =?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/notebooks/tracking.ipynb | 322 +++++++++++++++++----------------- 1 file changed, 161 insertions(+), 161 deletions(-) diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/tracking.ipynb index 041aebf3..5433480c 100644 --- a/docs/notebooks/tracking.ipynb +++ b/docs/notebooks/tracking.ipynb @@ -1,163 +1,163 @@ { - "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, or count objects in a frame. Some Supvervision [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": [ - "!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", - "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. Notice how even with 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! " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!open output.mp4" - ] - } - ], - "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, or count objects in a frame. Some Supvervision [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": [ + "!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", + "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. Notice how even with 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! " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [], + "source": [ + "!open output.mp4" + ] + } + ], + "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 } From 84b54d31bfe99dd428df44ecf0d4b273cb3d5e2a Mon Sep 17 00:00:00 2001 From: Nick Herrig Date: Fri, 9 Feb 2024 10:34:40 -0500 Subject: [PATCH 19/20] Add colab notebook button, test in notebooks --- docs/notebooks/bounding-boxes.ipynb | 30 +++++++++--------------- docs/notebooks/supervision-assets.ipynb | 31 ++++++++++++------------- docs/notebooks/tracking.ipynb | 25 +++++++------------- 3 files changed, 34 insertions(+), 52 deletions(-) diff --git a/docs/notebooks/bounding-boxes.ipynb b/docs/notebooks/bounding-boxes.ipynb index e509b897..6649ca39 100644 --- a/docs/notebooks/bounding-boxes.ipynb +++ b/docs/notebooks/bounding-boxes.ipynb @@ -6,11 +6,18 @@ "source": [ "# How to Display Bounding Boxes on Objects in a Video.\n", "\n", + "---\n", + "\n", + "

\n", + " \"Colab\"\n", + "

\n", + "\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." + "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." ] }, { @@ -23,9 +30,7 @@ }, "outputs": [], "source": [ - "#!/bin/bash\n", - "!python -m venv venv\n", - "!source venv/bin/activate\n", + "\n", "!pip install -q inference \"supervision[assets]\"" ] }, @@ -112,7 +117,7 @@ "\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. " + "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. " ] }, { @@ -168,20 +173,7 @@ "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" + " 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! " ] }, { diff --git a/docs/notebooks/supervision-assets.ipynb b/docs/notebooks/supervision-assets.ipynb index e9f8256e..5efbfd07 100644 --- a/docs/notebooks/supervision-assets.ipynb +++ b/docs/notebooks/supervision-assets.ipynb @@ -6,32 +6,31 @@ "source": [ "# Experimenting with Supervision Assets\n", "\n", + "---\n", + "\n", + "

\n", + " \"Colab\"\n", + "

\n", + "\n", "When experimenting with interesting and useful features of the Supervision package, its 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 with Pip\n", "\n", - "To start utilizing Supervision assets let's install them with pip. To install the assets with pip, let's first create a virtual enviornment. It's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Let's create one below and source it. A quick reminder that if you're running these commands in a terminal, first remove the \"!\". " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!python -m venv venv\n", - "!source venv/bin/activate" + "If you're running this code outside a Jupyter notebook, it's generally a best practice to create a virtual environment so that your projects deppendencies are isolated. Run the following ing your command line of choice to create and source one.\n", + "\n", + "```shell\n", + "\n", + "python -m venv venv\n", + "source venv/bin/activate\n", + "\n", + "``` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package." + "Now that we've got a virtual enviornment installed and sourced, let's install the pip package with the assets \"extra\" package. Make sure to remove the \"!\" if running this in your command line." ] }, { diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/tracking.ipynb index 5433480c..35f42167 100644 --- a/docs/notebooks/tracking.ipynb +++ b/docs/notebooks/tracking.ipynb @@ -6,7 +6,13 @@ "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, or count objects in a frame. Some Supvervision [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", + "\n", + "

\n", + " \"Colab\"\n", + "

\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 Supvervision [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", @@ -25,8 +31,6 @@ }, "outputs": [], "source": [ - "!python -m venv venv\n", - "!source venv/bin/activate\n", "!pip install -q inference \"supervision[assets]\"" ] }, @@ -122,20 +126,7 @@ "source": [ "# View the Video\n", "\n", - "Let's take a look at our resulting video. Notice how even with 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! " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "shellscript" - } - }, - "outputs": [], - "source": [ - "!open output.mp4" + "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! " ] } ], From 115d4492dab7eee9394449f66b94812d8872976e Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Fri, 9 Feb 2024 17:36:13 +0100 Subject: [PATCH 20/20] changing colab names, card names, and card labels --- ...ing-boxes.ipynb => annotate-video-with-detections.ipynb} | 0 ...ision-assets.ipynb => download-supervision-assets.ipynb} | 0 docs/notebooks/{tracking.ipynb => object-tracking.ipynb} | 0 docs/theme/cookbooks.html | 6 +++--- 4 files changed, 3 insertions(+), 3 deletions(-) rename docs/notebooks/{bounding-boxes.ipynb => annotate-video-with-detections.ipynb} (100%) rename docs/notebooks/{supervision-assets.ipynb => download-supervision-assets.ipynb} (100%) rename docs/notebooks/{tracking.ipynb => object-tracking.ipynb} (100%) diff --git a/docs/notebooks/bounding-boxes.ipynb b/docs/notebooks/annotate-video-with-detections.ipynb similarity index 100% rename from docs/notebooks/bounding-boxes.ipynb rename to docs/notebooks/annotate-video-with-detections.ipynb diff --git a/docs/notebooks/supervision-assets.ipynb b/docs/notebooks/download-supervision-assets.ipynb similarity index 100% rename from docs/notebooks/supervision-assets.ipynb rename to docs/notebooks/download-supervision-assets.ipynb diff --git a/docs/notebooks/tracking.ipynb b/docs/notebooks/object-tracking.ipynb similarity index 100% rename from docs/notebooks/tracking.ipynb rename to docs/notebooks/object-tracking.ipynb diff --git a/docs/theme/cookbooks.html b/docs/theme/cookbooks.html index 2a11684e..068b1b22 100644 --- a/docs/theme/cookbooks.html +++ b/docs/theme/cookbooks.html @@ -12,9 +12,9 @@

-

-

-

+

+

+