Skip to content

Commit 65d22c9

Browse files
committed
Implement experimental smoothening for video mode
1 parent 9b7150f commit 65d22c9

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/common_ui.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ def depthmap_mode_video(inp):
228228
"use a zoedepth model, since they provide the highest level of coherency between frames.")
229229
inp += gr.File(elem_id='depthmap_vm_input', label="Video or animated file",
230230
file_count="single", interactive=True, type="file")
231-
inp += gr.Dropdown(elem_id="depthmap_vm_smoothening_mode", label="Smoothening", type="value", choices=['none'])
232231
inp += gr.Checkbox(elem_id="depthmap_vm_custom_checkbox",
233232
label="Use custom/pregenerated DepthMap video", value=False)
233+
inp += gr.Dropdown(elem_id="depthmap_vm_smoothening_mode", label="Smoothening",
234+
type="value", choices=['none', 'experimental'], value='experimental')
234235
inp += gr.File(elem_id='depthmap_vm_custom', file_count="single",
235236
interactive=True, type="file", visible=False)
236237
with gr.Row():
@@ -239,6 +240,7 @@ def depthmap_mode_video(inp):
239240
minimum=1000, value=15000, maximum=50000, step=250)
240241

241242
inp.add_rule('depthmap_vm_custom', 'visible-if', 'depthmap_vm_custom_checkbox')
243+
inp.add_rule('depthmap_vm_smoothening_mode', 'visible-if-not', 'depthmap_vm_custom_checkbox')
242244
inp.add_rule('depthmap_vm_compress_bitrate', 'visible-if', 'depthmap_vm_compress_checkbox')
243245

244246
return inp

src/video_mode.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def frames_to_video(fps, frames, path, name, colorvids_bitrate=None):
7070
priority = [('avi', 'png'), ('avi', 'rawvideo'), ('mp4', 'libx264'), ('webm', 'libvpx')]
7171
if colorvids_bitrate:
7272
priority = reversed(priority)
73-
for format, codec in priority:
73+
for v_format, codec in priority:
7474
try:
7575
br = f'{colorvids_bitrate}k' if codec not in ['png', 'rawvideo'] else None
76-
clip.write_videofile(os.path.join(path, f"{name}.{format}"), codec=codec, bitrate=br)
76+
clip.write_videofile(os.path.join(path, f"{name}.{v_format}"), codec=codec, bitrate=br)
7777
done = True
7878
break
7979
except:
@@ -83,19 +83,31 @@ def frames_to_video(fps, frames, path, name, colorvids_bitrate=None):
8383

8484

8585
def process_predicitons(predictions, smoothening='none'):
86+
def global_scaling(objs, a=None, b=None):
87+
"""Normalizes objs, but uses (a, b) instead of (minimum, maximum) value of objs, if supplied"""
88+
normalized = []
89+
min_value = a if a is not None else min([obj.min() for obj in objs])
90+
max_value = b if b is not None else max([obj.max() for obj in objs])
91+
for obj in objs:
92+
normalized += [(obj - min_value) / (max_value - min_value)]
93+
return normalized
94+
8695
print('Processing generated depthmaps')
87-
# TODO: Smart normalizing (drop 0.001% of top and bottom values from the video/every cut)
88-
# TODO: Smoothening between frames (use splines)
8996
# TODO: Detect cuts and process segments separately
90-
9197
if smoothening == 'none':
92-
input_depths = []
93-
preds_min_value = min([pred.min() for pred in predictions])
94-
preds_max_value = max([pred.max() for pred in predictions])
95-
for pred in predictions:
96-
norm = (pred - preds_min_value) / (preds_max_value - preds_min_value) # normalize to [0; 1]
97-
input_depths += [norm]
98-
return input_depths
98+
return global_scaling(predictions)
99+
elif smoothening == 'experimental':
100+
processed = []
101+
clip = lambda val: min(max(0, val), len(predictions) - 1)
102+
for i in range(len(predictions)):
103+
f = np.zeros_like(predictions[i])
104+
for u, mul in enumerate([0.10, 0.20, 0.40, 0.20, 0.10]): # Eyeballed it, math person please fix this
105+
f += mul * predictions[clip(i + (u - 2))]
106+
processed += [f]
107+
# This could have been deterministic monte carlo... Oh well, this version is faster.
108+
a, b = np.percentile(np.stack(processed), [0.5, 99.5])
109+
return global_scaling(predictions, a, b)
110+
return predictions
99111

100112

101113
def gen_video(video, outpath, inp, custom_depthmap=None, colorvids_bitrate=None, smoothening='none'):

0 commit comments

Comments
 (0)