-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathimage_helpers.py
More file actions
1465 lines (1278 loc) · 59.4 KB
/
Copy pathimage_helpers.py
File metadata and controls
1465 lines (1278 loc) · 59.4 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import io
import math
import os
import re
from dataclasses import dataclass
from fractions import Fraction
from typing import Callable, Iterator, Sequence
import av
import cv2
import numpy as np
import torch
from .helper_functions import resize_nchw
def _robust_channel_stats(values: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
low, median, high = np.percentile(values, (10.0, 50.0, 90.0), axis=0)
return median.astype(np.float32), np.maximum(high - low, 1e-4).astype(np.float32)
def _covariance_shape(values: np.ndarray) -> tuple[np.ndarray, np.ndarray, float]:
clipped = np.clip(
values,
np.percentile(values, 1.0, axis=0),
np.percentile(values, 99.0, axis=0),
)
center = np.median(clipped, axis=0).astype(np.float32)
covariance = (
np.cov(clipped - center, rowvar=False).astype(np.float32)
if len(clipped) > 1
else np.zeros((2, 2), dtype=np.float32)
)
covariance += np.eye(2, dtype=np.float32) * 1e-5
spread = max(float(np.trace(covariance)), 1e-5)
return center, covariance / spread, spread
def _symmetric_matrix_power(matrix: np.ndarray, power: float) -> np.ndarray:
values, vectors = np.linalg.eigh(matrix)
values = np.maximum(values, 1e-5) ** power
return (vectors * values) @ vectors.T
def _prepare_mask(mask: torch.Tensor | None, index: int, height: int, width: int) -> np.ndarray | None:
if mask is None:
return None
selected = mask[min(index, mask.shape[0] - 1)].detach().float().cpu().numpy().squeeze()
if selected.shape != (height, width):
selected = cv2.resize(selected, (width, height), interpolation=cv2.INTER_LINEAR)
return np.clip(selected, 0.0, 1.0).astype(np.float32)
def _feather_outpaint_mask(mask: np.ndarray | None) -> np.ndarray | None:
if mask is None:
return None
binary = (mask > 0.5).astype(np.uint8)
if not np.any(binary) or np.all(binary):
return mask
feather_width = max(2.0, math.hypot(*mask.shape) * 0.005)
distance_inside = cv2.distanceTransform(binary, cv2.DIST_L2, 3)
inward_feather = np.clip(distance_inside / feather_width, 0.0, 1.0)
return mask * inward_feather
def _alignment_detail_image(image_rgb: np.ndarray) -> tuple[np.ndarray, float]:
gray = cv2.cvtColor((image_rgb * 255.0).astype(np.uint8), cv2.COLOR_RGB2GRAY)
scale = min(1.0, 1024.0 / max(gray.shape))
if scale < 1.0:
gray = cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
sigma = max(0.8, math.hypot(*gray.shape) * 0.0015)
blurred = cv2.GaussianBlur(gray, (0, 0), sigmaX=sigma, sigmaY=sigma)
return cv2.addWeighted(gray, 1.75, blurred, -0.75, 0.0), scale
def _fit_source_to_target(source_rgb: np.ndarray, target_rgb: np.ndarray) -> np.ndarray | None:
source_gray, source_scale = _alignment_detail_image(source_rgb)
target_gray, target_scale = _alignment_detail_image(target_rgb)
detector = cv2.SIFT_create(nfeatures=2500, contrastThreshold=0.01, edgeThreshold=20)
source_points, source_descriptors = detector.detectAndCompute(source_gray, None)
target_points, target_descriptors = detector.detectAndCompute(target_gray, None)
if source_descriptors is None or target_descriptors is None:
return None
pairs = cv2.BFMatcher(cv2.NORM_L2).knnMatch(source_descriptors, target_descriptors, k=2)
matches = [first for first, second in pairs if first.distance < 0.8 * second.distance]
if len(matches) < 4:
return None
source_xy = np.float32([source_points[match.queryIdx].pt for match in matches])
target_xy = np.float32([target_points[match.trainIdx].pt for match in matches])
threshold = max(2.0, math.hypot(*target_rgb.shape[:2]) * 0.003)
affine, inliers = cv2.estimateAffinePartial2D(
source_xy,
target_xy,
method=cv2.RANSAC,
ransacReprojThreshold=threshold,
maxIters=3000,
confidence=0.995,
refineIters=20,
)
inlier_count = 0 if inliers is None else int(inliers.sum())
if affine is None or inliers is None or inlier_count < 4 or inlier_count / len(matches) < 0.6:
return None
affine_full = np.eye(3, dtype=np.float64)
affine_full[:2] = affine
affine_full = (
np.diag([1.0 / target_scale, 1.0 / target_scale, 1.0])
@ affine_full
@ np.diag([source_scale, source_scale, 1.0])
)
affine = affine_full[:2]
scale = math.hypot(float(affine[0, 0]), float(affine[1, 0]))
if not 0.2 <= scale <= 5.0:
return None
source_h, source_w = source_rgb.shape[:2]
corners = np.float32([[[0, 0], [source_w, 0], [source_w, source_h], [0, source_h]]])
mapped = cv2.transform(corners, affine)[0]
target_h, target_w = target_rgb.shape[:2]
visible = cv2.intersectConvexConvex(
mapped.astype(np.float32),
np.float32([[0, 0], [target_w, 0], [target_w, target_h], [0, target_h]]),
)[0]
mapped_area = abs(float(cv2.contourArea(mapped)))
if mapped_area <= 1.0 or visible / mapped_area < 0.5:
return None
return affine.astype(np.float32)
def _matched_overlap_edge_pixels(
source_rgb: np.ndarray,
target_rgb: np.ndarray,
source_lab: np.ndarray,
target_lab: np.ndarray,
outpaint_mask: np.ndarray | None = None,
) -> tuple[np.ndarray, np.ndarray] | None:
affine = _fit_source_to_target(source_rgb, target_rgb)
if affine is None:
return None
target_h, target_w = target_rgb.shape[:2]
warped_source = cv2.warpAffine(
source_lab,
affine,
(target_w, target_h),
flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
)
source_mask = np.ones(source_rgb.shape[:2], dtype=np.uint8)
overlap = cv2.warpAffine(
source_mask,
affine,
(target_w, target_h),
flags=cv2.INTER_NEAREST,
borderMode=cv2.BORDER_CONSTANT,
)
distance = cv2.distanceTransform(overlap, cv2.DIST_L2, 3)
overlap_area = int(np.count_nonzero(overlap))
edge_width = max(3.0, math.sqrt(overlap_area) * 0.04)
source_edge = (overlap > 0) & (distance <= edge_width)
outside = (overlap == 0).astype(np.uint8)
outside_distance = cv2.distanceTransform(outside, cv2.DIST_L2, 3)
target_edge = (outside > 0) & (outside_distance <= edge_width)
if outpaint_mask is not None:
target_edge &= outpaint_mask > 1e-4
radius = max(1, int(math.ceil(edge_width)))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (radius * 2 + 1, radius * 2 + 1))
adjacent = cv2.dilate(target_edge.astype(np.uint8), kernel) > 0
source_edge &= adjacent
if np.count_nonzero(source_edge) < 16 or np.count_nonzero(target_edge) < 16:
return None
return warped_source[source_edge], target_lab[target_edge]
def match_image_properties(
source: torch.Tensor,
target: torch.Tensor,
overall_weight: float,
color_weight: float,
lighting_weight: float,
texture_preservation: float,
mask: torch.Tensor | None = None,
saturation_weight: float = 1.0,
contrast_weight: float = 1.0,
) -> torch.Tensor:
"""Transfer global color and lighting statistics without spatial correspondence."""
if overall_weight <= 0.0 or (color_weight <= 0.0 and lighting_weight <= 0.0 and saturation_weight <= 0.0 and contrast_weight <= 0.0):
return target.clone()
outputs = []
for index in range(target.shape[0]):
source_index = min(index, source.shape[0] - 1)
source_rgb = np.clip(source[source_index, ..., :3].detach().float().cpu().numpy(), 0.0, 1.0)
target_rgb = np.clip(target[index, ..., :3].detach().float().cpu().numpy(), 0.0, 1.0)
source_lab = cv2.cvtColor(source_rgb, cv2.COLOR_RGB2LAB).astype(np.float32)
target_lab = cv2.cvtColor(target_rgb, cv2.COLOR_RGB2LAB).astype(np.float32)
apply_mask = _feather_outpaint_mask(_prepare_mask(mask, index, *target_rgb.shape[:2]))
matched_pixels = _matched_overlap_edge_pixels(
source_rgb,
target_rgb,
source_lab,
target_lab,
apply_mask,
)
if matched_pixels is None:
source_pixels = source_lab.reshape(-1, 3)
target_pixels = target_lab.reshape(-1, 3)
else:
source_pixels, target_pixels = matched_pixels
source_l_center, source_l_range = _robust_channel_stats(source_pixels[:, :1])
target_l_center, target_l_range = _robust_channel_stats(target_pixels[:, :1])
contrast = float(np.clip(source_l_range[0] / target_l_range[0], 0.25, 4.0))
contrast = 1.0 + (contrast - 1.0) * contrast_weight * overall_weight
lighting = lighting_weight * overall_weight
target_l = target_lab[..., 0]
sigma = max(1.0, math.hypot(*target_l.shape) * 0.01)
base = cv2.GaussianBlur(target_l, (0, 0), sigmaX=sigma, sigmaY=sigma)
detail = target_l - base
full_transfer = (target_l - target_l_center[0]) * contrast + target_l_center[0]
detail_preserving = (base - target_l_center[0]) * contrast + target_l_center[0] + detail
transferred_l = full_transfer * (1.0 - texture_preservation) + detail_preserving * texture_preservation
transferred_l += (source_l_center[0] - target_l_center[0]) * lighting
source_center, source_shape, source_spread = _covariance_shape(source_pixels[:, 1:3])
target_center, target_shape, target_spread = _covariance_shape(target_pixels[:, 1:3])
shape_transform = _symmetric_matrix_power(source_shape, 0.5) @ _symmetric_matrix_power(target_shape, -0.5)
centered_chroma = target_lab[..., 1:3] - target_center
shaped_chroma = centered_chroma @ shape_transform.T
saturation = math.sqrt(source_spread / target_spread)
saturation = float(np.clip(saturation, 0.25, 4.0))
saturation = 1.0 + (saturation - 1.0) * saturation_weight * overall_weight
color = color_weight * overall_weight
color_matched = shaped_chroma + source_center
transferred_chroma = target_lab[..., 1:3] * (1.0 - color) + color_matched * color
transferred_chroma *= saturation
result_lab = target_lab.copy()
result_lab[..., 0] = transferred_l
result_lab[..., 1:3] = transferred_chroma
result_lab[..., 0] = np.clip(result_lab[..., 0], 0.0, 100.0)
result_lab[..., 1:3] = np.clip(result_lab[..., 1:3], -127.0, 127.0)
result_rgb = np.clip(cv2.cvtColor(result_lab, cv2.COLOR_LAB2RGB), 0.0, 1.0)
if apply_mask is not None:
result_rgb = target_rgb * (1.0 - apply_mask[..., None]) + result_rgb * apply_mask[..., None]
if target.shape[-1] > 3:
extra = target[index, ..., 3:].detach().float().cpu().numpy()
result_rgb = np.concatenate((result_rgb, extra), axis=-1)
outputs.append(torch.from_numpy(result_rgb.astype(np.float32)))
return torch.stack(outputs).to(device=target.device, dtype=target.dtype)
def mask_to_bounding_box(
mask: torch.Tensor,
invert: bool = False,
image: torch.Tensor | None = None,
) -> tuple[dict[str, int], torch.Tensor | None]:
"""Return one Core bounding box covering all nonzero mask pixels."""
if mask.ndim < 2:
raise ValueError("Mask to Bounding Box requires a mask with at least two dimensions.")
active_mask = 1.0 - mask if invert else mask
nonzero = torch.nonzero(active_mask)
if nonzero.numel() == 0:
raise ValueError("Mask to Bounding Box requires at least one nonzero pixel.")
y_min = int(nonzero[:, -2].min().item())
y_max = int(nonzero[:, -2].max().item())
x_min = int(nonzero[:, -1].min().item())
x_max = int(nonzero[:, -1].max().item())
bounding_box = {
"x": x_min,
"y": y_min,
"width": x_max - x_min + 1,
"height": y_max - y_min + 1,
}
cropped_image = (
image[:, y_min:y_max + 1, x_min:x_max + 1, :]
if image is not None
else None
)
return bounding_box, cropped_image
_HALO_CONTEXT_RADIUS = 13
_HALO_WORKSPACE_BYTES = 128 * 1024 * 1024
_LOHALO_CONTRAST = 3.38589
def halo_downscale_dimensions(width: int, height: int, megapixels: float, multiple: int) -> tuple[int, int, float, float, float]:
"""Return aligned output dimensions and a uniform, centered source transform."""
target_pixels = megapixels * 1024 * 1024
scale = math.sqrt(target_pixels / (width * height))
output_width = max(multiple, int((width * scale + multiple / 2) // multiple) * multiple)
output_height = max(multiple, int((height * scale + multiple / 2) // multiple) * multiple)
cover_scale = max(output_width / width, output_height / height)
if cover_scale >= 1.0:
raise ValueError("NoHalo/LoHalo Downscale requires an output smaller than the input.")
view_width = output_width / cover_scale
view_height = output_height / cover_scale
return output_width, output_height, cover_scale, (width - view_width) * 0.5, (height - view_height) * 0.5
def _mitchell_kernel(distance: torch.Tensor) -> torch.Tensor:
distance = distance.abs()
inner = (7.0 / 6.0) * distance**3 - 2.0 * distance**2 + 8.0 / 9.0
outer = (-7.0 / 18.0) * distance**3 + 2.0 * distance**2 - (10.0 / 3.0) * distance + 16.0 / 9.0
return torch.where(distance < 1.0, inner, torch.where(distance < 2.0, outer, 0.0))
def _robidoux_kernel(radius_squared: torch.Tensor) -> torch.Tensor:
sqrt_two = math.sqrt(2.0)
radius = torch.sqrt(radius_squared.clamp_min(0.0))
inner = radius_squared * (-3.0 * radius + (45739.0 + 7164.0 * sqrt_two) / 10319.0) + (-8926.0 - 14328.0 * sqrt_two) / 10319.0
outer = (radius + (-103.0 - 36.0 * sqrt_two) / (7.0 + 72.0 * sqrt_two)) * (radius - 2.0) ** 2
return torch.where(radius_squared < 1.0, inner, torch.where(radius_squared < 4.0, outer, 0.0))
def _inverse_sigmoidal(value: torch.Tensor) -> torch.Tensor:
sig1 = math.tanh(0.25 * _LOHALO_CONTRAST)
slope = (1.0 / sig1 - sig1) * 0.25 * _LOHALO_CONTRAST
middle = torch.atanh(((2.0 * sig1) * value - sig1).clamp(-0.999999, 0.999999)) * (2.0 / _LOHALO_CONTRAST) + 0.5
return torch.where(value <= 0.0, value / slope, torch.where(value >= 1.0, value / slope + 1.0 - 1.0 / slope, middle))
def _extended_sigmoidal(value: torch.Tensor) -> torch.Tensor:
sig1 = math.tanh(0.25 * _LOHALO_CONTRAST)
slope = (1.0 / sig1 - sig1) * 0.25 * _LOHALO_CONTRAST
middle = (0.5 / sig1) * torch.tanh(0.5 * _LOHALO_CONTRAST * value - 0.25 * _LOHALO_CONTRAST) + 0.5
return torch.where(value <= 0.0, slope * value, torch.where(value >= 1.0, slope * value + 1.0 - slope, middle))
def _minmod(first: torch.Tensor, second: torch.Tensor) -> torch.Tensor:
return torch.where(first * second >= 0.0, torch.where(first.square() <= first * second, first, second), 0.0)
def _nohalo_subdivision(p: torch.Tensor) -> tuple[torch.Tensor, ...]:
"""Vectorized GEGL/libvips NoHalo level-one subdivision for an oriented 5x5 stencil."""
u2, u3, u4 = p[..., 0, 1], p[..., 0, 2], p[..., 0, 3]
d1, d2, d3, d4, d5 = (p[..., 1, index] for index in range(5))
t1, t2, t3, t4, t5 = (p[..., 2, index] for index in range(5))
q1, q2, q3, q4, q5 = (p[..., 3, index] for index in range(5))
c2, c3, c4 = p[..., 4, 1], p[..., 4, 2], p[..., 4, 3]
du2, dt2, tq2, qc2 = d2-u2, t2-d2, q2-t2, c2-q2
du3, dt3, tq3, qc3 = d3-u3, t3-d3, q3-t3, c3-q3
du4, dt4, tq4, qc4 = d4-u4, t4-d4, q4-t4, c4-q4
d12, d23, d34, d45 = d2-d1, d3-d2, d4-d3, d5-d4
t12, t23, t34, t45 = t2-t1, t3-t2, t4-t3, t5-t4
q12, q23, q34, q45 = q2-q1, q3-q2, q4-q3, q5-q4
d3y, t3y = _minmod(dt3, du3), _minmod(dt3, tq3)
q3y = _minmod(qc3, tq3)
t4y, q4y, d4y = _minmod(dt4, tq4), _minmod(qc4, tq4), _minmod(dt4, du4)
t2x, t3x, t4x = _minmod(t23, t12), _minmod(t23, t34), _minmod(t45, t34)
q3x, q4x, q2x = _minmod(q23, q34), _minmod(q45, q34), _minmod(q23, q12)
d3x, d4x, d2x = _minmod(d23, d34), _minmod(d45, d34), _minmod(d23, d12)
t2y, q2y, d2y = _minmod(dt2, tq2), _minmod(qc2, tq2), _minmod(dt2, du2)
a12 = 0.5*(d3+t3) + 0.25*(d3y-t3y)
a32 = 0.5*(t3+q3) + 0.25*(t3y-q3y)
a34 = 0.5*(t4+q4) + 0.25*(t4y-q4y)
a14 = 0.5*(d4+t4) + 0.25*(d4y-t4y)
a21 = 0.5*(t2+t3) + 0.25*(t2x-t3x)
a23 = 0.5*(t3+t4) + 0.25*(t3x-t4x)
a43 = 0.5*(q3+q4) + 0.25*(q3x-q4x)
a41 = 0.5*(q2+q3) + 0.25*(q2x-q3x)
a33 = 0.125*((t3x-t4x)+(q3x-q4x)) + 0.5*(a32+a34)
a13 = 0.25*(d4-t3) + 0.125*(d4y-t4y+d3x-d4x) + 0.5*(a12+a23)
a31 = 0.25*(q2-t3) + 0.125*(q2x-q3x+t2y-q2y) + 0.5*(a21+a32)
a11 = 0.25*(d2+d3+t2+t3) + 0.125*(d2x-d3x+t2x-t3x+d2y+d3y-t2y-t3y)
return a11, a12, a13, a14, a21, t3, a23, t4, a31, a32, a33, a34, a41, q3, a43, q4
def _lbb(stencil: tuple[torch.Tensor, ...], x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
u1,u2,u3,u4,d1,d2,d3,d4,t1,t2,t3,t4,q1,q2,q3,q4 = stencil
min00 = torch.minimum(torch.minimum(torch.minimum(u1,u2),u3), torch.minimum(torch.minimum(d1,d2), torch.minimum(d3, torch.minimum(t1,torch.minimum(t2,t3)))))
max00 = torch.maximum(torch.maximum(torch.maximum(u1,u2),u3), torch.maximum(torch.maximum(d1,d2), torch.maximum(d3, torch.maximum(t1,torch.maximum(t2,t3)))))
min10 = torch.minimum(torch.minimum(torch.minimum(u2,u3),u4), torch.minimum(torch.minimum(d2,d3), torch.minimum(d4, torch.minimum(t2,torch.minimum(t3,t4)))))
max10 = torch.maximum(torch.maximum(torch.maximum(u2,u3),u4), torch.maximum(torch.maximum(d2,d3), torch.maximum(d4, torch.maximum(t2,torch.maximum(t3,t4)))))
min01 = torch.minimum(torch.minimum(torch.minimum(d1,d2),d3), torch.minimum(torch.minimum(t1,t2), torch.minimum(t3, torch.minimum(q1,torch.minimum(q2,q3)))))
max01 = torch.maximum(torch.maximum(torch.maximum(d1,d2),d3), torch.maximum(torch.maximum(t1,t2), torch.maximum(t3, torch.maximum(q1,torch.maximum(q2,q3)))))
min11 = torch.minimum(torch.minimum(torch.minimum(d2,d3),d4), torch.minimum(torch.minimum(t2,t3), torch.minimum(t4, torch.minimum(q2,torch.minimum(q3,q4)))))
max11 = torch.maximum(torch.maximum(torch.maximum(d2,d3),d4), torch.maximum(torch.maximum(t2,t3), torch.maximum(t4, torch.maximum(q2,torch.maximum(q3,q4)))))
values = (d2,d3,t2,t3)
mins, maxs = (min00,min10,min01,min11), (max00,max10,max01,max11)
dx0 = (d3-d1,d4-d2,t3-t1,t4-t2)
dy0 = (t2-u2,t3-u3,q2-d2,q3-d3)
cross0 = (u1-u3+t3-t1, u2-u4+t4-t2, q3-q1-d3+d1, q4-q2-d4+d2)
dx, dy, cross = [], [], []
for value, minimum, maximum, ddx, ddy, dcross in zip(values, mins, maxs, dx0, dy0, cross0):
limit = 6.0 * torch.minimum(value-minimum, maximum-value)
ddx = ddx.sign() * torch.minimum(ddx.abs(), limit)
ddy = ddy.sign() * torch.minimum(ddy.abs(), limit)
sum12, dif12 = 6.0*(ddx+ddy), 6.0*(ddx-ddy)
lower = torch.maximum(sum12.abs()-36.0*(value-minimum), dif12.abs()-36.0*(maximum-value))
upper = torch.minimum(36.0*(maximum-value)-sum12.abs(), 36.0*(value-minimum)-dif12.abs())
dx.append(ddx)
dy.append(ddy)
cross.append(dcross.clamp(min=lower, max=upper))
hx0, hx1 = 2*x**3-3*x**2+1, -2*x**3+3*x**2
hdx0, hdx1 = x**3-2*x**2+x, x**3-x**2
hy0, hy1 = 2*y**3-3*y**2+1, -2*y**3+3*y**2
hdy0, hdy1 = y**3-2*y**2+y, y**3-y**2
c = (hx0*hy0,hx1*hy0,hx0*hy1,hx1*hy1)
cx = (hdx0*hy0,hdx1*hy0,hdx0*hy1,hdx1*hy1)
cy = (hx0*hdy0,hx1*hdy0,hx0*hdy1,hx1*hdy1)
cxy = (hdx0*hdy0,hdx1*hdy0,hdx0*hdy1,hdx1*hdy1)
return sum(a*b for a,b in zip(c,values)) + 0.5*sum(a*b for a,b in zip(cx,dx)) + 0.5*sum(a*b for a,b in zip(cy,dy)) + 0.25*sum(a*b for a,b in zip(cxy,cross))
def _gather_halo_patch(image: torch.Tensor, x_index: torch.Tensor, y_index: torch.Tensor, offsets: torch.Tensor) -> torch.Tensor:
batch, channels, height, width = image.shape
yy = (y_index[..., None, None] + offsets[:, None]).clamp(0, height-1)
xx = (x_index[..., None, None] + offsets[None, :]).clamp(0, width-1)
linear = (yy * width + xx).reshape(1, 1, -1).expand(batch, channels, -1)
return torch.gather(image.reshape(batch, channels, -1), 2, linear).reshape(batch, channels, *x_index.shape, offsets.numel(), offsets.numel())
def downscale_nohalo_lohalo(image: torch.Tensor, method: str, megapixels: float, multiple: int) -> torch.Tensor:
"""Downscale BHWC images using axis-aligned GEGL NoHalo or LoHalo sampling."""
original_dtype = image.dtype
batch, height, width, channels = image.shape
out_width, out_height, scale, offset_x, offset_y = halo_downscale_dimensions(width, height, megapixels, multiple)
source = image.movedim(-1, 1).float()
support = min(_HALO_CONTEXT_RADIUS, max(2, math.ceil((2.0 if method == "lohalo" else 1.0) / scale + 0.5)))
kernel_size = 2 * support + 1
bytes_per_pixel = batch * kernel_size * kernel_size * (channels * 4 + 24)
tile_rows = max(1, min(out_height, _HALO_WORKSPACE_BYTES // max(1, out_width * bytes_per_pixel)))
x = offset_x + (torch.arange(out_width, device=image.device, dtype=torch.float32) + 0.5) / scale
x_anchor = torch.floor(x).long()
x_fraction = x - (x_anchor.float() + 0.5)
offsets = torch.arange(-support, support+1, device=image.device)
output = torch.empty((batch, channels, out_height, out_width), device=image.device, dtype=torch.float32)
for y_start in range(0, out_height, tile_rows):
y_stop = min(out_height, y_start + tile_rows)
y = offset_y + (torch.arange(y_start, y_stop, device=image.device, dtype=torch.float32) + 0.5) / scale
y_anchor = torch.floor(y).long()
y_fraction = y - (y_anchor.float() + 0.5)
xi = x_anchor.unsqueeze(0).expand(y.numel(), -1)
yi = y_anchor.unsqueeze(1).expand(-1, out_width)
patch = _gather_halo_patch(source, xi, yi, offsets)
dx = x_fraction[None, :, None, None] - offsets.float()[None, None, None, :]
dy = y_fraction[:, None, None, None] - offsets.float()[None, None, :, None]
if method == "lohalo":
weights = _mitchell_kernel(dx) * _mitchell_kernel(dy)
sigmoid_patch = _inverse_sigmoidal(patch)
mitchell = (sigmoid_patch * weights).sum((-1,-2))
if channels == 4:
mitchell[:, :3] = _extended_sigmoidal(mitchell[:, :3])
mitchell[:, 3] = (patch[:, 3] * weights).sum((-1,-2))
else:
mitchell = _extended_sigmoidal(mitchell)
ewa_weights = _robidoux_kernel(scale*scale*(dx*dx+dy*dy))
ewa_total = ewa_weights.sum((-1,-2))
ewa_total = torch.where(ewa_total.abs() < 1e-8, torch.ones_like(ewa_total), ewa_total)
ewa = (patch * ewa_weights).sum((-1,-2)) / ewa_total
tile = scale*scale*mitchell + (1.0-scale*scale)*ewa
else:
signs_x = torch.where(x_fraction >= 0, 1, -1)
signs_y = torch.where(y_fraction >= 0, 1, -1)
oriented = torch.empty((*patch.shape[:-2],5,5), device=image.device, dtype=patch.dtype)
center = support
patch_flat = patch.flatten(-2)
for row in range(5):
for column in range(5):
source_row = center + (row-2) * signs_y[:, None]
source_column = center + (column-2) * signs_x[None, :]
source_index = source_row * kernel_size + source_column
gather_index = source_index[None,None].expand(batch, channels, -1, -1).unsqueeze(-1)
oriented[..., row, column] = torch.gather(patch_flat, -1, gather_index).squeeze(-1)
subdivision = _nohalo_subdivision(oriented)
local_x = 2.0*x_fraction.abs()
local_y = 2.0*y_fraction.abs()
lbb = _lbb(subdivision, local_x[None,None,None,:], local_y[None,None,:,None])
ewa_weights = (1.0-torch.sqrt((scale*scale*(dx*dx+dy*dy)).clamp_min(0.0))).clamp_min(0.0)
ewa = (patch * ewa_weights).sum((-1,-2)) / ewa_weights.sum((-1,-2)).clamp_min(1e-8)
tile = scale*scale*lbb + (1.0-scale*scale)*ewa
output[:,:,y_start:y_stop] = tile
return output.movedim(1,-1).to(original_dtype)
VIDEO_FRAME_SAMPLING_STRATEGIES = (
"codec keyframes",
"uniform PTS",
"focused PTS",
)
VIDEO_FRAME_TIMESTAMP_FORMATS = (
"HH:MM:SS.mmm",
"HH:MM:SS:mmm",
"MM:SS.mmm",
"MM:SS:mmm",
"00.000s",
"0.0s",
"0.00s",
)
VIDEO_FRAME_TIMELINE_STYLES = (
"H3 alignment prefix",
"H3 pictures",
"indexed",
"timestamps only",
"custom",
)
VIDEO_TIMELINE_TEXT_STRUCTURE = (
"For the target video, at <<time>> into the target video, "
"<<picture>> (from <<shot>>) is fully referenced."
)
VIDEO_STRUCTURED_TIMELINE_TEXT_STRUCTURE = (
"Target video duration is <<duration>> seconds divided into "
"<<segments>> segments. Reference each image with <<references>>."
)
VIDEO_TEXT_TIMELINE_TEXT_STRUCTURE = "Shot <<shot>> at <<timestamp>>."
VIDEO_TEXT_STRUCTURED_TIMELINE_TEXT_STRUCTURE = (
"Target video duration is <<duration>> seconds divided into "
"<<segments>> segments. <<shot>> at <<timestamp>>."
)
_VIDEO_TIMELINE_TEXT_MARKERS = frozenset(
{"time", "timestamp", "picture", "shot"}
)
_VIDEO_STRUCTURED_TIMELINE_TEXT_MARKERS = frozenset(
{"duration", "segments", "timestamps", "references", "shot", "timestamp"}
)
_VIDEO_TEXT_TIMELINE_TEXT_MARKERS = frozenset({"shot", "time", "timestamp"})
_VIDEO_TEXT_STRUCTURED_TIMELINE_TEXT_MARKERS = frozenset(
{"duration", "segments", "timestamps", "shot", "timestamp"}
)
_VIDEO_TIMESTAMP_COLON_PATTERN = re.compile(
r"^(?:(?P<hours>\d+):)?(?P<minutes>\d+):(?P<seconds>\d+)(?P<fraction>[.:]\d+)?$"
)
def parse_video_timestamp(value) -> Fraction:
"""Parse one supported video timestamp into exact nonnegative seconds."""
if isinstance(value, bool):
raise ValueError("boolean values are not timestamps")
if isinstance(value, Fraction):
result = value
elif isinstance(value, (int, float)):
if not math.isfinite(value):
raise ValueError("timestamp must be finite")
result = Fraction(str(value))
elif isinstance(value, str):
text = value.strip()
if not text:
raise ValueError("timestamp is empty")
suffix = re.fullmatch(r"(.+?)\s*(?:s|seconds?)", text, re.IGNORECASE)
if suffix:
text = suffix.group(1).strip()
colon_parts = text.split(":")
if len(colon_parts) in (3, 4) and all(part.isdigit() for part in colon_parts):
if len(colon_parts) == 4:
hours, minutes, seconds, milliseconds = map(int, colon_parts)
else:
hours = 0
minutes, seconds, milliseconds = map(int, colon_parts)
if minutes >= 60 and hours:
raise ValueError("minute component must be below 60")
if seconds >= 60:
raise ValueError("second component must be below 60")
result = Fraction(hours * 3600 + minutes * 60 + seconds) + Fraction(milliseconds, 10 ** len(colon_parts[-1]))
if result < 0:
raise ValueError("timestamp must not be negative")
return result
match = _VIDEO_TIMESTAMP_COLON_PATTERN.fullmatch(text)
if match:
hours = int(match.group("hours") or 0)
minutes = int(match.group("minutes"))
seconds = int(match.group("seconds"))
if minutes >= 60 and match.group("hours") is not None:
raise ValueError("minute component must be below 60")
if seconds >= 60:
raise ValueError("second component must be below 60")
fraction = match.group("fraction")
fractional = Fraction(0)
if fraction:
digits = fraction[1:]
fractional = Fraction(int(digits), 10 ** len(digits))
result = Fraction(hours * 3600 + minutes * 60 + seconds) + fractional
else:
try:
result = Fraction(text)
except (ValueError, ZeroDivisionError) as exc:
raise ValueError(f"unsupported timestamp {value!r}") from exc
else:
raise ValueError(f"unsupported timestamp type {type(value).__name__}")
if result < 0:
raise ValueError("timestamp must not be negative")
return result
def parse_video_timestamps(value) -> list[Fraction]:
"""Flatten and parse timestamp containers while preserving source order."""
raw = []
def collect(item):
if isinstance(item, (list, tuple)):
for child in item:
collect(child)
elif isinstance(item, str) and re.search(r"[,;\n]", item):
parts = re.split(r"[,;\n]", item)
if any(not part.strip() for part in parts):
raise ValueError("timestamp list contains an empty item")
raw.extend(parts)
else:
raw.append(item)
collect(value)
if not raw:
raise ValueError("at least one timestamp is required")
parsed = []
for index, item in enumerate(raw, start=1):
try:
parsed.append(parse_video_timestamp(item))
except ValueError as exc:
raise ValueError(f"timestamp {index}: {exc}") from exc
for index in range(1, len(parsed)):
if parsed[index] < parsed[index - 1]:
raise ValueError(f"timestamp {index + 1} is earlier than timestamp {index}")
return parsed
@dataclass(frozen=True)
class VideoFrameRecord:
"""Presentation metadata for one frame in the active VIDEO input."""
frame_index: int
timestamp: Fraction
key_frame: bool
@dataclass(frozen=True)
class SampledVideoFrames:
image_batch: torch.Tensor
image_list: list[torch.Tensor]
timestamps: list[str]
timestamps_text: str
timeline_text: str
video_runtime: float
structured_timeline_text: str
def _as_fraction(value: Fraction | float | int) -> Fraction:
if isinstance(value, Fraction):
return value
if isinstance(value, int):
return Fraction(value)
return Fraction(str(value))
def _video_source_factory(video) -> Callable[[], str | io.BytesIO]:
source = video.get_stream_source()
if isinstance(source, (str, os.PathLike)):
path = os.fspath(source)
return lambda: path
seek = getattr(source, "seek", None)
if callable(seek):
seek(0)
data = source.read()
return lambda: io.BytesIO(data)
def _active_video_frames(
video,
source_factory: Callable[[], str | io.BytesIO] | None = None,
) -> Iterator[tuple[int, Fraction, av.VideoFrame]]:
"""Yield active frames in presentation order with clip-relative PTS."""
if source_factory is None:
source_factory = _video_source_factory(video)
source = source_factory()
start_seconds, duration_seconds = video.get_active_trim_window()
trim_start = _as_fraction(start_seconds)
trim_duration = _as_fraction(duration_seconds)
with av.open(source, mode="r") as container:
if not container.streams.video:
raise ValueError("The VIDEO input contains no video stream.")
stream = container.streams.video[0]
if stream.time_base is None:
raise ValueError("The video stream has no time base for PTS conversion.")
stream_time_base = Fraction(stream.time_base)
stream_start_pts = stream.start_time if stream.start_time is not None else 0
stream_origin = Fraction(stream_start_pts) * stream_time_base
active_start = stream_origin + trim_start
active_end = active_start + trim_duration if trim_duration > 0 else None
if trim_start > 0:
seek_pts = stream_start_pts + int(trim_start / stream_time_base)
container.seek(seek_pts, stream=stream, backward=True, any_frame=False)
relative_origin = None
previous_time = None
frame_index = 0
for frame in container.decode(stream):
if frame.pts is None:
raise ValueError(
"A decoded video frame has no PTS; exact timestamp sampling is unavailable."
)
frame_time_base = frame.time_base or stream.time_base
if frame_time_base is None:
raise ValueError(
"A decoded video frame has no time base for PTS conversion."
)
presentation_time = Fraction(frame.pts) * Fraction(frame_time_base)
if presentation_time < active_start:
continue
if active_end is not None and presentation_time >= active_end:
break
if previous_time is not None and presentation_time < previous_time:
raise ValueError("Video presentation timestamps are not monotonic.")
if relative_origin is None:
relative_origin = presentation_time
relative_time = presentation_time - relative_origin
previous_time = presentation_time
yield frame_index, relative_time, frame
frame_index += 1
def scan_video_frame_records(
video,
source_factory: Callable[[], str | io.BytesIO] | None = None,
) -> list[VideoFrameRecord]:
records = [
VideoFrameRecord(
frame_index=frame_index,
timestamp=timestamp,
key_frame=bool(frame.key_frame),
)
for frame_index, timestamp, frame in _active_video_frames(
video, source_factory
)
]
if not records:
raise ValueError("The active VIDEO input contains no decodable frames.")
return records
def _spacing_filter(
records: Sequence[VideoFrameRecord], minimum_spacing: Fraction
) -> list[VideoFrameRecord]:
selected: list[VideoFrameRecord] = []
for record in sorted(records, key=lambda item: (item.timestamp, item.frame_index)):
if not selected or record.timestamp - selected[-1].timestamp >= minimum_spacing:
selected.append(record)
return selected
def _evenly_thin(
records: Sequence[VideoFrameRecord],
count: int,
*,
single_from_end: bool,
) -> list[VideoFrameRecord]:
if count <= 0 or not records:
return []
if count >= len(records):
return list(records)
if count == 1:
index = len(records) - 1 if single_from_end else len(records) // 2
return [records[index]]
denominator = count - 1
last_index = len(records) - 1
indices = [
(position * last_index + denominator // 2) // denominator
for position in range(count)
]
return [records[index] for index in indices]
def _select_uniform_samples(
records: Sequence[VideoFrameRecord],
maximum_frames: int,
include_zero_time: bool,
minimum_spacing: Fraction,
timestamp_format: str | None,
) -> tuple[list[VideoFrameRecord], list[Fraction]]:
zero_record = records[0]
candidates = [record for record in records if record.frame_index != zero_record.frame_index]
if maximum_frames == 0:
combined = ([zero_record] if include_zero_time else []) + candidates
selected = _spacing_filter(combined, minimum_spacing)
return selected, [record.timestamp for record in selected]
if not candidates:
selected = [zero_record] if include_zero_time else []
return selected, [record.timestamp for record in selected]
if include_zero_time and maximum_frames == 1:
return [zero_record], [Fraction(0)]
duration = records[-1].timestamp
if duration <= 0:
selected = [zero_record] if include_zero_time else []
return selected, [record.timestamp for record in selected]
if include_zero_time:
target_count = maximum_frames - 1
targets = [
duration * Fraction(position, target_count)
for position in range(1, target_count + 1)
]
selected_pairs = [(zero_record, Fraction(0))]
else:
target_count = maximum_frames
targets = [
duration * Fraction(position, target_count + 1)
for position in range(1, target_count + 1)
]
selected_pairs = []
if timestamp_format is not None:
targets = [
round_video_timestamp(target, timestamp_format)
for target in targets
]
for target in targets:
selected_pairs.append(
(
min(
candidates,
key=lambda record: (
abs(record.timestamp - target),
record.timestamp,
record.frame_index,
),
),
target,
)
)
unique = {
record.frame_index: (record, timestamp)
for record, timestamp in selected_pairs
}
spaced = _spacing_filter(
[record for record, _ in unique.values()],
minimum_spacing,
)
timestamps_by_index = {
record.frame_index: timestamp
for record, timestamp in unique.values()
}
return spaced, [timestamps_by_index[record.frame_index] for record in spaced]
def _select_uniform_records(
records: Sequence[VideoFrameRecord],
maximum_frames: int,
include_zero_time: bool,
minimum_spacing: Fraction,
) -> list[VideoFrameRecord]:
selected, _ = _select_uniform_samples(
records,
maximum_frames,
include_zero_time,
minimum_spacing,
timestamp_format=None,
)
return selected
def _select_focused_samples(records: Sequence[VideoFrameRecord], maximum_frames: int, include_zero_time: bool, minimum_spacing: Fraction, timestamp_format: str | None, focus_areas: int, focus_one: float, focus_two: float, focus_three: float) -> tuple[list[VideoFrameRecord], list[Fraction]]:
if maximum_frames == 0:
return _select_uniform_samples(records, maximum_frames, include_zero_time, minimum_spacing, timestamp_format)
zero_record = records[0]
candidates = [record for record in records if record.frame_index != zero_record.frame_index]
if not candidates:
selected = [zero_record] if include_zero_time else []
return selected, [record.timestamp for record in selected]
if include_zero_time and maximum_frames == 1:
return [zero_record], [Fraction(0)]
duration = records[-1].timestamp
if duration <= 0:
selected = [zero_record] if include_zero_time else []
return selected, [record.timestamp for record in selected]
targets = [
_as_fraction(target) for target in focused_timeline_timestamps(
maximum_frames, float(duration), focus_areas, focus_one, focus_two, focus_three, include_zero_time, include_zero_time
)
]
if timestamp_format is not None:
targets = [round_video_timestamp(target, timestamp_format) for target in targets]
selected_pairs = []
for target in targets:
if include_zero_time and target == 0:
selected_pairs.append((zero_record, Fraction(0)))
else:
selected_pairs.append((min(candidates, key=lambda record: (abs(record.timestamp - target), record.timestamp, record.frame_index)), target))
unique = {record.frame_index: (record, timestamp) for record, timestamp in selected_pairs}
spaced = _spacing_filter([record for record, _ in unique.values()], minimum_spacing)
timestamps_by_index = {record.frame_index: timestamp for record, timestamp in unique.values()}
return spaced, [timestamps_by_index[record.frame_index] for record in spaced]
def _select_keyframe_records(
records: Sequence[VideoFrameRecord],
maximum_frames: int,
include_zero_time: bool,
minimum_spacing: Fraction,
keyframe_stride: int,
) -> list[VideoFrameRecord]:
zero_record = records[0]
raw_keyframes = [record for record in records if record.key_frame]
candidates = raw_keyframes[::keyframe_stride]
if include_zero_time:
combined = [zero_record] + [
record for record in candidates if record.frame_index != zero_record.frame_index
]
else:
combined = [record for record in candidates if record.timestamp > 0]
spaced = _spacing_filter(combined, minimum_spacing)
if maximum_frames == 0 or len(spaced) <= maximum_frames:
return spaced
if include_zero_time:
zero = spaced[0]
remaining = _evenly_thin(
spaced[1:],
maximum_frames - 1,
single_from_end=True,
)
return [zero] + remaining
return _evenly_thin(
spaced,
maximum_frames,
single_from_end=False,
)
def _select_video_frame_records_and_timestamps(
records: Sequence[VideoFrameRecord],
strategy: str,
maximum_frames: int,
include_zero_time: bool,
minimum_spacing_seconds: float,
keyframe_stride: int,
timestamp_format: str | None,
focus_areas: int = 0,
focus_one: float = 0.5,
focus_two: float = 0.5,
focus_three: float = 0.5,
) -> tuple[list[VideoFrameRecord], list[Fraction]]:
if strategy not in VIDEO_FRAME_SAMPLING_STRATEGIES:
raise ValueError(f"Unsupported video-frame sampling strategy: {strategy}")
if maximum_frames < 0:
raise ValueError("maximum_frames must be zero or greater.")
if minimum_spacing_seconds < 0:
raise ValueError("minimum_spacing_seconds must be zero or greater.")
if keyframe_stride < 1:
raise ValueError("keyframe_stride must be at least one.")
if not records:
raise ValueError("No video-frame records are available for selection.")
ordered = sorted(records, key=lambda item: (item.timestamp, item.frame_index))
minimum_spacing = _as_fraction(minimum_spacing_seconds)
if strategy == "uniform PTS":
selected, output_timestamps = _select_uniform_samples(
ordered,
maximum_frames,
include_zero_time,
minimum_spacing,
timestamp_format,
)
elif strategy == "focused PTS":
selected, output_timestamps = _select_focused_samples(
ordered, maximum_frames, include_zero_time, minimum_spacing, timestamp_format, focus_areas, focus_one, focus_two, focus_three
)
else:
selected = _select_keyframe_records(