Skip to content
Merged
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
5 changes: 4 additions & 1 deletion vllm/assets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from functools import lru_cache
from typing import Any, ClassVar, Literal

import cv2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Instead of moving the cv2 import inside each function, consider using the LazyLoader utility from vllm.utils. This is a cleaner and more idiomatic approach for lazy loading in this project, as seen with other modules. It avoids repeating import statements and keeps the function bodies cleaner, while still achieving the goal of lazy loading.

Suggested change
import cv2
from vllm.utils import LazyLoader
cv2 = LazyLoader("cv2", globals(), "cv2")

import numpy as np
import numpy.typing as npt
from huggingface_hub import hf_hub_download
Expand Down Expand Up @@ -43,6 +42,8 @@ def download_video_asset(filename: str) -> str:


def video_to_ndarrays(path: str, num_frames: int = -1) -> npt.NDArray:
import cv2

cap = cv2.VideoCapture(path)
if not cap.isOpened():
raise ValueError(f"Could not open video file {path}")
Expand Down Expand Up @@ -78,6 +79,8 @@ def video_to_pil_images_list(path: str, num_frames: int = -1) -> list[Image.Imag


def video_get_metadata(path: str, num_frames: int = -1) -> dict[str, Any]:
import cv2

Comment on lines +82 to +83
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

With the proposed LazyLoader at the module level, this local import is no longer necessary and can be removed.

cap = cv2.VideoCapture(path)
if not cap.isOpened():
raise ValueError(f"Could not open video file {path}")
Expand Down