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": {