-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision_analyzer.py
More file actions
261 lines (216 loc) · 8.47 KB
/
vision_analyzer.py
File metadata and controls
261 lines (216 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable, List, Optional, Iterable, Sequence, Any
import io
from PIL import Image
import torch
import requests
from transformers import (
BlipForConditionalGeneration,
BlipProcessor,
Qwen2VLForConditionalGeneration,
AutoProcessor,
)
@dataclass
class Caption:
timestamp_s: float
text: str
class SimpleVisionCaptioner:
"""Lightweight image captioning using smaller BLIP model.
Uses Salesforce BLIP (not BLIP-2) for fast, lightweight image captioning.
Much smaller than BLIP-2 and won't cause system hangs.
"""
def __init__(
self,
model_name: str = "Salesforce/blip-image-captioning-base",
device: Optional[str] = None,
max_new_tokens: int = 64,
prompt: str = "Describe what's happening in this image in detail.",
batch_size: int = 4,
) -> None:
self.model_name = model_name
self.max_new_tokens = max_new_tokens
self.prompt = prompt
self.batch_size = max(1, int(batch_size))
if device is None:
if torch.cuda.is_available():
device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
self.device = device
print(f"Loading model {model_name} on {device}...")
# Use smaller BLIP model instead of BLIP-2
self.processor = BlipProcessor.from_pretrained(model_name)
self.model = BlipForConditionalGeneration.from_pretrained(
model_name, torch_dtype=torch.float16 if device in ("cuda", "mps") else torch.float32
)
self.model.to(device)
self.model.eval()
print("✅ Model loaded successfully!")
@torch.inference_mode()
def caption_image(self, image: Image.Image) -> str:
inputs = self.processor(image, return_tensors="pt").to(self.device)
generated_ids = self.model.generate(
**inputs,
max_new_tokens=self.max_new_tokens,
)
text = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return text.strip()
@torch.inference_mode()
def caption_frames(
self,
frames: Sequence[Image.Image],
progress_callback: Optional[Callable[[int, int], None]] = None,
) -> List[str]:
captions: List[str] = []
total = len(frames)
processed = 0
# Batch generation for performance
for start in range(0, total, self.batch_size):
batch = frames[start : start + self.batch_size]
inputs = self.processor(list(batch), return_tensors="pt", padding=True).to(self.device)
generated_ids = self.model.generate(
**inputs,
max_new_tokens=self.max_new_tokens,
)
texts = self.processor.batch_decode(generated_ids, skip_special_tokens=True)
captions.extend([t.strip() for t in texts])
processed += len(batch)
if progress_callback is not None:
progress_callback(processed, total)
return captions
class QwenVisionCaptioner:
"""Image captioning using Qwen2-VL vision-language model.
Uses Qwen2-VL-2B-Instruct for high-quality image understanding and captioning.
Supports dynamic resolution and advanced visual reasoning.
"""
def __init__(
self,
model_name: str = "Qwen/Qwen2-VL-2B-Instruct",
device: Optional[str] = None,
max_new_tokens: int = 128,
prompt: str = "Describe what's happening in this image in detail.",
batch_size: int = 1,
) -> None:
self.model_name = model_name
self.max_new_tokens = max_new_tokens
self.prompt = prompt
self.batch_size = max(1, int(batch_size))
if device is None:
if torch.cuda.is_available():
device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
self.device = device
print(f"Loading Qwen2-VL model {model_name} on {device}...")
# Load Qwen2-VL model and processor
self.processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
# Use appropriate dtype based on device
if device in ("cuda", "mps"):
torch_dtype = torch.float16
else:
torch_dtype = torch.float32
self.model = Qwen2VLForConditionalGeneration.from_pretrained(
model_name,
torch_dtype=torch_dtype,
device_map=device if device != "mps" else None,
trust_remote_code=True,
)
# For MPS, manually move to device
if device == "mps":
self.model.to(device)
self.model.eval()
print("✅ Qwen2-VL model loaded successfully!")
def _image_to_messages(self, image: Image.Image, prompt: str) -> List[dict]:
"""Convert PIL image to Qwen's message format."""
# Save image to bytes buffer
img_buffer = io.BytesIO()
image.save(img_buffer, format="PNG")
img_buffer.seek(0)
# Create message in Qwen format
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": prompt},
],
}
]
return messages
@torch.inference_mode()
def caption_image(self, image: Image.Image) -> str:
"""Generate caption for a single image."""
try:
# Import qwen_vl_utils here to avoid import errors if not installed
from qwen_vl_utils import process_vision_info
except ImportError:
raise ImportError(
"qwen-vl-utils is required for Qwen2-VL. Install with: pip install qwen-vl-utils"
)
messages = self._image_to_messages(image, self.prompt)
# Apply chat template
text = self.processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
# Process vision information
image_inputs, video_inputs = process_vision_info(messages)
# Prepare inputs
inputs = self.processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
# Move to device
inputs = inputs.to(self.device)
# Generate
generated_ids = self.model.generate(
**inputs,
max_new_tokens=self.max_new_tokens,
)
# Trim input tokens from output
generated_ids_trimmed = [
out_ids[len(in_ids):]
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
# Decode
output_text = self.processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
return output_text.strip()
@torch.inference_mode()
def caption_frames(
self,
frames: Sequence[Image.Image],
progress_callback: Optional[Callable[[int, int], None]] = None,
) -> List[str]:
"""Generate captions for multiple frames.
Note: Qwen2-VL processes images one at a time due to its architecture.
Batching is less efficient for vision-language models compared to pure captioning models.
"""
captions: List[str] = []
total = len(frames)
for idx, frame in enumerate(frames):
caption = self.caption_image(frame)
captions.append(caption)
if progress_callback is not None:
progress_callback(idx + 1, total)
return captions
def make_captions_for_timestamps(
captioner: Any,
images_with_ts: Iterable[tuple[float, Image.Image]],
progress_callback: Optional[Callable[[int, int], None]] = None,
) -> List[Caption]:
pairs = list(images_with_ts)
timestamps = [ts for ts, _ in pairs]
images = [img for _, img in pairs]
texts = captioner.caption_frames(images, progress_callback=progress_callback)
return [Caption(timestamp_s=ts, text=txt) for ts, txt in zip(timestamps, texts)]