Skip to content

Commit 58245f7

Browse files
committed
Refactor more, simplify stereo image mode selection logic
1 parent 3674d87 commit 58245f7

File tree

1 file changed

+27
-60
lines changed

1 file changed

+27
-60
lines changed

scripts/depthmap.py

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ def ui(self, is_img2img):
102102
show_heat = gr.Checkbox(label="Show HeatMap",value=False)
103103
with gr.Group():
104104
with gr.Row():
105-
gen_stereo = gr.Checkbox(label="Generate Stereo side-by-side image",value=False)
106-
gen_stereotb = gr.Checkbox(label="Generate Stereo top-bottom image",value=False)
107-
gen_anaglyph = gr.Checkbox(label="Generate Stereo anaglyph image (red/cyan)",value=False)
105+
gen_stereo = gr.Checkbox(label="Generate stereoscopic image", value=False)
106+
stereo_mode = gr.Dropdown(label="Stereoscopic image type", choices=['left-right', 'right-left', 'top-bottom', 'bottom-top', 'red-cyan-anaglyph'], value='left-right', type="value", elem_id="stereo_mode")
108107
with gr.Row():
109108
stereo_divergence = gr.Slider(minimum=0.05, maximum=10.005, step=0.01, label='Divergence (3D effect)', value=2.5)
110109
with gr.Row():
@@ -140,10 +139,10 @@ def ui(self, is_img2img):
140139
outputs=[clipthreshold_far]
141140
)
142141

143-
return [compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, gen_stereotb, gen_anaglyph, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, background_removal_model, background_removal, pre_depth_background_removal, save_background_removal_masks, gen_normal]
142+
return [compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, stereo_mode, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, background_removal_model, background_removal, pre_depth_background_removal, save_background_removal_masks, gen_normal]
144143

145144
# run from script in txt2img or img2img
146-
def run(self, p, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, gen_stereotb, gen_anaglyph, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, background_removal_model, background_removal, pre_depth_background_removal, save_background_removal_masks, gen_normal):
145+
def run(self, p, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, stereo_mode, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, background_removal_model, background_removal, pre_depth_background_removal, save_background_removal_masks, gen_normal):
147146

148147
# sd process
149148
processed = processing.process_images(p)
@@ -166,14 +165,14 @@ def run(self, p, compute_device, model_type, net_width, net_height, match_size,
166165
else:
167166
background_removed_images = batched_background_removal(inputimages, background_removal_model)
168167

169-
newmaps, mesh_fi = run_depthmap(processed, p.outpath_samples, inputimages, None, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, gen_stereotb, gen_anaglyph, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, "mp4", 0, background_removal, background_removed_images, save_background_removal_masks, gen_normal)
168+
newmaps, mesh_fi = run_depthmap(processed, p.outpath_samples, inputimages, None, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, stereo_mode, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, "mp4", 0, background_removal, background_removed_images, save_background_removal_masks, gen_normal)
170169

171170
for img in newmaps:
172171
processed.images.append(img)
173172

174173
return processed
175174

176-
def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, gen_stereotb, gen_anaglyph, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, fnExt, vid_ssaa, background_removal, background_removed_images, save_background_removal_masks, gen_normal):
175+
def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, stereo_mode, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, fnExt, vid_ssaa, background_removal, background_removed_images, save_background_removal_masks, gen_normal):
177176

178177
if len(inputimages) == 0 or inputimages[0] == None:
179178
return []
@@ -464,50 +463,22 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
464463
heatmap = (colormap(img_output2[:,:,0] / 256.0) * 2**16).astype(np.uint16)[:,:,:3]
465464
outimages.append(heatmap)
466465

467-
if gen_stereo or gen_stereotb or gen_anaglyph:
468-
print("Generating Stereo image..")
469-
#img_output = cv2.blur(img_output, (3, 3))
470-
balance = (stereo_balance + 1) / 2
471-
original_image = np.asarray(inputimages[count])
472-
left_image = original_image if balance < 0.001 else \
473-
apply_stereo_divergence(original_image, img_output, - stereo_divergence * balance, stereo_fill)
474-
right_image = original_image if balance > 0.999 else \
475-
apply_stereo_divergence(original_image, img_output, stereo_divergence * (1 - balance), stereo_fill)
476-
stereo_img = np.hstack([left_image, right_image])
477-
stereotb_img = np.vstack([left_image, right_image])
478-
479-
# flip sbs left/right if enabled in settings
480-
if hasattr(opts, 'depthmap_script_sbsflip'):
481-
if opts.depthmap_script_sbsflip:
482-
stereo_img = np.hstack([right_image, left_image])
483-
stereotb_img = np.vstack([right_image, left_image])
484-
485-
if gen_stereo:
486-
outimages.append(stereo_img)
487-
if gen_stereotb:
488-
outimages.append(stereotb_img)
489-
if gen_anaglyph:
490-
print("Generating Anaglyph image..")
491-
anaglyph_img = overlap_red_cyan(left_image, right_image)
492-
outimages.append(anaglyph_img)
493-
if (processed is not None):
494-
if gen_stereo:
495-
images.save_image(Image.fromarray(stereo_img), outpath, "", processed.all_seeds[count], processed.all_prompts[count], opts.samples_format, info=info, p=processed, suffix="_stereo")
496-
if gen_stereotb:
497-
images.save_image(Image.fromarray(stereotb_img), outpath, "", processed.all_seeds[count], processed.all_prompts[count], opts.samples_format, info=info, p=processed, suffix="_stereotb")
498-
if gen_anaglyph:
499-
images.save_image(Image.fromarray(anaglyph_img), outpath, "", processed.all_seeds[count], processed.all_prompts[count], opts.samples_format, info=info, p=processed, suffix="_anaglyph")
466+
if gen_stereo:
467+
print("Generating stereoscopic image(s)..")
468+
stereoimage = create_stereoimage(inputimages[count], img_output, stereo_divergence, stereo_mode, stereo_balance, stereo_fill)
469+
outimages.append(stereoimage)
470+
471+
if processed is not None:
472+
images.save_image(stereoimage, outpath, "", processed.all_seeds[count],
473+
processed.all_prompts[count], opts.samples_format, info=info, p=processed,
474+
suffix=f"_{stereo_mode}")
500475
else:
501476
# from tab
502-
if gen_stereo:
503-
images.save_image(Image.fromarray(stereo_img), path=outpath, basename=basename, seed=None, prompt=None, extension=opts.samples_format, info=info, short_filename=True,no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None, forced_filename=None, suffix="_stereo")
504-
if gen_stereotb:
505-
images.save_image(Image.fromarray(stereotb_img), path=outpath, basename=basename, seed=None, prompt=None, extension=opts.samples_format, info=info, short_filename=True,no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None, forced_filename=None, suffix="_stereotb")
506-
if gen_anaglyph:
507-
images.save_image(Image.fromarray(anaglyph_img), path=outpath, basename=basename, seed=None, prompt=None, extension=opts.samples_format, info=info, short_filename=True,no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None, forced_filename=None, suffix="_anaglyph")
477+
images.save_image(stereoimage, path=outpath, basename=basename, seed=None,
478+
prompt=None, extension=opts.samples_format, info=info, short_filename=True,
479+
no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None,
480+
forced_filename=None, suffix=f"_{stereo_mode}")
508481

