Skip to content

Assets

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

install extra

To install the Supervision assets utility, you can use pip. This utility is available as an extra within the Supervision package.

pip install

pip install supervision[assets]

download_assets

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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

VideoAssets

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
Source code in supervision/assets/list.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)      |
    """

    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"

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