Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 297 additions & 0 deletions Video_Demo_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fb864ed3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"\n",
"import supervision as sv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "94cbdffa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Video gemist.mp4 : VideoInfo(width=848, height=480, fps=15, precise_fps=14.955430835259161, total_frames=151) : Backend: opencv>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"video = sv.Video(\"gemist.mp4\")\n",
"video"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3cd5bbd6",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 151/151 [00:00<00:00, 1929.48it/s]\n"
]
}
],
"source": [
"video = sv.Video(\"gemist.mp4\")\n",
"for frame in tqdm(video):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5ca4d68c",
"metadata": {},
"outputs": [],
"source": [
"for frame in sv.Video(\"gemist.mp4\").frames(\n",
" stride=5, start=100, end=500, resolution_wh=(1280, 720)\n",
"):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "90463868",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Saving video: 151it [00:00, 540.72it/s]\n"
]
}
],
"source": [
"import cv2\n",
"\n",
"\n",
"def blur(frame, i):\n",
" return cv2.GaussianBlur(frame, (11, 11), 0)\n",
"\n",
"\n",
"sv.Video(\"gemist.mp4\").save(\"blurred.mp4\", callback=blur, show_progress=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "47382afa",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Saving video: 151it [00:00, 539.70it/s]\n"
]
}
],
"source": [
"sv.Video(\"gemist.mp4\").save(\n",
" \"timelapse.mp4\", fps=60, callback=lambda f, i: f, show_progress=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "11bbe111",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Saving video: 151it [00:00, 625.05it/s]\n"
]
}
],
"source": [
"from supervision import Video, VideoInfo\n",
"\n",
"# Need more elaboration on this\n",
"\n",
"source = Video(\"gemist.mp4\")\n",
"target_info = VideoInfo(width=200, height=600, fps=200)\n",
"\n",
"source.save(\"resized.mp4\", info=target_info, show_progress=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f60323ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Video gemist.mp4 : VideoInfo(width=848, height=480, fps=15, precise_fps=14.955430835259161, total_frames=151) : Backend: pyav>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import supervision as sv\n",
"\n",
"video = sv.Video(\"gemist.mp4\", backend=\"pyav\")\n",
"video"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "de97b6c3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 151/151 [00:00<00:00, 1969.45it/s]\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"\n",
"import supervision as sv\n",
"\n",
"video = sv.Video(\"gemist.mp4\", backend=\"pyav\")\n",
"for frame in tqdm(video):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "726ffd1a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"13it [00:00, 318.78it/s]\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"\n",
"import supervision as sv\n",
"\n",
"for frame in tqdm(\n",
" sv.Video(\"gemist.mp4\", backend=\"pyav\").frames(\n",
" stride=5, start=100, end=500, resolution_wh=(1280, 720)\n",
" )\n",
"):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0da30fa8",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Saving video: 151it [00:00, 413.05it/s]\n"
]
}
],
"source": [
"def blur(frame, i):\n",
" return cv2.GaussianBlur(frame, (11, 11), 0)\n",
"\n",
"\n",
"sv.Video(\"gemist.mp4\", backend=\"pyav\").save(\n",
" \"blurred_av.mp4\", callback=blur, show_progress=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ef5fb50e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Saving video: 151it [00:00, 413.50it/s]\n"
]
}
],
"source": [
"sv.Video(\"gemist.mp4\", backend=\"pyav\").save(\n",
" \"timelapse.mp4\", fps=60, callback=lambda f, i: f, show_progress=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8db8606",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ build = [
"wheel>=0.40,<0.46",
"build>=0.10,<1.3"
]
video = [
"av>=15.0.0"
]

[tool.bandit]
target = ["test", "supervision"]
Expand Down
3 changes: 2 additions & 1 deletion supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@
from supervision.utils.notebook import plot_image, plot_images_grid
from supervision.utils.video import (
FPSMonitor,
VideoInfo,
VideoSink,
get_video_frames_generator,
process_video,
)
from supervision.video.core import Video, VideoInfo

__all__ = [
"LMM",
Expand Down Expand Up @@ -193,6 +193,7 @@
"TriangleAnnotator",
"VertexAnnotator",
"VertexLabelAnnotator",
"Video",
"VideoInfo",
"VideoSink",
"approximate_polygon",
Expand Down
14 changes: 14 additions & 0 deletions supervision/utils/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np
from tqdm.auto import tqdm

from supervision.utils.internal import deprecated


@dataclass
class VideoInfo:
Expand Down Expand Up @@ -60,6 +62,10 @@ def resolution_wh(self) -> tuple[int, int]:
return self.width, self.height


@deprecated(
"VideoSink is deprated and will be removed in a future version. "
"Use `sv.Video(path).save(...)` instead."
)
class VideoSink:
"""
Context manager that saves video frames to a file using OpenCV.
Expand Down Expand Up @@ -141,6 +147,10 @@ def _validate_and_setup_video(
return video, start, end


@deprecated(
"get_video_frames_generator is deprated and will be removed in a future version. "
"Use `sv.Video(source).frames(...)` instead."
)
def get_video_frames_generator(
source_path: str,
stride: int = 1,
Expand Down Expand Up @@ -192,6 +202,10 @@ def get_video_frames_generator(
video.release()


@deprecated(
"get_video_frames_generator is deprated and will be removed in a future version."
"Use `sv.Video(source).save(target, callback=...)` instead."
)
def process_video(
source_path: str,
target_path: str,
Expand Down
4 changes: 4 additions & 0 deletions supervision/video/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .core import Video
from .utils import VideoInfo

__all__ = ["Video", "VideoInfo"]
7 changes: 7 additions & 0 deletions supervision/video/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BACKENDS = {
"opencv": "supervision.video.backends.opencv",
"pyav": "supervision.video.backends.pyav",
}


__all__ = ["BACKENDS"]
Loading