-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for llava-next-video
* Inference with llava-hf/llava-next-video* (with bugs) * Add VideoPlugin. * Add example for llava-next-video
- Loading branch information
Showing
6 changed files
with
590 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from dataclasses import dataclass | ||
from functools import lru_cache | ||
from typing import Literal, List | ||
from huggingface_hub import hf_hub_download | ||
import cv2 | ||
import numpy as np | ||
|
||
from PIL import Image | ||
|
||
from .base import get_cache_dir | ||
|
||
|
||
@lru_cache | ||
def download_video_asset(filename: str) -> str: | ||
""" | ||
Download and open an image from huggingface | ||
repo: raushan-testing-hf/videos-test | ||
""" | ||
video_directory = get_cache_dir() / "video-eample-data" | ||
video_directory.mkdir(parents=True, exist_ok=True) | ||
|
||
video_path = video_directory / filename | ||
if not video_path.exists(): | ||
video_path = hf_hub_download( | ||
repo_id="raushan-testing-hf/videos-test", | ||
filename=filename, | ||
repo_type="dataset", | ||
cache_dir=video_directory, | ||
) | ||
return video_path | ||
|
||
|
||
def video_to_ndarrays_list( | ||
path: str, num_frames: int) -> List[np.ndarray]: | ||
cap = cv2.VideoCapture(path) | ||
if not cap.isOpened(): | ||
raise ValueError(f"Could not open video file {path}") | ||
|
||
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
|
||
frames = [] | ||
for i in range(total_frames): | ||
ret, frame = cap.read() | ||
if ret: | ||
frames.append(frame) | ||
|
||
cap.release() | ||
|
||
frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int) | ||
frames = [frames[i] for i in frame_indices if i < len(frames)] | ||
|
||
if len(frames) < num_frames: | ||
raise ValueError( | ||
f"Could not read enough frames from video file {path}" | ||
f" (expected {num_frames} frames, got {len(frames)})") | ||
|
||
return frames | ||
|
||
def video_to_pil_images_list( | ||
path: str, num_frames: int) -> List[Image.Image]: | ||
frames = video_to_ndarrays_list(path, num_frames) | ||
return [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | ||
for frame in frames] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class VideoAsset: | ||
name: Literal["sample_demo_1.mp4"] | ||
num_frames: int | ||
|
||
@property | ||
def pil_images(self) -> List[Image.Image]: | ||
video_path = download_video_asset(self.name) | ||
return video_to_pil_images_list(video_path, self.num_frames) | ||
|
||
@property | ||
def np_ndarrays(self) -> List[np.ndarray]: | ||
video_path = download_video_asset(self.name) | ||
return video_to_ndarrays_list(video_path, self.num_frames) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.