Skip to content

Commit 0170b5c

Browse files
committed
UI improvements
1 parent aa4ef10 commit 0170b5c

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

src/common_ui.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def main_ui_panel(is_depth_tab):
8080
"left-only", "only-right", "cyan-red-reverseanaglyph"
8181
][0:8 if backbone.get_opt('depthmap_script_extra_stereomodes', False) else 5], label="Output")
8282
with gr.Row():
83-
inp += go.STEREO_DIVERGENCE, gr.Slider(minimum=0.05, maximum=10.005, step=0.01,
83+
inp += go.STEREO_DIVERGENCE, gr.Slider(minimum=0.05, maximum=15.005, step=0.01,
8484
label='Divergence (3D effect)')
8585
inp += go.STEREO_SEPARATION, gr.Slider(minimum=-5.0, maximum=5.0, step=0.01,
8686
label='Separation (moves images apart)')
@@ -99,13 +99,16 @@ def main_ui_panel(is_depth_tab):
9999
with gr.Column(visible=False) as normalmap_options:
100100
with gr.Row():
101101
inp += go.NORMALMAP_PRE_BLUR, gr.Checkbox(label="Smooth before calculating normals")
102-
inp += go.NORMALMAP_PRE_BLUR_KERNEL, gr.Slider(minimum=1, maximum=31, step=2, label='Pre-smooth kernel size')
102+
inp += go.NORMALMAP_PRE_BLUR_KERNEL, gr.Slider(minimum=1, maximum=31, step=2, label='Pre-smooth kernel size', visible=False)
103+
inp.add_rule(go.NORMALMAP_PRE_BLUR_KERNEL, 'visible-if', go.NORMALMAP_PRE_BLUR)
103104
with gr.Row():
104105
inp += go.NORMALMAP_SOBEL, gr.Checkbox(label="Sobel gradient")
105106
inp += go.NORMALMAP_SOBEL_KERNEL, gr.Slider(minimum=1, maximum=31, step=2, label='Sobel kernel size')
107+
inp.add_rule(go.NORMALMAP_SOBEL_KERNEL, 'visible-if', go.NORMALMAP_SOBEL)
106108
with gr.Row():
107109
inp += go.NORMALMAP_POST_BLUR, gr.Checkbox(label="Smooth after calculating normals")
108-
inp += go.NORMALMAP_POST_BLUR_KERNEL, gr.Slider(minimum=1, maximum=31, step=2, label='Post-smooth kernel size')
110+
inp += go.NORMALMAP_POST_BLUR_KERNEL, gr.Slider(minimum=1, maximum=31, step=2, label='Post-smooth kernel size', visible=False)
111+
inp.add_rule(go.NORMALMAP_POST_BLUR_KERNEL, 'visible-if', go.NORMALMAP_POST_BLUR)
109112
with gr.Row():
110113
inp += go.NORMALMAP_INVERT, gr.Checkbox(label="Invert")
111114

@@ -219,8 +222,12 @@ def open_folder_action():
219222

220223

221224
def depthmap_mode_video(inp):
222-
inp += gr.File(elem_id='depthmap_input_video', label="Video or animated file",
225+
gr.HTML(value="Single video mode allows generating videos from videos. Every frame of the video is processed, "
226+
"please adjust generation settings, so that generation is not too slow. For the best results, "
227+
"Use a zoedepth model, since they provide the highest level of temporal coherency.")
228+
inp += gr.File(elem_id='depthmap_vm_input', label="Video or animated file",
223229
file_count="single", interactive=True, type="file")
230+
inp += gr.Dropdown(elem_id="depthmap_vm_smoothening_mode", label="Smoothening", type="value", choices=['none'])
224231
inp += gr.Checkbox(elem_id="depthmap_vm_custom_checkbox",
225232
label="Use custom/pregenerated DepthMap video", value=False)
226233
inp += gr.File(elem_id='depthmap_vm_custom', file_count="single",
@@ -230,23 +237,21 @@ def depthmap_mode_video(inp):
230237
inp += gr.Slider(elem_id='depthmap_vm_compress_bitrate', label="Bitrate (kbit)", visible=False,
231238
minimum=1000, value=15000, maximum=50000, step=250)
232239

233-
inp['depthmap_vm_custom_checkbox'].change(
234-
fn=lambda v: inp['depthmap_vm_custom'].update(visible=v),
235-
inputs=[inp['depthmap_vm_custom_checkbox']],
236-
outputs=[inp['depthmap_vm_custom']]
237-
)
238-
239-
inp['depthmap_vm_compress_checkbox'].change(
240-
fn=lambda v: inp['depthmap_vm_compress_bitrate'].update(visible=v),
241-
inputs=[inp['depthmap_vm_compress_checkbox']],
242-
outputs=[inp['depthmap_vm_compress_bitrate']]
243-
)
240+
inp.add_rule('depthmap_vm_custom', 'visible-if', 'depthmap_vm_custom_checkbox')
241+
inp.add_rule('depthmap_vm_compress_bitrate', 'visible-if', 'depthmap_vm_compress_checkbox')
244242

245243
return inp
246244

245+
246+
custom_css = """
247+
#depthmap_vm_input {height: 75px}
248+
#depthmap_vm_custom {height: 75px}
249+
"""
250+
251+
247252
def on_ui_tabs():
248253
inp = GradioComponentBundle()
249-
with gr.Blocks(analytics_enabled=False, title="DepthMap") as depthmap_interface:
254+
with gr.Blocks(analytics_enabled=False, title="DepthMap", css=custom_css) as depthmap_interface:
250255
with gr.Row().style(equal_height=False):
251256
with gr.Column(variant='panel'):
252257
inp += 'depthmap_mode', gr.HTML(visible=False, value='0')
@@ -341,17 +346,20 @@ def on_ui_tabs():
341346
depthmap_mode_2.select(lambda: '2', None, inp['depthmap_mode'])
342347
depthmap_mode_3.select(lambda: '3', None, inp['depthmap_mode'])
343348

344-
def custom_depthmap_change_fn(turned_on):
345-
return inp['custom_depthmap_img'].update(visible=turned_on), \
346-
inp['depthmap_gen_row_0'].update(visible=not turned_on), \
347-
inp['depthmap_gen_row_1'].update(visible=not turned_on), \
348-
inp['depthmap_gen_row_3'].update(visible=not turned_on), not turned_on
349-
inp['custom_depthmap'].change(
349+
def custom_depthmap_change_fn(mode, zero_on, three_on):
350+
hide = mode == '0' and zero_on or mode == '3' and three_on
351+
return inp['custom_depthmap_img'].update(visible=hide), \
352+
inp['depthmap_gen_row_0'].update(visible=not hide), \
353+
inp['depthmap_gen_row_1'].update(visible=not hide), \
354+
inp['depthmap_gen_row_3'].update(visible=not hide), not hide
355+
custom_depthmap_change_els = ['depthmap_mode', 'custom_depthmap', 'depthmap_vm_custom_checkbox']
356+
for el in custom_depthmap_change_els:
357+
inp[el].change(
350358
fn=custom_depthmap_change_fn,
351-
inputs=[inp['custom_depthmap']],
352-
outputs=[inp[st] for st in
353-
['custom_depthmap_img', 'depthmap_gen_row_0', 'depthmap_gen_row_1', 'depthmap_gen_row_3',
354-
go.DO_OUTPUT_DEPTH]])
359+
inputs=[inp[el] for el in custom_depthmap_change_els],
360+
outputs=[inp[st] for st in [
361+
'custom_depthmap_img', 'depthmap_gen_row_0', 'depthmap_gen_row_1', 'depthmap_gen_row_3',
362+
go.DO_OUTPUT_DEPTH]])
355363

356364
unloadmodels.click(
357365
fn=unload_models,
@@ -433,7 +441,8 @@ def run_generate(*inputs):
433441
colorvids_bitrate = inputs['depthmap_vm_compress_bitrate'] \
434442
if inputs['depthmap_vm_compress_checkbox'] else None
435443
ret = video_mode.gen_video(
436-
inputs['depthmap_input_video'], backbone.get_outpath(), inputs, custom_depthmap, colorvids_bitrate)
444+
inputs['depthmap_vm_input'], backbone.get_outpath(), inputs, custom_depthmap, colorvids_bitrate,
445+
inputs['depthmap_vm_smoothening_mode'])
437446
return [], None, None, ret
438447
except Exception as e:
439448
ret = format_exception(e)

src/video_mode.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ def frames_to_video(fps, frames, path, name, colorvids_bitrate=None):
8282
raise Exception('Saving the video failed!')
8383

8484

85-
def process_predicitons(predictions):
85+
def process_predicitons(predictions, smoothening='none'):
8686
print('Processing generated depthmaps')
8787
# TODO: Smart normalizing (drop 0.001% of top and bottom values from the video/every cut)
88-
preds_min_value = min([pred.min() for pred in predictions])
89-
preds_max_value = max([pred.max() for pred in predictions])
90-
91-
input_depths = []
92-
for pred in predictions:
93-
norm = (pred - preds_min_value) / (preds_max_value - preds_min_value) # normalize to [0; 1]
94-
input_depths += [norm]
9588
# TODO: Smoothening between frames (use splines)
9689
# TODO: Detect cuts and process segments separately
9790

98-
return input_depths
91+
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
9999

100100

101-
def gen_video(video, outpath, inp, custom_depthmap=None, colorvids_bitrate=None):
101+
def gen_video(video, outpath, inp, custom_depthmap=None, colorvids_bitrate=None, smoothening='none'):
102102
if inp[go.GEN_SIMPLE_MESH.name.lower()] or inp[go.GEN_INPAINTED_MESH.name.lower()]:
103103
return 'Creating mesh-videos is not supported. Please split video into frames and use batch processing.'
104104

@@ -117,7 +117,7 @@ def gen_video(video, outpath, inp, custom_depthmap=None, colorvids_bitrate=None)
117117

118118
gen_obj = core.core_generation_funnel(None, input_images, None, None, first_pass_inp)
119119
predictions = [x[2] for x in list(gen_obj)]
120-
input_depths = process_predicitons(predictions)
120+
input_depths = process_predicitons(predictions, smoothening)
121121
else:
122122
print('Using custom depthmap video')
123123
cdm_fps, input_depths = open_path_as_images(os.path.abspath(custom_depthmap.name), maybe_depthvideo=True)

0 commit comments

Comments
 (0)