-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowcore.py
More file actions
1160 lines (980 loc) · 37.3 KB
/
Copy pathflowcore.py
File metadata and controls
1160 lines (980 loc) · 37.3 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
#!/usr/bin/env python3
# cineFlow -- degraining small-gauge film scans
#
# Copyright (C) 2026 Dr. R. Henkel
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version. See <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Commercial licences are available for use cases the GPL does not cover.
# Enquiries: license@pixelcircus.com
import importlib
import math
import os
import subprocess
import sys
import threading
from collections import OrderedDict
from typing import NamedTuple
import cv2
import numpy as np
from cineflow_defaults import (DEFAULT_CONFIG, SCENE_PARAMS, EPS_GUARD,
VIDEO_EXTS as _VIDEO_EXTS)
import cineio
USE_BURT = False
_BURT_9 = np.array([1, 8, 28, 56, 70, 56, 28, 8, 1], dtype=np.float32)
_BURT_9 = _BURT_9 / _BURT_9.sum()
_dis = cv2.DISOpticalFlow_create(cv2.DISOPTICAL_FLOW_PRESET_MEDIUM)
_raft_models = {}
_raft_device = None
_tv_of = None
_raft_import_error = None
RAFT_AVAILABLE = False
try:
import torch
import torchvision.models.optical_flow as _tv_of
_raft_device = "cuda" if torch.cuda.is_available() else "cpu"
RAFT_AVAILABLE = True
except Exception as _e:
_raft_import_error = repr(_e)
RAFT_AVAILABLE = False
def log(tag, text=""):
line = f"[{tag}] {text}" if text else f"[{tag}]"
try:
out = sys.stdout
if out is None:
return
out.write(line.encode("ascii", "replace").decode("ascii") + "\n")
out.flush()
except Exception:
pass
def log_is_tty():
try:
return bool(sys.stdout) and sys.stdout.isatty()
except Exception:
return False
class TrustRaw(NamedTuple):
resid: object
warped: object
offset: int
_OFFSET_DEPENDENT_KEYS = {
"nbr_warped", "nbr_warped_trust",
"flow_fw", "warped_flow_bw",
"trust_geo", "trust_photo",
}
_VIEW_ONLY_KEYS = {
"_neighbor_offset",
"_proxy",
"_need_dustA",
"_need_dustB",
}
def make_cfg(**overrides):
cfg = dict(DEFAULT_CONFIG)
unknown = [k for k in overrides
if k not in cfg and k not in _VIEW_ONLY_KEYS]
if unknown:
raise KeyError(f"unknown parameters: {unknown}")
cfg.update(overrides)
cfg.setdefault("_neighbor_offset", 1)
return cfg
class Backend(NamedTuple):
key: str
label: str
gpu: bool
color: bool
max_pixels: int
probe: str
note: str
BACKENDS = OrderedDict((b.key, b) for b in (
Backend("RAFT", "RAFT", True, True, 1_690_000, "torchvision",
"Torch/GPU. Accurate, robust against grain -- but fills areas "
"without correspondence with self-consistent flow that passes "
"geoTrust."),
Backend("DIS", "DIS", False, False, 0, "cv2",
"OpenCV/CPU. Faster, and fails LOUDLY: without correspondence "
"the flow becomes incoherent and geoTrust catches it."),
))
ALL_BACKENDS = tuple(BACKENDS)
DEFAULT_BACKEND = "RAFT"
def backend_label(key):
b = BACKENDS.get(key)
return b.label if b else key
def backend_key(label):
for k, b in BACKENDS.items():
if b.label == label:
return k
return label
def backend_is_gpu(key):
b = BACKENDS.get(key)
return bool(b.gpu) if b else False
def backend_wants_color(key):
b = BACKENDS.get(key)
return bool(b.color) if b else False
def min_flow_div(key, width, height):
b = BACKENDS.get(key)
if not b or not b.max_pixels or not width or not height:
return 1.0
need = math.sqrt(float(width) * float(height) / float(b.max_pixels))
return max(1.0, math.ceil(need * 20.0) / 20.0)
def resolve_backend(cfg=None, backend=None):
want = backend or (cfg or {}).get("flow_backend") or DEFAULT_BACKEND
have = backends_available()
return (want if want in have else "DIS"), want
_backend_probe_cache = {}
_backend_probe_why = {}
def backends_available():
out = []
for key, b in BACKENDS.items():
ok = _backend_probe_cache.get(key)
if ok is None:
why = None
try:
importlib.import_module(b.probe)
if b.gpu:
importlib.import_module("torch")
ok = True
except Exception as e:
ok = False
why = repr(e)
_backend_probe_cache[key] = ok
if why:
_backend_probe_why[key] = why
if not ok:
hint = ("PyTorch + torchvision" if b.gpu
else "opencv-python")
log(b.label, f"not available -- install {hint}")
log(b.label, f" {why}")
if ok:
out.append(key)
return out or ["DIS"]
def backend_reason(key):
if key not in BACKENDS:
return f"not a valid backend name (valid: {' | '.join(ALL_BACKENDS)})"
backends_available()
if key in _backend_probe_why:
return f"probe failed: {_backend_probe_why[key]}"
return None
def norm(d, lo, hi):
x = (d.astype(np.float32) - lo) / (hi - lo)
x = np.clip(x, 0.0, 1.0)
u8 = (x * 255.0 + 0.5).astype(np.uint8)
if u8.ndim == 2:
return cv2.cvtColor(u8, cv2.COLOR_GRAY2BGR)
return u8[:, :, ::-1].copy()
def heat(d):
x = np.clip(d.astype(np.float32), 0.0, 1.0)
u8 = (x * 255.0 + 0.5).astype(np.uint8)
if u8.ndim == 3:
u8 = cv2.cvtColor(u8, cv2.COLOR_RGB2GRAY)
return cv2.applyColorMap(u8, cv2.COLORMAP_TURBO)
_FLOW_HUE_OFFSET = 0.0
_FLOW_SAT = 191
FLOW_MAXMAG = 30.0
FLOW_MAXMAG_REL = 8.0
def flow_hsv(flow, maxmag=FLOW_MAXMAG, hue_offset=_FLOW_HUE_OFFSET, sat=_FLOW_SAT):
fx, fy = flow[..., 0], flow[..., 1]
mag, ang = cv2.cartToPolar(fx.astype(np.float32), fy.astype(np.float32))
hue = (ang * 180.0 / np.pi / 2.0 + float(hue_offset)) % 180.0
hsv = np.zeros((*flow.shape[:2], 3), dtype=np.uint8)
hsv[..., 0] = hue.astype(np.uint8)
hsv[..., 1] = int(sat)
hsv[..., 2] = np.clip(mag / maxmag * 255.0, 0, 255).astype(np.uint8)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def dominant_flow(flow):
return np.median(flow.reshape(-1, 2), axis=0)
def flow_hsv_rel(flow, maxmag=FLOW_MAXMAG_REL, hue_offset=_FLOW_HUE_OFFSET):
return flow_hsv(flow - dominant_flow(flow), maxmag=maxmag,
hue_offset=hue_offset)
def resid_mag(flow, hi=10.0):
mag = np.sqrt(flow[..., 0]**2 + flow[..., 1]**2)
return heat(np.clip(mag / hi, 0, 1))
def load_tiff(path):
return cineio.load_tiff(path)
VIDEO_EXTS = _VIDEO_EXTS
_VIDEO_CACHE_FRAMES = 240
_VIDEO_READ_AHEAD = 24
_VIDEO_SEEK_BACK = 3
_tool = cineio.tool
def have_ffmpeg():
return bool(_tool("ffmpeg")) and bool(_tool("ffprobe"))
def is_video(path):
return os.path.isfile(path) and path.lower().endswith(VIDEO_EXTS)
_ffprobe = cineio.ffprobe
_video_fps = cineio.video_fps
_video_frame_count = cineio.video_frame_count
_guess_range = cineio.guess_range
class FolderSource:
kind = "tiff"
depth = 16
backend = "TIFF"
CACHE_N = 9
def __init__(self, folder):
import glob
self.path = folder
self.files = sorted(glob.glob(os.path.join(folder, "*.tif")) +
glob.glob(os.path.join(folder, "*.tiff")),
key=cineio.numeric_sort_key)
if not self.files:
raise ValueError(
f"no TIFF files in {folder} -- cineFlow reads "
f".tif/.tiff image sequences and .mov/.mkv/.mp4/.avi "
f"video files")
self.height = self.width = None
self._cache = {}
self._order = []
def __len__(self):
return len(self.files)
def __bool__(self):
return bool(self.files)
def __getitem__(self, i):
hit = self._cache.get(i)
if hit is not None:
try:
self._order.remove(i)
except ValueError:
pass
self._order.append(i)
return hit
img = load_tiff(self.files[i])
self._cache[i] = img
self._order.append(i)
while len(self._order) > self.CACHE_N:
self._cache.pop(self._order.pop(0), None)
return img
def close(self):
self._cache.clear()
self._order.clear()
class VideoSource:
kind = "video"
depth = 16
backend = "ffmpeg (16 bit)"
def __init__(self, path, cache_frames=_VIDEO_CACHE_FRAMES):
self.path = path
self.st = _ffprobe(path)
self.width = int(self.st["width"])
self.height = int(self.st["height"])
self.fps = _video_fps(self.st)
self.n = _video_frame_count(path, self.st)
self.color_range = _guess_range(path, self.st)
self.color_space = self.st.get("color_space") or "bt709"
if self.color_space in ("unknown", "-"):
self.color_space = "bt709"
self._cache = OrderedDict()
self._cache_max = max(8, int(cache_frames))
self._proc = None
self._next = None
self._drain = None
self._fb = self.width * self.height * 3 * 2
self._lock = threading.RLock()
def _cmd(self, seek_idx):
cmd = [_tool("ffmpeg"), "-nostdin", "-v", "error"]
if seek_idx > 0:
cmd += ["-ss", f"{max(0.0, (seek_idx - 0.1) / self.fps):.6f}"]
cmd += ["-i", self.path]
pix = str(self.st.get("pix_fmt", ""))
if not ("gbr" in pix or "rgb" in pix):
cmd += ["-vf", f"scale=in_color_matrix={self.color_space}:"
f"in_range={self.color_range}:out_range=full"]
cmd += ["-pix_fmt", "rgb48le", "-f", "rawvideo", "-"]
return cmd
def _open(self, idx):
self._close_pipe()
seek = max(0, idx - _VIDEO_SEEK_BACK)
self._proc = subprocess.Popen(
self._cmd(seek), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, bufsize=self._fb)
self._drain = cineio.StderrDrain(self._proc)
self._next = seek
def _close_pipe(self):
if self._proc is not None:
try:
self._proc.stdout.close()
except Exception:
pass
try:
self._proc.kill()
self._proc.wait(timeout=2)
except Exception:
pass
self._proc = None
self._next = None
self._drain = None
def _pull(self):
buf = self._proc.stdout.read(self._fb)
if not buf or len(buf) < self._fb:
err = self._drain.text(300) if self._drain else ""
if err.strip():
log("video", f"ffmpeg: {err.strip()}")
return None
return (np.frombuffer(buf, dtype="<u2")
.reshape(self.height, self.width, 3)
.astype(np.float32) / 65535.0)
def _put(self, i, frame):
self._cache[i] = frame
self._cache.move_to_end(i)
while len(self._cache) > self._cache_max:
self._cache.popitem(last=False)
def __len__(self):
return self.n
def __bool__(self):
return self.n > 0
def __getitem__(self, i):
if i < 0:
i += self.n
if not (0 <= i < self.n):
raise IndexError(f"frame {i} out of range 0..{self.n - 1}")
with self._lock:
hit = self._cache.get(i)
if hit is not None:
self._cache.move_to_end(i)
return hit
if not (self._proc is not None and self._next == i):
self._open(i)
got = None
while self._next is not None and self._next <= i:
frame = self._pull()
if frame is None:
self._close_pipe()
raise IndexError(f"frame {i} could not be read")
self._put(self._next, frame)
if self._next == i:
got = frame
self._next += 1
for _ in range(_VIDEO_READ_AHEAD - 1):
if self._next is None or self._next >= self.n:
break
frame = self._pull()
if frame is None:
self._close_pipe()
break
self._put(self._next, frame)
self._next += 1
return got
def close(self):
with self._lock:
self._close_pipe()
self._cache.clear()
def __del__(self):
try:
self.close()
except Exception:
pass
class VideoSourceCV:
kind = "video"
depth = 8
backend = "OpenCV (8 bit)"
def __init__(self, path, cache_frames=_VIDEO_CACHE_FRAMES):
self.path = path
self.cap = cv2.VideoCapture(path)
if not self.cap.isOpened():
raise ValueError(f"OpenCV cannot open {path}")
self.n = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.fps = float(self.cap.get(cv2.CAP_PROP_FPS)) or 18.0
if self.n <= 0:
raise ValueError(f"cannot determine the frame count of {path}")
self.color_range = None
self._cache = OrderedDict()
self._cache_max = max(8, int(cache_frames))
self._next = 0
self._lock = threading.RLock()
log("video", "no ffmpeg found -- OpenCV fallback, 8 bit only.")
log("video", " Fine shadow detail cannot be judged this way; "
"install ffmpeg and ffprobe,")
log("video", " or set FFMPEG_DIR to the folder holding them.")
def _put(self, i, frame):
self._cache[i] = frame
self._cache.move_to_end(i)
while len(self._cache) > self._cache_max:
self._cache.popitem(last=False)
def _grab(self):
ok, bgr = self.cap.read()
if not ok or bgr is None:
return None
return (bgr[:, :, ::-1].astype(np.float32) / 255.0)
def __len__(self):
return self.n
def __bool__(self):
return self.n > 0
def __getitem__(self, i):
if i < 0:
i += self.n
if not (0 <= i < self.n):
raise IndexError(f"frame {i} out of range 0..{self.n - 1}")
with self._lock:
hit = self._cache.get(i)
if hit is not None:
self._cache.move_to_end(i)
return hit
if self._next != i:
self.cap.set(cv2.CAP_PROP_POS_FRAMES, i)
self._next = i
frame = self._grab()
if frame is None:
raise IndexError(f"frame {i} could not be read")
self._put(i, frame)
self._next = i + 1
for _ in range(_VIDEO_READ_AHEAD - 1):
if self._next >= self.n:
break
nxt = self._grab()
if nxt is None:
break
self._put(self._next, nxt)
self._next += 1
return frame
def close(self):
with self._lock:
try:
self.cap.release()
except Exception:
pass
self._cache.clear()
def __del__(self):
try:
self.close()
except Exception:
pass
def open_source(path):
if is_video(path):
if have_ffmpeg():
return VideoSource(path)
return VideoSourceCV(path)
if os.path.isdir(path):
return FolderSource(path)
raise ValueError(f"neither a TIFF folder nor a video file: {path}")
def to_gray(rgb):
return (0.299 * rgb[..., 0] + 0.587 * rgb[..., 1] + 0.114 * rgb[..., 2])
def burt_filter(gray):
tmp = cv2.filter2D(gray, -1, _BURT_9.reshape(1, -1), borderType=cv2.BORDER_REFLECT)
return cv2.filter2D(tmp, -1, _BURT_9.reshape(-1, 1), borderType=cv2.BORDER_REFLECT)
def pyr_downscale(img, factor):
f = float(factor)
if f <= 1.0 + 1e-6:
return img
n = int(math.floor(math.log2(f)))
out = img
for _ in range(n):
out = cv2.pyrDown(out)
rest = f / (2 ** n)
if rest > 1.0 + 1e-6:
sigma = 0.5 * math.sqrt(max(rest * rest - 1.0, 0.0))
if sigma > 1e-3:
k = int(math.ceil(3.0 * sigma)) * 2 + 1
out = cv2.GaussianBlur(out, (k, k), sigma)
h, w = out.shape[:2]
nh = max(8, int(round(h / rest)))
nw = max(8, int(round(w / rest)))
out = cv2.resize(out, (nw, nh), interpolation=cv2.INTER_AREA)
return out
def prep_flow_input(rgb):
g = to_gray(rgb)
return burt_filter(g) if USE_BURT else g
_raft_warned = set()
def _ensure_raft(which):
if which in _raft_models:
return _raft_models[which]
if not RAFT_AVAILABLE:
if which not in _raft_warned:
_raft_warned.add(which)
print(f"[RAFT] not available ({which}) -- computing with DIS instead!")
if _raft_import_error:
print(f"[RAFT] reason: {_raft_import_error}")
return None
try:
if which == "RAFT":
weights = _tv_of.Raft_Small_Weights.DEFAULT
model = _tv_of.raft_small(weights=weights).to(_raft_device).eval()
else:
weights = _tv_of.Raft_Large_Weights.DEFAULT
model = _tv_of.raft_large(weights=weights).to(_raft_device).eval()
_raft_models[which] = model
print(f"[RAFT] model loaded ({which}, {_raft_device}).")
return model
except Exception as e:
_raft_models[which] = None
if which not in _raft_warned:
_raft_warned.add(which)
print(f"[RAFT] Laden fehlgeschlagen ({which}): {e}")
print(f"[RAFT] -> computing with DIS although {which} was selected!")
return None
def _dis_flow(gray_from, gray_to):
a = (np.clip(gray_from, 0, 1) * 255).astype(np.uint8)
b = (np.clip(gray_to, 0, 1) * 255).astype(np.uint8)
return _dis.calc(a, b, None)
def _raft_flow(model, rgb_from, rgb_to, cfg, cache=None, key_from=None, key_to=None):
def prepare(rgb, key):
if cache is not None and key is not None and key in cache:
return cache[key]
down = pyr_downscale(rgb, cfg["downscale"])
dh, dw = down.shape[:2]
ph = (8 - dh % 8) % 8
pw = (8 - dw % 8) % 8
t = torch.from_numpy(np.clip(down, 0, 1).astype(np.float32))
t = t.permute(2, 0, 1)[None]
t = t * 2.0 - 1.0
if ph or pw:
t = torch.nn.functional.pad(t, (0, pw, 0, ph), mode='replicate')
t = t.to(_raft_device)
result = {"tensor": t, "h": dh, "w": dw}
if cache is not None and key is not None:
cache[key] = result
return result
prepped_from = prepare(rgb_from, key_from)
prepped_to = prepare(rgb_to, key_to)
dh, dw = prepped_from["h"], prepped_from["w"]
use_amp = (bool(cfg.get("raft_fp16", DEFAULT_CONFIG["raft_fp16"]))
and _raft_device == "cuda")
with torch.inference_mode():
with torch.autocast(device_type="cuda", enabled=use_amp):
flow = model(prepped_from["tensor"], prepped_to["tensor"],
num_flow_updates=int(cfg.get(
"raft_iterations",
DEFAULT_CONFIG["raft_iterations"])))[-1]
flow = flow[0].float().permute(1, 2, 0).cpu().numpy()
return flow[:dh, :dw].copy()
def compute_flow(rgb_from, rgb_to, cfg, backend, cache=None, key_from=None, key_to=None):
h, w = rgb_from.shape[:2]
model = _ensure_raft(backend) if BACKENDS.get(backend, None) and \
BACKENDS[backend].gpu else None
if model is not None:
flow = _raft_flow(model, rgb_from, rgb_to, cfg, cache=cache, key_from=key_from, key_to=key_to)
else:
ga = to_gray(rgb_from)
gb = to_gray(rgb_to)
a = pyr_downscale(ga, cfg["downscale"])
b = pyr_downscale(gb, cfg["downscale"])
flow = _dis_flow(a, b)
fh, fw = flow.shape[:2]
if (fh, fw) != (h, w):
sx, sy = w / fw, h / fh
flow = cv2.resize(flow, (w, h), interpolation=cv2.INTER_LINEAR)
flow[..., 0] *= sx
flow[..., 1] *= sy
return flow
def warp(img, flow):
h, w = flow.shape[:2]
xx, yy = np.meshgrid(np.arange(w, dtype=np.float32),
np.arange(h, dtype=np.float32))
map_x = (xx + flow[..., 0]).astype(np.float32)
map_y = (yy + flow[..., 1]).astype(np.float32)
return cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_REFLECT)
def sigmoid_trust(err, mismatch, softness):
raw = 1.0 - 1.0 / (1.0 + np.exp(-((err - mismatch) / softness)))
raw0 = 1.0 - 1.0 / (1.0 + np.exp(mismatch / softness))
return np.clip(raw / max(float(raw0), 1e-12), 0.0, 1.0)
def geometric_trust(resid, mismatch=3.0, softness=1.5):
error = np.sqrt(resid[..., 0]**2 + resid[..., 1]**2)
error = np.clip(error, 0.0, 15.0)
return sigmoid_trust(error, mismatch, softness)
def photometric_trust(warped_frame, frame_0, mismatch=0.1, radius=3,
softness=0.025):
photo_dev = np.abs(warped_frame - frame_0).mean(axis=2)
k = 2 * radius + 1
photo_smooth = cv2.blur(photo_dev, (k, k))
return sigmoid_trust(photo_smooth, mismatch, softness)
def group_median_mad(f0, warped_list, center_weight=1):
stack = np.stack([f0] * center_weight + list(warped_list), axis=0)
median_img = np.median(stack, axis=0)
dev = np.abs(stack - median_img[None, ...]).mean(axis=3)
mad = np.median(dev, axis=0)
return median_img, mad, dev
def committee_stats(f0, warped_list):
if not warped_list:
return None, None, None, None, None
stack = np.stack(warped_list, axis=0)
median_nb = np.median(stack, axis=0)
dev_nb = np.mean(np.abs(stack - median_nb[None]), axis=3)
disp = np.median(dev_nb, axis=0)
diff = f0 - median_nb
signed = np.mean(diff, axis=2)
resid = np.mean(np.abs(diff), axis=2)
return (median_nb.astype(np.float32), disp.astype(np.float32),
resid.astype(np.float32), signed.astype(np.float32),
dev_nb.astype(np.float32))
def outlier_trust(dev_member, mad, mismatch, softness, eps):
score = dev_member / (mad + eps)
return sigmoid_trust(score, mismatch, softness)
def _guided_filter_gray(guide, src, radius, eps):
k = 2 * radius + 1
mean_I = cv2.blur(guide, (k, k))
mean_p = cv2.blur(src, (k, k))
corr_I = cv2.blur(guide * guide, (k, k))
corr_Ip = cv2.blur(guide * src, (k, k))
var_I = corr_I - mean_I * mean_I
cov_Ip = corr_Ip - mean_I * mean_p
a = cov_Ip / (var_I + eps)
b = mean_p - a * mean_I
mean_a = cv2.blur(a, (k, k))
mean_b = cv2.blur(b, (k, k))
return mean_a * guide + mean_b
_WARNED_FILTERS = set()
def _smooth_detail(luma, sigma, detail_filter, detail_eps):
sig = max(float(sigma), 0.05)
r = max(1, int(math.ceil(3.0 * sig)))
if detail_filter == "guided":
return _guided_filter_gray(luma, luma, r, detail_eps)
if detail_filter != "gauss" and detail_filter not in _WARNED_FILTERS:
_WARNED_FILTERS.add(detail_filter)
print(f"[detail_filter] WARNING: unknown value {detail_filter!r} "
f"-- continuing with 'gauss' (valid: guided | gauss)")
k = 2 * r + 1
return cv2.GaussianBlur(luma, (k, k), sig)
def _neighbor_diag(f0, fj, cfg, backend, cache=None):
flow_fwd = compute_flow(f0, fj, cfg, backend, cache=cache, key_from=id(f0), key_to=id(fj))
flow_bwd = compute_flow(fj, f0, cfg, backend, cache=cache, key_from=id(fj), key_to=id(f0))
warped_frame = warp(fj, flow_fwd)
warped_flow_bw = warp(flow_bwd, flow_fwd)
resid = flow_fwd + warped_flow_bw
gtrust = geometric_trust(
resid,
mismatch=float(cfg.get("geo_mismatch",
DEFAULT_CONFIG["geo_mismatch"])),
softness=float(cfg.get("geo_softness",
DEFAULT_CONFIG["geo_softness"])))
ptrust = photometric_trust(
warped_frame, f0,
mismatch=float(cfg.get("photo_mismatch",
DEFAULT_CONFIG["photo_mismatch"])),
radius=int(cfg.get("photo_radius",
DEFAULT_CONFIG["photo_radius"])),
softness=float(cfg.get("photo_softness",
DEFAULT_CONFIG["photo_softness"])))
ctrust = gtrust * ptrust
return {
"flow_fwd": flow_fwd, "flow_bwd": flow_bwd,
"warped_frame": warped_frame, "warped_flow_bw": warped_flow_bw,
"resid": resid, "gtrust": gtrust, "ptrust": ptrust, "ctrust": ctrust,
}
def _apply_trust_stage(data, cfg):
raw = data.get("_trust_raw")
f0 = data.get("_center")
if raw is None or f0 is None:
return data
gd = float(cfg.get("geo_mismatch", DEFAULT_CONFIG["geo_mismatch"]))
gs = float(cfg.get("geo_softness", DEFAULT_CONFIG["geo_softness"]))
pd = float(cfg.get("photo_mismatch", DEFAULT_CONFIG["photo_mismatch"]))
prd = int(cfg.get("photo_radius", DEFAULT_CONFIG["photo_radius"]))
ps = float(cfg.get("photo_softness", DEFAULT_CONFIG["photo_softness"]))
trust_weighted = []
offsets = []
for entry in raw:
offsets.append(entry.offset)
gt = geometric_trust(entry.resid, mismatch=gd, softness=gs)
pt = photometric_trust(entry.warped, f0, mismatch=pd, radius=prd,
softness=ps)
warped = entry.warped
trust_weighted.append((warped, gt, pt, gt * pt))
data["_trust_weighted"] = trust_weighted
if data.get("_lazy"):
if trust_weighted:
_wf, _gt, _pt, _ct = trust_weighted[0]
data["trust_geo"] = _gt
data["trust_photo"] = _pt
data["nbr_warped_trust"] = _wf * _ct[..., None]
return data
num = f0.copy()
den = np.ones(f0.shape[:2], dtype=np.float32)
for wf, _gt, _pt, ct in trust_weighted:
num = num + wf * ct[..., None]
den = den + ct
data["fuse_best"] = num / den[..., None]
if trust_weighted:
want_off = cfg.get("_neighbor_offset", 1)
try:
i_diag = offsets.index(want_off)
except ValueError:
i_diag = 0
_wf, _gt, _pt, _ct = trust_weighted[i_diag]
data["trust_geo"] = _gt
data["trust_photo"] = _pt
data["nbr_warped_trust"] = _wf * _ct[..., None]
if trust_weighted:
data["trust_mean_best"] = np.mean([ct for _, _, _, ct in trust_weighted],
axis=0)
else:
data["trust_mean_best"] = np.zeros(f0.shape[:2], dtype=np.float32)
data["trust_by_offset"] = {off: float(ct.mean())
for off, (_w, _g, _p, ct)
in zip(offsets, trust_weighted)}
_apply_dustA_stage(data, cfg)
_apply_dustB_stage(data, cfg)
_apply_sharp_stage(data, cfg)
return data
DUSTA_KEYS = frozenset({
"fuse_dustA", "trust_mean_dustA",
"output_dustA",
"tex_weight_dustA", "sharp_gate_dustA",
})
DUSTB_KEYS = frozenset({
"fuse_dustB", "trust_mean_dustB",
"output_dustB",
"tex_weight_dustB", "sharp_gate_dustB",
})
def _need_dustA(cfg):
if cfg.get("mode", "best") == "dustA":
return True
want = cfg.get("_need_dustA")
return True if want is None else bool(want)
def _need_dustB(cfg):
if cfg.get("mode", "best") == "dustB":
return True
want = cfg.get("_need_dustB")
return True if want is None else bool(want)
def _apply_dustA_stage(data, cfg):
if not _need_dustA(cfg):
for k in DUSTA_KEYS:
data.pop(k, None)
return data
f0 = data.get("_center")
warped_neighbors = data.get("_warped_neighbors")
trust_weighted = data.get("_trust_weighted")
if f0 is None or not warped_neighbors:
return data
median_img, mad, dev = group_median_mad(f0, warped_neighbors, center_weight=cfg["center_weight"])
_mk, _ms, _me = cfg["dustA_mismatch"], cfg["dustA_softness"], EPS_GUARD
f0_trust = outlier_trust(dev[0], mad, _mk, _ms, _me)
grp_trusts = [outlier_trust(dev[cfg["center_weight"] + i], mad, _mk, _ms, _me)
for i in range(len(warped_neighbors))]
ctg_list = []
num = f0 * f0_trust[..., None]
den = f0_trust.copy()
for (wf, gt, _pt, _ct), pgt in zip(trust_weighted, grp_trusts):
ctg = gt * pgt
ctg_list.append(ctg)
num = num + wf * ctg[..., None]
den = den + ctg
den = np.clip(den, 1e-4, None)
data["fuse_dustA"] = np.clip(num / den[..., None], 0.0, 1.0)
data["trust_mean_dustA"] = np.mean(ctg_list, axis=0) if ctg_list else \
np.zeros(f0.shape[:2], dtype=np.float32)
return data
def _apply_dustB_stage(data, cfg):
if not _need_dustB(cfg):
for k in DUSTB_KEYS:
data.pop(k, None)
return data
f0 = data.get("_center")
warped_neighbors = data.get("_warped_neighbors")
trust_weighted = data.get("_trust_weighted")
if f0 is None or not warped_neighbors:
return data
median_nb, disp, resid, _signed, dev_nb = committee_stats(f0, warped_neighbors)
if median_nb is None:
return data
eps = EPS_GUARD
score = resid / (disp + eps)
t_center = sigmoid_trust(score, cfg["dustB_mismatch"], cfg["dustB_softness"])
committee_ok = sigmoid_trust(disp, cfg["dustB_disagreement"],
cfg["dustB_disagreement_softness"])
f0_trust = 1.0 - (1.0 - t_center) * committee_ok
f0_trust = np.clip(f0_trust, 0.0, 1.0).astype(np.float32)
grp_trusts = [outlier_trust(dev_nb[i], disp, cfg["dustB_mismatch"],
cfg["dustB_softness"], eps)
for i in range(len(warped_neighbors))]
ctg_list = []
num = f0 * f0_trust[..., None]
den = f0_trust.copy()
for (wf, gt, _pt, _ct), pgt in zip(trust_weighted, grp_trusts):
ctg = gt * pgt
ctg_list.append(ctg)
num = num + wf * ctg[..., None]
den = den + ctg
den = np.clip(den, 1e-4, None)
data["fuse_dustB"] = np.clip(num / den[..., None], 0.0, 1.0)
data["trust_mean_dustB"] = np.mean(ctg_list, axis=0) if ctg_list else \
np.zeros(f0.shape[:2], dtype=np.float32)
return data
def _sharpen(base_img, trust_mean, cfg):
luma = to_gray(base_img)
r = 4
k = 2 * r + 1
sigma_tex = max(k / 6.0, 0.1)
mean = cv2.GaussianBlur(luma, (k, k), sigma_tex)
mean_sq = cv2.GaussianBlur(luma * luma, (k, k), sigma_tex)
var = np.clip(mean_sq - mean * mean, 0, None)
texture = np.sqrt(var)
t = np.clip(texture / cfg["sharp_full"], 0.0, 1.0) ** cfg["sharp_gamma"]
tex_w = cfg["sharp_base"] + (1.0 - cfg["sharp_base"]) * t
gate = tex_w * trust_mean
luma_smooth = _smooth_detail(luma, cfg["detail_sigma"],
cfg["detail_filter"], cfg["detail_eps"])
detail = luma - luma_smooth
out = base_img + detail[..., None] * cfg["sharp_amount"] * gate[..., None]
return np.clip(out, 0.0, 1.0), texture, tex_w, gate
def _apply_sharp_stage(data, cfg):
if "fuse_best" not in data:
return data
out, texture, tex_w, gate = _sharpen(data["fuse_best"],
data["trust_mean_best"], cfg)
data["output_best"] = out
data["texture"] = texture
data["tex_weight_best"] = tex_w
data["sharp_gate_best"] = gate
if "fuse_dustA" in data:
_o, _tex, _tw, _g = _sharpen(data["fuse_dustA"],
data["trust_mean_dustA"], cfg)
data["output_dustA"] = _o
data["tex_weight_dustA"] = _tw
data["sharp_gate_dustA"] = _g
if "fuse_dustB" in data:
_o, _tex, _tw, _g = _sharpen(data["fuse_dustB"],
data["trust_mean_dustB"], cfg)
data["output_dustB"] = _o
data["tex_weight_dustB"] = _tw
data["sharp_gate_dustB"] = _g
return data
def process_frame(idx, files, cfg, backend, active_view=None):
data = {}
n = len(files)
f0 = files[idx]
data["input"] = f0
if active_view == "input":
return data
lazy = active_view is not None and active_view in _OFFSET_DEPENDENT_KEYS
warped_neighbors = []
trust_weighted = []
trust_raw = []
diag_by_offset = {}
flow_cache = {}
if not lazy:
offsets = [o for o in range(-cfg["context"], cfg["context"] + 1) if o != 0]
for off in offsets: