-
Notifications
You must be signed in to change notification settings - Fork 302
Samples for VLM video input. #3050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
popovaan
wants to merge
24
commits into
openvinotoolkit:master
Choose a base branch
from
popovaan:video_to_text_sample
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+345
−13
Open
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ca857ff
Video to text python sample.
popovaan 3b3c69d
Sample test.
popovaan b4a84f7
Update samples/python/visual_language_chat/video_to_text_chat.py
popovaan 29d78c4
Added c++ sample.
popovaan 1a8944a
Attempt to add opencv build to ga workflow.
popovaan 1a0d25c
Revert "Attempt to add opencv build to ga workflow."
popovaan 4d070ab
Used FetchContent to add opencv.
popovaan a8fa911
Corrected test.
popovaan bdc6940
Convert path to string().
popovaan 735060e
Updated readme.
popovaan 5dfdcf7
Set 8 frames.
popovaan 43c76c9
Update samples/cpp/visual_language_chat/README.md
popovaan d77276f
Fixed opencv version, minor corrections.
popovaan cabd763
Added assert.
popovaan a1c1290
Merge branch 'master' into video_to_text_sample
popovaan 46e7d5d
Increase samples build timeout.
popovaan 9f4e6b9
Merge branch 'video_to_text_sample' of https://github.com/popovaan/op…
popovaan 5b6044d
Cmake corrected.
popovaan 54cebe6
Attempt to fix ci.
popovaan e8cb51e
Fix on win.
popovaan 58b8be4
Merge branch 'master' into video_to_text_sample
popovaan 6dac23e
Apply suggestions from code review
popovaan faffe59
Attempt too fix error.
popovaan 6e749ff
Attempt to fix cmake.
popovaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
samples/python/visual_language_chat/video_to_text_chat.py
Wovchena marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,100 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (C) 2024 Intel Corporation | ||
popovaan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| import argparse | ||
| import numpy as np | ||
| import cv2 | ||
| import openvino_genai | ||
| from openvino import Tensor | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def streamer(subword: str) -> bool: | ||
| ''' | ||
| Args: | ||
| subword: sub-word of the generated text. | ||
| Returns: Return flag corresponds whether generation should be stopped. | ||
| ''' | ||
| print(subword, end='', flush=True) | ||
|
|
||
| # No value is returned as in this example we don't want to stop the generation in this method. | ||
| # "return None" will be treated the same as "return openvino_genai.StreamingStatus.RUNNING". | ||
|
|
||
|
|
||
| def read_video(path: str, num_frames: int = 10) -> Tensor: | ||
| ''' | ||
| Args: | ||
| path: The path to the video. | ||
| Returns: the ov.Tensor containing the video. | ||
| ''' | ||
| cap = cv2.VideoCapture(path) | ||
|
|
||
| frames = [] | ||
|
|
||
| while cap.isOpened(): | ||
| ret, frame = cap.read() | ||
| if not ret: | ||
| break | ||
|
|
||
| frames.append(np.array(frame)) | ||
| cap.release() | ||
popovaan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| indices = np.arange(0, len(frames), len(frames) / num_frames).astype(int) | ||
| frames = [frames[i] for i in indices] | ||
popovaan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return Tensor(frames) | ||
|
|
||
|
|
||
| def read_videos(path: str) -> list[Tensor]: | ||
| entry = Path(path) | ||
| if entry.is_dir(): | ||
| return [read_video(str(file)) for file in sorted(entry.iterdir())] | ||
| return [read_video(path)] | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('model_dir', help="Path to the model directory") | ||
| parser.add_argument('video_dir', help="Path to a video file.") | ||
| parser.add_argument('device', nargs='?', default='CPU', help="Device to run the model on (default: CPU)") | ||
| args = parser.parse_args() | ||
|
|
||
| video = read_videos(args.video_dir) | ||
popovaan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # GPU and NPU can be used as well. | ||
| # Note: If NPU is selected, only the language model will be run on the NPU. | ||
| enable_compile_cache = dict() | ||
| if args.device == "GPU": | ||
| # Cache compiled models on disk for GPU to save time on the next run. | ||
| # It's not beneficial for CPU. | ||
| enable_compile_cache["CACHE_DIR"] = "vlm_cache" | ||
|
|
||
| pipe = openvino_genai.VLMPipeline(args.model_dir, args.device, **enable_compile_cache) | ||
|
|
||
| config = openvino_genai.GenerationConfig() | ||
| config.max_new_tokens = 100 | ||
|
|
||
| pipe.start_chat() | ||
| prompt = input('question:\n') | ||
| pipe.generate(prompt, videos=video, generation_config=config, streamer=streamer) | ||
|
|
||
| while True: | ||
| try: | ||
| prompt = input("\n----------\n" | ||
| "question:\n") | ||
| except EOFError: | ||
| break | ||
| pipe.generate(prompt, generation_config=config, streamer=streamer) | ||
| pipe.finish_chat() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or 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 hidden or 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,36 @@ | ||
| # Copyright (C) 2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import os | ||
| import pytest | ||
| import subprocess # nosec B404 | ||
| import sys | ||
|
|
||
| from conftest import SAMPLES_PY_DIR, SAMPLES_CPP_DIR, SAMPLES_C_DIR | ||
| from test_utils import run_sample | ||
|
|
||
| class TestVisualLanguageChat: | ||
| @pytest.mark.vlm | ||
| @pytest.mark.samples | ||
| @pytest.mark.parametrize( | ||
| "convert_model, download_test_content, questions", | ||
| [ | ||
| pytest.param("tiny-random-llava-next-video", "videos/sample_video.mp4", 'What is unusual on this video?\nGo on.') | ||
| ], | ||
| indirect=["convert_model", "download_test_content"], | ||
| ) | ||
| def test_sample_visual_language_chat(self, convert_model, download_test_content, questions): | ||
| # Test CPP sample | ||
| # TODO | ||
|
|
||
| # Test C sample | ||
| # TODO | ||
|
|
||
| # Test Python sample | ||
| py_script = os.path.join(SAMPLES_PY_DIR, "visual_language_chat/video_to_text_chat.py") | ||
| py_command = [sys.executable, py_script, convert_model, download_test_content] | ||
| py_result = run_sample(py_command, questions) | ||
|
|
||
| # Compare results | ||
| # assert py_result.stdout == cpp_result.stdout, f"Results should match" | ||
| # assert cpp_result.stdout == c_result.stdout, f"Results should match" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.