509-
510-
511482
if gen_normal:
512483
# taken from @graemeniedermayer, hidden, for api use only, will remove in future
513484
# take gradients
@@ -873,9 +844,8 @@ def run_generate(depthmap_mode,
873844
show_heat,
874845
combine_output,
875846
combine_output_axis,
876-
gen_stereo,
877-
gen_stereotb,
878-
gen_anaglyph,
847+
gen_stereo,
848+
stereo_mode,
879849
stereo_divergence,
880850
stereo_fill,
881851
stereo_balance,
@@ -937,16 +907,15 @@ def run_generate(depthmap_mode,
937907
imageArr = batched_background_removal(imageArr, background_removal_model)
938908
background_removed_images = imageArr
939909
else:
940-
background_removed_images = batched_background_removal(imageArr, background_removal_model)
910+
background_removed_images = batched_background_removal(imageArr, background_removal_model)
941911

942-
outputs, mesh_fi = run_depthmap(None, outpath, imageArr, imageNameArr, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, gen_stereotb, gen_anaglyph, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, fnExt, vid_ssaa, background_removal, background_removed_images, save_background_removal_masks, False)
912+
outputs, mesh_fi = run_depthmap(None, outpath, imageArr, imageNameArr, compute_device, model_type, net_width, net_height, match_size, invert_depth, boost, save_depth, show_depth, show_heat, combine_output, combine_output_axis, gen_stereo, stereo_mode, stereo_divergence, stereo_fill, stereo_balance, clipdepth, clipthreshold_far, clipthreshold_near, inpaint, inpaint_vids, fnExt, vid_ssaa, background_removal, background_removed_images, save_background_removal_masks, False)
943913

944914
return outputs, mesh_fi, plaintext_to_html('info'), ''
945915

946916
def on_ui_settings():
947917
section = ('depthmap-script', "Depthmap extension")
948918
shared.opts.add_option("depthmap_script_boost_rmax", shared.OptionInfo(1600, "Maximum wholesize for boost", section=section))
949-
shared.opts.add_option("depthmap_script_sbsflip", shared.OptionInfo(False, "Flip Left/Right in SBS stereo images (requested for cross-eyed viewing)", section=section))
950919

951920
def on_ui_tabs():
952921
with gr.Blocks(analytics_enabled=False) as depthmap_interface:
@@ -994,9 +963,8 @@ def on_ui_tabs():
994963
show_heat = gr.Checkbox(label="Show HeatMap",value=False)
995964
with gr.Group():
996965
with gr.Row():
997-
gen_stereo = gr.Checkbox(label="Generate Stereo side-by-side image",value=False)
998-
gen_stereotb = gr.Checkbox(label="Generate Stereo top-bottom image",value=False)
999-
gen_anaglyph = gr.Checkbox(label="Generate Stereo anaglyph image (red/cyan)",value=False)
966+
gen_stereo = gr.Checkbox(label="Generate stereoscopic image",value=False)
967+
stereo_mode = gr.Dropdown(label="Stereoscopic image type", choices=['left-right', 'right-left', 'top-bottom', 'bottom-top', 'red-cyan-anaglyph'], value='left-right', type="value", elem_id="stereo_mode")
1000968
with gr.Row():
1001969
stereo_divergence = gr.Slider(minimum=0.05, maximum=10.005, step=0.01, label='Divergence (3D effect)', value=2.5)
1002970
with gr.Row():
@@ -1080,9 +1048,8 @@ def on_ui_tabs():
10801048
show_heat,
10811049
combine_output,
10821050
combine_output_axis,
1083-
gen_stereo,
1084-
gen_stereotb,
1085-
gen_anaglyph,
1051+
gen_stereo,
1052+
stereo_mode,
10861053
stereo_divergence,
10871054
stereo_fill,
10881055
stereo_balance,

0 commit comments

Comments
 (0)