Skip to content

Assets

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

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

Parameters:

Name Type Description Default

asset_name

Union[VideoAssets, str]

The name or type of the asset to be downloaded.

required

Returns:

Name Type Description
str str

The filename of the downloaded asset.

Example
from supervision.assets import download_assets, VideoAssets

download_assets(VideoAssets.VEHICLES)
"vehicles.mp4"
Source code in supervision/assets/downloader.py
def download_assets(asset_name: Union[VideoAssets, str]) -> str:
    """
    Download a specified asset if it doesn't already exist or is corrupted.

    Parameters:
        asset_name (Union[VideoAssets, str]): The name or type of the asset to be
            downloaded.

    Returns:
        str: The filename of the downloaded asset.

    Example:
        ```python
        from supervision.assets import download_assets, VideoAssets

        download_assets(VideoAssets.VEHICLES)
        "vehicles.mp4"
        ```
    """

    filename = asset_name.value if isinstance(asset_name, VideoAssets) else asset_name

    if not Path(filename).exists() and filename in VIDEO_ASSETS:
        print(f"Downloading {filename} assets \n")
        response = get(VIDEO_ASSETS[filename][0], stream=True, allow_redirects=True)
        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)

    elif Path(filename).exists():
        if not is_md5_hash_matching(filename, VIDEO_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(asset.value for asset in VideoAssets)
        raise ValueError(
            f"Invalid asset. It should be one of the following: {valid_assets}."
        )

    return filename

Bases: Enum

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

Enum Member 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 supervision/assets/list.py
class VideoAssets(Enum):
    """
    Each member of this enum represents a video asset. The value associated with each
    member is the filename of the video.

    | Enum Member            | 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"
    MILK_BOTTLING_PLANT = "milk-bottling-plant.mp4"
    VEHICLES_2 = "vehicles-2.mp4"
    GROCERY_STORE = "grocery-store.mp4"
    SUBWAY = "subway.mp4"
    MARKET_SQUARE = "market-square.mp4"
    PEOPLE_WALKING = "people-walking.mp4"
    BEACH = "beach-1.mp4"
    BASKETBALL = "basketball-1.mp4"
    SKIING = "skiing.mp4"

    @classmethod
    def list(cls):
        return list(map(lambda c: c.value, cls))

Comments