Skip to content

Assets

Supervision offers an assets download utility that allows you to download image and video files that you can use in your demos.

supervision.assets.downloader.download_assets(asset_name: Assets | str) -> str

Download a specified asset if it doesn't already exist or is corrupted.

Parameters:

Name Type Description Default

asset_name

Assets | str

The name or type of the asset to be downloaded.

required

Returns:

Type Description
str

The filename of the downloaded asset.

Example
>>> from supervision.assets import download_assets, ImageAssets, VideoAssets
>>> download_assets(VideoAssets.VEHICLES)  # doctest: +SKIP
'vehicles.mp4'

>>> download_assets(ImageAssets.PEOPLE_WALKING)  # doctest: +SKIP
'people-walking.jpg'
Source code in src/supervision/assets/downloader.py
def download_assets(asset_name: Assets | str) -> str:
    """
    Download a specified asset if it doesn't already exist or is corrupted.

    Args:
        asset_name: The name or type of the asset to be downloaded.

    Returns:
        The filename of the downloaded asset.

    Example:
        ```pycon
        >>> from supervision.assets import download_assets, ImageAssets, VideoAssets
        >>> download_assets(VideoAssets.VEHICLES)  # doctest: +SKIP
        'vehicles.mp4'

        >>> download_assets(ImageAssets.PEOPLE_WALKING)  # doctest: +SKIP
        'people-walking.jpg'

        ```
    """

    filename = asset_name.filename if isinstance(asset_name, Assets) else asset_name

    if filename in MEDIA_ASSETS:
        if not Path(filename).exists():
            print(f"Downloading {filename} assets \n")
            response = get(
                MEDIA_ASSETS[filename][0], stream=True, allow_redirects=True, timeout=30
            )
            response.raise_for_status()

            file_size = int(response.headers.get("Content-Length", 0))
            folder_path = Path(filename).expanduser().resolve()
            folder_path.parent.mkdir(parents=True, exist_ok=True)

            with tqdm.wrapattr(
                response.raw, "read", total=file_size, desc="", colour="#a351fb"
            ) as raw_resp:
                with folder_path.open("wb") as file:
                    copyfileobj(raw_resp, file)
        else:
            if not is_md5_hash_matching(filename, MEDIA_ASSETS[filename][1]):
                print("File corrupted. Re-downloading... \n")
                os.remove(filename)
                return download_assets(filename)

            print(f"{filename} asset download complete. \n")
    else:
        valid_assets = ", ".join(filename for filename in MEDIA_ASSETS.keys())
        raise ValueError(
            f"Invalid asset. It should be one of the following: {valid_assets}."
        )

    return filename

supervision.assets.list.VideoAssets

Bases: Assets

Each member of this class represents a video asset. The value associated with each member has a filename and hash of the video. File names and links can be seen below.

Asset Video Filename Video URL
VEHICLES vehicles.mp4 Link
MILK_BOTTLING_PLANT milk-bottling-plant.mp4 Link
VEHICLES_2 vehicles-2.mp4 Link
GROCERY_STORE grocery-store.mp4 Link
SUBWAY subway.mp4 Link
MARKET_SQUARE market-square.mp4 Link
PEOPLE_WALKING people-walking.mp4 Link
BEACH beach-1.mp4 Link
BASKETBALL basketball-1.mp4 Link
SKIING skiing.mp4 Link
Source code in src/supervision/assets/list.py
class VideoAssets(Assets):
    """
    Each member of this class represents a video asset. The value associated with each
    member has a filename and hash of the video. File names and links can be seen below.

    | Asset                  | Video Filename             | Video URL                                                                             |
    |------------------------|----------------------------|---------------------------------------------------------------------------------------|
    | `VEHICLES`             | `vehicles.mp4`             | [Link](https://media.roboflow.com/supervision/video-examples/vehicles.mp4)            |
    | `MILK_BOTTLING_PLANT`  | `milk-bottling-plant.mp4`  | [Link](https://media.roboflow.com/supervision/video-examples/milk-bottling-plant.mp4) |
    | `VEHICLES_2`           | `vehicles-2.mp4`           | [Link](https://media.roboflow.com/supervision/video-examples/vehicles-2.mp4)          |
    | `GROCERY_STORE`        | `grocery-store.mp4`        | [Link](https://media.roboflow.com/supervision/video-examples/grocery-store.mp4)       |
    | `SUBWAY`               | `subway.mp4`               | [Link](https://media.roboflow.com/supervision/video-examples/subway.mp4)              |
    | `MARKET_SQUARE`        | `market-square.mp4`        | [Link](https://media.roboflow.com/supervision/video-examples/market-square.mp4)       |
    | `PEOPLE_WALKING`       | `people-walking.mp4`       | [Link](https://media.roboflow.com/supervision/video-examples/people-walking.mp4)      |
    | `BEACH`                | `beach-1.mp4`              | [Link](https://media.roboflow.com/supervision/video-examples/beach-1.mp4)             |
    | `BASKETBALL`           | `basketball-1.mp4`         | [Link](https://media.roboflow.com/supervision/video-examples/basketball-1.mp4)        |
    | `SKIING`               | `skiing.mp4`               | [Link](https://media.roboflow.com/supervision/video-examples/skiing.mp4)              |
    """  # noqa: E501 // docs

    VEHICLES = ("vehicles.mp4", "8155ff4e4de08cfa25f39de96483f918")
    MILK_BOTTLING_PLANT = (
        "milk-bottling-plant.mp4",
        "9e8fb6e883f842a38b3d34267290bdc7",
    )
    VEHICLES_2 = ("vehicles-2.mp4", "830af6fba21ffbf14867a7fea595937b")
    GROCERY_STORE = ("grocery-store.mp4", "48608fb4a8981f1c2469fa492adeec9c")
    SUBWAY = ("subway.mp4", "453475750691fb23c56a0cffef089194")
    MARKET_SQUARE = ("market-square.mp4", "859179bf4a21f80a8baabfdb2ed716dc")
    PEOPLE_WALKING = ("people-walking.mp4", "0574c053c8686c3f1dc0aa3743e45cb9")
    BEACH = ("beach-1.mp4", "4175d42fec4d450ed081523fd39e0cf8")
    BASKETBALL = ("basketball-1.mp4", "60d94a3c7c47d16f09d342b088012ecc")
    SKIING = ("skiing.mp4", "d30987cbab1bbc5934199cdd1b293119")

supervision.assets.list.ImageAssets

Bases: Assets

Each member of this enum represents a image asset. The value associated with each member is the filename of the image.

Asset Image Filename Video URL
PEOPLE_WALKING people-walking.jpg Link
SOCCER soccer.jpg Link
Source code in src/supervision/assets/list.py
class ImageAssets(Assets):
    """
    Each member of this enum represents a image asset. The value associated with each
    member is the filename of the image.

    | Asset              | Image Filename         | Video URL                                                                             |
    |--------------------|------------------------|---------------------------------------------------------------------------------------|
    | `PEOPLE_WALKING`   | `people-walking.jpg`   | [Link](https://media.roboflow.com/supervision/image-examples/people-walking.jpg)      |
    | `SOCCER`           | `soccer.jpg`           | [Link](https://media.roboflow.com/supervision/image-examples/soccer.jpg)              |

    """  # noqa: E501 // docs

    PEOPLE_WALKING = ("people-walking.jpg", "e6bda00b47f2908eeae7df86ef995dcd")
    SOCCER = ("soccer.jpg", "0f5a4b98abf3e3973faf9e9260a7d876")

Comments