-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_processing.py
More file actions
607 lines (502 loc) · 21.8 KB
/
image_processing.py
File metadata and controls
607 lines (502 loc) · 21.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import copy
import math
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import comfy.clip_vision
import folder_paths
from comfy.utils import ProgressBar
# Constants from StoryMem
IMAGE_FACTOR = 28
VIDEO_MIN_PIXELS = 48 * IMAGE_FACTOR * IMAGE_FACTOR # 37,632
MIN_FRAME_SIMILARITY = 0.9
MAX_KEYFRAME_NUM = 3
ADAPTIVE_ALPHA = 0.01
HPSV3_QUALITY_THRESHOLD = 3.0
class FunPackClipVisionOutputCombine:
CATEGORY = "FunPack"
RETURN_TYPES = ("CLIP_VISION_OUTPUT",)
RETURN_NAMES = ("clip_vision_output",)
FUNCTION = "combine"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"clip_vision_output1": ("CLIP_VISION_OUTPUT",),
"method": (["mean", "median", "maximum", "minimum"], {"default": "mean"}),
},
"optional": {
"clip_vision_output2": ("CLIP_VISION_OUTPUT",),
"clip_vision_output3": ("CLIP_VISION_OUTPUT",),
"clip_vision_output4": ("CLIP_VISION_OUTPUT",),
},
}
def _keys(self, clip_output):
if isinstance(clip_output, dict):
return clip_output.keys()
return vars(clip_output).keys()
def _get(self, clip_output, key):
if isinstance(clip_output, dict):
return clip_output[key]
return getattr(clip_output, key)
def _set(self, clip_output, key, value):
if isinstance(clip_output, dict):
clip_output[key] = value
else:
setattr(clip_output, key, value)
def _copy_value(self, value):
if isinstance(value, torch.Tensor):
return value.clone()
return copy.deepcopy(value)
def _new_output(self, source):
if isinstance(source, dict):
return {}
return comfy.clip_vision.Output()
def _combine_tensors(self, key, tensors, method):
first = tensors[0]
first_shape = first.shape
for tensor in tensors[1:]:
if tensor.shape != first_shape:
raise ValueError(
f"Cannot combine CLIP Vision output field '{key}' with shapes "
f"{tuple(first_shape)} and {tuple(tensor.shape)}."
)
stacked = torch.stack(
[tensor.to(device=first.device, dtype=first.dtype) for tensor in tensors],
dim=0,
)
if method == "median":
return stacked.median(dim=0).values
if method == "maximum":
return stacked.max(dim=0).values
if method == "minimum":
return stacked.min(dim=0).values
return stacked.mean(dim=0)
@torch.no_grad()
def combine(self, clip_vision_output1, method, clip_vision_output2=None, clip_vision_output3=None, clip_vision_output4=None):
outputs = [
output
for output in (clip_vision_output1, clip_vision_output2, clip_vision_output3, clip_vision_output4)
if output is not None
]
combined = self._new_output(outputs[0])
for key in self._keys(outputs[0]):
values = [self._get(output, key) for output in outputs if key in self._keys(output)]
first_value = self._get(outputs[0], key)
if len(values) == len(outputs) and all(isinstance(value, torch.Tensor) for value in values):
self._set(combined, key, self._combine_tensors(key, values, method))
else:
self._set(combined, key, self._copy_value(first_value))
return (combined,)
class FunPackVideoStitch:
CATEGORY = "FunPack"
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("STITCHED",)
FUNCTION = "stitch"
INPUT_TYPES = lambda: {
"required": {
"blend_frames": ("INT", {"default": 8, "min": 0, "max": 64}),
"transition_type": (["linear", "ease_in", "ease_out", "ease_in_out", "cosine"], {"default": "linear"}),
},
"optional": {
"video1": ("IMAGE",),
"video2": ("IMAGE",),
"video3": ("IMAGE",),
"video4": ("IMAGE",),
"video5": ("IMAGE",),
"video6": ("IMAGE",),
"video7": ("IMAGE",),
"video8": ("IMAGE",),
}
}
def get_alpha(self, step_index, blend_frames, transition_type):
if blend_frames == 1:
return 0.5
t = step_index / (blend_frames - 1)
if transition_type == "ease_in":
return t * t
if transition_type == "ease_out":
return 1 - ((1 - t) * (1 - t))
if transition_type == "ease_in_out":
return t * t * (3 - (2 * t))
if transition_type == "cosine":
return 0.5 - (0.5 * math.cos(math.pi * t))
return t
def blend_batches(self, batch_a, batch_b, blend_frames, transition_type):
blended = []
for i in range(blend_frames):
alpha = self.get_alpha(i, blend_frames, transition_type)
blended_frame = (1 - alpha) * batch_a[-blend_frames + i] + alpha * batch_b[i]
blended.append(blended_frame.unsqueeze(0))
return torch.cat(blended, dim=0)
def stitch(self, blend_frames, transition_type, video1=None, video2=None, video3=None, video4=None, video5=None, video6=None, video7=None, video8=None):
input_videos = [video1, video2, video3, video4, video5, video6, video7, video8]
video_batches = [v for v in input_videos if v is not None]
if len(video_batches) < 2:
raise ValueError("VideoStitch requires at least 2 connected video inputs.")
if blend_frames == 0:
return (torch.cat(video_batches, dim=0),)
output_frames = []
for i in range(len(video_batches) - 1):
batch_a = video_batches[i]
batch_b = video_batches[i + 1]
if batch_a.shape[0] < blend_frames or batch_b.shape[0] < blend_frames:
raise ValueError(f"Each video batch must have at least {blend_frames} frames.")
stable_a = batch_a[:-blend_frames]
stable_b = batch_b[blend_frames:]
transition = self.blend_batches(batch_a, batch_b, blend_frames, transition_type)
if i == 0:
output_frames.append(stable_a)
output_frames.append(transition)
output_frames.append(stable_b if i == len(video_batches) - 2 else batch_b[blend_frames:-blend_frames])
final_video = torch.cat(output_frames, dim=0)
return (final_video,)
class FunPackContinueVideo:
CATEGORY = "FunPack"
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("CONTINUED",)
FUNCTION = "continue_video"
INPUT_TYPES = lambda: {
"required": {
"images": ("IMAGE",),
"frame_count": ("INT", {"default": 1, "min": 1, "max": 9999}),
}
}
def continue_video(self, images, frame_count):
total_frames = images.shape[0]
if frame_count > total_frames:
raise ValueError(f"Cannot extract {frame_count} frames from video with only {total_frames} frames.")
continued = images[-frame_count:]
return (continued,)
class FunPackStoryMemKeyframeExtractor:
"""
Extracts keyframes from video frames using:
1. HPSv3 for quality assessment (optional)
2. CLIP Vision for frame similarity
3. Adaptive threshold to limit keyframe count
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"frames": ("IMAGE",), # ComfyUI IMAGE format [B, H, W, C]
"clip_vision": (folder_paths.get_filename_list("clip_vision"),),
"max_keyframes": ("INT", {
"default": 3,
"min": 1,
"max": 20,
"step": 1,
"tooltip": "Maximum number of keyframes to extract"
}),
"similarity_threshold": ("FLOAT", {
"default": 0.9,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": "CLIP similarity threshold (lower = more keyframes)"
}),
"use_quality_filter": ("BOOLEAN", {
"default": False,
"tooltip": "Use HPSv3 to filter low-quality frames (requires hpsv3 package)"
}),
"quality_threshold": ("FLOAT", {
"default": 3.0,
"min": 0.0,
"max": 10.0,
"step": 0.1,
"tooltip": "HPSv3 quality threshold (higher = stricter)"
}),
},
"optional": {
"memory_frames": ("IMAGE", {
"tooltip": "Previous keyframes to compare against (avoid duplicates)"
}),
}
}
RETURN_TYPES = ("IMAGE", "INT",)
RETURN_NAMES = ("keyframes", "keyframe_count",)
FUNCTION = "extract_keyframes"
CATEGORY = "FunPack"
DESCRIPTION = "Extract keyframes using CLIP similarity + HPSv3 quality (StoryMem algorithm)"
def __init__(self):
self.quality_model = None
def load_clip_model(self, clip_vision_name):
"""Load CLIP Vision model from ComfyUI models/clip_vision folder"""
clip_path = folder_paths.get_full_path("clip_vision", clip_vision_name)
clip_vision = comfy.clip_vision.load(clip_path)
return clip_vision
def load_quality_model(self):
"""Load HPSv3 quality assessment model"""
if self.quality_model is not None:
return
try:
from hpsv3 import HPSv3RewardInferencer
device = "cuda" if torch.cuda.is_available() else "cpu"
self.quality_model = HPSv3RewardInferencer(device=device)
except ImportError:
print("WARNING: HPSv3 not installed. Install with: pip install hpsv3")
print("Quality filtering will be disabled.")
self.quality_model = None
def smart_resize(self, height: int, width: int) -> tuple:
"""Resize frame to efficient size for processing"""
factor = IMAGE_FACTOR
min_pixels = VIDEO_MIN_PIXELS
max_pixels = 256 * IMAGE_FACTOR * IMAGE_FACTOR
h_bar = max(factor, round(height / factor) * factor)
w_bar = max(factor, round(width / factor) * factor)
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = math.floor(height / beta / factor) * factor
w_bar = math.floor(width / beta / factor) * factor
elif h_bar * w_bar < min_pixels:
beta = math.sqrt(min_pixels / (height * width))
h_bar = math.ceil(height * beta / factor) * factor
w_bar = math.ceil(width * beta / factor) * factor
return max(h_bar, factor), max(w_bar, factor)
def clip_preprocess(self, frame_chw: torch.Tensor, clip_vision) -> torch.Tensor:
"""Preprocess frame for CLIP Vision model"""
# ComfyUI CLIP Vision expects [B, H, W, C] format in range [0, 1]
# Convert from [C, H, W] to [1, H, W, C]
frame = frame_chw.permute(1, 2, 0).unsqueeze(0)
# Ensure [0, 1] range
if not torch.is_floating_point(frame):
frame = frame.float()
if frame.max() > 1.5:
frame = frame / 255.0
frame = frame.clamp(0.0, 1.0)
return frame
@torch.no_grad()
def get_clip_similarity(self, frame1: torch.Tensor, frame2: torch.Tensor, clip_vision) -> float:
# Preprocess frames to [1, H, W, C] format
x1 = self.clip_preprocess(frame1, clip_vision)
x2 = self.clip_preprocess(frame2, clip_vision)
# Get CLIP Vision embeddings
z1_raw = clip_vision.encode_image(x1)
z2_raw = clip_vision.encode_image(x2)
# Extract the actual embedding tensor from various possible return formats
def extract_embedding(output):
# Case 1: Direct tensor (older/basic models)
if isinstance(output, torch.Tensor):
return output
# Case 2: ComfyUI's custom Output wrapper (common with projection models)
if isinstance(output, comfy.clip_vision.Output): # Import at top if needed: import comfy.clip_vision
if hasattr(output, 'image_embeds'):
return output.image_embeds
elif hasattr(output, 'pooled_output'):
return output.pooled_output
# Fallback: treat like dict
try:
return output['image_embeds']
except:
pass
# Case 3: Dictionary (some models)
if isinstance(output, dict):
if 'image_embeds' in output:
return output['image_embeds']
if 'pooled_output' in output:
return output['pooled_output']
if 'last_hidden_state' in output:
return output['last_hidden_state'][:, 0] # CLS token if sequence
# Fallback: first tensor value
for v in output.values():
if isinstance(v, torch.Tensor) and v.ndim >= 2:
return v
# Case 4: Tuple (rare here, but safe)
if isinstance(output, (tuple, list)) and len(output) == 1:
return extract_embedding(output[0])
raise TypeError(f"Unexpected output from encode_image: {type(output)}. "
"Supported: tensor, dict, or comfy.clip_vision.Output with 'image_embeds'.")
z1 = extract_embedding(z1_raw)
z2 = extract_embedding(z2_raw)
# Final check
if not (isinstance(z1, torch.Tensor) and isinstance(z2, torch.Tensor)):
raise RuntimeError(f"Failed to extract tensor embeddings: {type(z1)}, {type(z2)}")
# Normalize and compute cosine similarity
z1 = F.normalize(z1, dim=-1)
z2 = F.normalize(z2, dim=-1)
similarity = (z1 * z2).sum(dim=-1).item()
return similarity
def is_low_quality(self, frame: torch.Tensor, threshold: float) -> bool:
"""Check if frame quality is below threshold using HPSv3"""
if self.quality_model is None:
return False # Skip quality check if model not available
# Convert to PIL Image
frame_np = frame.permute(1, 2, 0).cpu().numpy()
frame_np = (frame_np * 255).astype(np.uint8).clip(0, 255)
pil_image = Image.fromarray(frame_np)
# Get quality score
try:
rewards = self.quality_model.reward(image_paths=[pil_image], prompts=[""])
score = rewards[0][0].item()
return score < threshold
except Exception as e:
print(f"Quality check failed: {e}")
return False
def extract_keyframe_indices(self, frames: torch.Tensor, threshold: float,
quality_threshold: float, use_quality: bool, clip_vision) -> list:
"""
Extract keyframe indices using CLIP similarity and quality filtering
Args:
frames: [N, C, H, W] tensor
threshold: CLIP similarity threshold
quality_threshold: HPSv3 quality threshold
use_quality: Whether to use quality filtering
clip_vision: ComfyUI CLIP Vision model
Returns:
List of keyframe indices
"""
num_frames, _, height, width = frames.shape
# Resize frames for efficient processing
resized_h, resized_w = self.smart_resize(height, width)
resized_frames = F.interpolate(
frames,
size=(resized_h, resized_w),
mode="bilinear",
align_corners=False
).float()
# Load quality model if needed
if use_quality:
self.load_quality_model()
# Find first high-quality frame
first_idx = 0
if use_quality and self.quality_model is not None:
while first_idx < num_frames:
if not self.is_low_quality(resized_frames[first_idx], quality_threshold):
break
first_idx += 1
if first_idx >= num_frames:
return [] # No high-quality frames found
# Initialize keyframes
keyframe_indices = [first_idx]
last_keyframe = resized_frames[first_idx]
# Iterate through remaining frames
pbar = ProgressBar(num_frames - first_idx - 1)
for i in range(first_idx + 1, num_frames):
current_frame = resized_frames[i]
# Calculate similarity with last keyframe
similarity = self.get_clip_similarity(last_keyframe, current_frame, clip_vision)
# Check if frame is different enough and high quality
is_different = similarity < threshold
is_quality = True
if use_quality and self.quality_model is not None:
is_quality = not self.is_low_quality(current_frame, quality_threshold)
if is_different and is_quality:
keyframe_indices.append(i)
last_keyframe = current_frame
pbar.update(1)
return keyframe_indices
def check_memory_duplicates(self, keyframes: torch.Tensor,
memory_frames: torch.Tensor,
clip_vision,
threshold: float = 0.9) -> list:
"""
Filter out keyframes that are too similar to memory frames
Returns:
List of boolean flags (True = keep, False = duplicate)
"""
keep_flags = []
for keyframe in keyframes:
is_duplicate = False
for memory_frame in memory_frames:
similarity = self.get_clip_similarity(keyframe, memory_frame, clip_vision)
if similarity > threshold:
is_duplicate = True
break
keep_flags.append(not is_duplicate)
return keep_flags
def extract_keyframes(self, frames, clip_vision, max_keyframes, similarity_threshold,
use_quality_filter, quality_threshold, memory_frames=None):
"""
Main extraction function
Args:
frames: ComfyUI IMAGE format [B, H, W, C] in range [0, 1]
clip_vision: CLIP Vision model name from dropdown
max_keyframes: Maximum number of keyframes
similarity_threshold: Initial CLIP similarity threshold
use_quality_filter: Whether to use HPSv3 filtering
quality_threshold: HPSv3 threshold
memory_frames: Optional previous keyframes to avoid duplicates
Returns:
(keyframes, keyframe_count)
"""
# Load CLIP Vision model from ComfyUI models folder
clip_vision_model = self.load_clip_model(clip_vision)
# Convert ComfyUI format [B, H, W, C] to PyTorch [B, C, H, W]
frames_tensor = frames.permute(0, 3, 1, 2).contiguous()
# Adaptive threshold loop
threshold = similarity_threshold
while True:
keyframe_indices = self.extract_keyframe_indices(
frames_tensor,
threshold,
quality_threshold,
use_quality_filter,
clip_vision_model
)
# Check if we have too many keyframes
if len(keyframe_indices) <= max_keyframes:
break
# Increase threshold to get fewer keyframes
threshold -= ADAPTIVE_ALPHA
# Safety check
if threshold < 0.5:
# Take first N keyframes
keyframe_indices = keyframe_indices[:max_keyframes]
break
print(f"Extracted {len(keyframe_indices)} keyframes at threshold {threshold:.3f}")
# Extract keyframes
if len(keyframe_indices) == 0:
# Return first frame as fallback
keyframes_out = frames[:1]
return (keyframes_out, 1)
keyframes_tensor = frames_tensor[keyframe_indices]
# Check against memory frames to avoid duplicates
if memory_frames is not None:
memory_tensor = memory_frames.permute(0, 3, 1, 2).contiguous()
keep_flags = self.check_memory_duplicates(
keyframes_tensor,
memory_tensor,
clip_vision_model,
threshold=MIN_FRAME_SIMILARITY
)
# Filter keyframes
kept_indices = [i for i, keep in enumerate(keep_flags) if keep]
if len(kept_indices) > 0:
keyframes_tensor = keyframes_tensor[kept_indices]
else:
# Keep at least one keyframe
keyframes_tensor = keyframes_tensor[:1]
# Convert back to ComfyUI format [B, H, W, C]
keyframes_out = keyframes_tensor.permute(0, 2, 3, 1).contiguous()
return (keyframes_out, keyframes_out.shape[0])
class FunPackStoryMemLastFrameExtractor:
"""Extract last frame and last N frames for MI2V/MM2V continuity"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"frames": ("IMAGE",),
"n_frames": ("INT", {
"default": 5,
"min": 1,
"max": 20,
"step": 1,
"tooltip": "Number of frames to extract from end (for MM2V)"
}),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE",)
RETURN_NAMES = ("last_frame", "motion_frames",)
FUNCTION = "extract"
CATEGORY = "FunPack"
DESCRIPTION = "Extract last frame and last N frames for shot continuity (MI2V/MM2V)"
def extract(self, frames, n_frames):
"""
Extract last frame and last N frames
Returns:
(last_frame [1, H, W, C], motion_frames [N, H, W, C])
"""
last_frame = frames[-1:]
motion_frames = frames[-n_frames:]
return (last_frame, motion_frames)