Views
No views yet
pip install git+https://github.com/Victorwz/LLaVA-Unified.git1from llava.conversation import conv_templates
2from llava.model.builder import load_pretrained_model
3from llava.mm_utils import tokenizer_image_token
4from PIL import Image
5import requests
6import cv2
7import torch
8
9# load model and processor
10device = "cuda" if torch.cuda.is_available() else "cpu"
11tokenizer, model, image_processor, context_len = load_pretrained_model("weizhiwang/LLaVA-Video-Llama-3.1-8B", None, "Video-Language-Model-Llama-3.1-8B", False, False, device=device)
12
13# prepare image input
14url = "https://github.com/PKU-YuanGroup/Video-LLaVA/raw/main/videollava/serve/examples/sample_demo_1.mp4"
15
16def read_video(video_url):
17 response = requests.get(url)
18 if response.status_code != 200:
19 print("Failed to download video")
20 exit()
21 else:
22 with open("tmp_video.mp4", 'wb') as f:
23 for chunk in response.iter_content(chunk_size=1024):
24 f.write(chunk)
25
26 video = cv2.VideoCapture("tmp_video.mp4")
27 video_frames = []
28 while video.isOpened():
29 success, frame = video.read()
30 if not success:
31 break
32 frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
33 pil_image = Image.fromarray(frame_rgb)
34 video_frames.append(pil_image)
35
36 video.release()
37 print(len(video_frames), "frames read.")
38 return video_frames
39
40video_frames = read_video(video_url=url)
41image_tensors = []
42
43# Please change the total number frames loaded based on your video length
44total_num_frames = 30
45samplng_interval = int(len(video_frames) / total_num_frames)
46for i in range(0, len(video_frames), samplng_interval):
47 image_tensor = image_processor.preprocess(video_frames[i], return_tensors='pt')['pixel_values'][0].half().cuda()
48 image_tensors.append(image_tensor)
49
50# prepare inputs for the model
51text = "\n".join(['<image>' for i in range(len(image_tensors))]) + '\n' + "Why is this video funny"
52conv = conv_templates["llama_3"].copy()
53conv.append_message(conv.roles[0], text)
54conv.append_message(conv.roles[1], None)
55prompt = conv.get_prompt()
56input_ids = tokenizer_image_token(prompt, tokenizer, return_tensors='pt').unsqueeze(0).cuda()
57
58# autoregressively generate text
59with torch.inference_mode():
60 output_ids = model.generate(
61 input_ids,
62 images=image_tensors,
63 do_sample=False,
64 max_new_tokens=512,
65 use_cache=True)
66
67outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
68print(outputs[0])The video is funny because the baby is wearing glasses while reading a book, which is an unusual and amusing sight. Babies typically do not wear glasses, and it is not common for them to read books at such a young age. The combination of the baby's actions and the fact that they are wearing glasses creates a humorous and endearing scene.1@misc{wang2024llavavideollama3,
2 title={LLaVA-Video-Llama-3: A Video Understanding Multimodal LLM based on Llama-3-8B LLM backbone},
3 author={Wang, Weizhi},
4 year={2024}
5}