Skip to content

Add n-wide side-by-side stereo images #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions scripts/depthmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ def ui(self, is_img2img):
with gr.Group():
with gr.Row():
gen_stereo = gr.Checkbox(label="Generate Stereo side-by-side image",value=False)
gen_stereo_count = gr.Slider(minimum=2, maximum=10, step=2, label="Side-by-side image count", value=2)
gen_stereotb = gr.Checkbox(label="Generate Stereo top-bottom image",value=False)
gen_anaglyph = gr.Checkbox(label="Generate Stereo anaglyph image (red/cyan)",value=False)
with gr.Row():
stereo_divergence = gr.Slider(minimum=0.05, maximum=10.005, step=0.01, label='Divergence (3D effect)', value=2.5)
stereo_divergence = gr.Slider(minimum=0.05, maximum=100.005, step=0.01, label='Divergence (3D effect)', value=2.5)
with gr.Row():
stereo_fill = gr.Dropdown(label="Gap fill technique", choices=['none', 'naive', 'naive_interpolating', 'polylines_soft', 'polylines_sharp'], value='polylines_sharp', type="index", elem_id="stereo_fill_type")
stereo_balance = gr.Slider(minimum=-1.0, maximum=1.0, step=0.05, label='Balance between eyes', value=0.0)
Expand Down Expand Up @@ -138,10 +139,10 @@ def ui(self, is_img2img):
outputs=[clipthreshold_far]
)

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]
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_stereo_count, 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]

# run from script in txt2img or img2img
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):
# run from script in txt2img or img2img
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_stereo_count, 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):

# sd process
processed = processing.process_images(p)
Expand All @@ -164,14 +165,14 @@ def run(self, p, compute_device, model_type, net_width, net_height, match_size,
else:
background_removed_images = batched_background_removal(inputimages, background_removal_model)

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)
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_stereo_count, 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)

for img in newmaps:
processed.images.append(img)

return processed

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):
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_stereo_count, 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):

if len(inputimages) == 0 or inputimages[0] == None:
return []
Expand Down Expand Up @@ -462,23 +463,30 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
heatmap = (colormap(img_output2[:,:,0] / 256.0) * 2**16).astype(np.uint16)[:,:,:3]
outimages.append(heatmap)

if gen_stereo or gen_stereotb or gen_anaglyph:
print("Generating Stereo image..")
if gen_stereo or gen_anaglyph or gen_stereotb:
print("Generating Stereo image with "+str(gen_stereo_count)+" images..")
#img_output = cv2.blur(img_output, (3, 3))
balance = (stereo_balance + 1) / 2
original_image = np.asarray(inputimages[count])
left_image = original_image if balance < 0.001 else \
apply_stereo_divergence(original_image, img_output, - stereo_divergence * balance, stereo_fill)
right_image = original_image if balance > 0.999 else \
apply_stereo_divergence(original_image, img_output, stereo_divergence * (1 - balance), stereo_fill)
stereo_img = np.hstack([left_image, right_image])

img_array = []

# Make the stereogram iteratively
for i in np.linspace(-1,1,gen_stereo_count):
img_array.append(apply_stereo_divergence(original_image, img_output, (i) * stereo_divergence * (1.0/gen_stereo_count), stereo_fill))

# Keep the L/R generation for anaglyph
left_image = apply_stereo_divergence(original_image, img_output, - stereo_divergence * balance, stereo_fill)
right_image = apply_stereo_divergence(original_image, img_output, stereo_divergence * 1-balance, stereo_fill)

stereo_img = np.hstack(img_array)
stereotb_img = np.vstack([left_image, right_image])

# flip sbs left/right if enabled in settings
if hasattr(opts, 'depthmap_script_sbsflip'):
if opts.depthmap_script_sbsflip:
stereo_img = np.hstack([right_image, left_image])
stereotb_img = np.vstack([right_image, left_image])
stereo_img = np.hstack(img_array.reverse)
stereotb_img = np.vstack(img_array.reverse)

if gen_stereo:
outimages.append(stereo_img)
Expand Down Expand Up @@ -1090,6 +1098,7 @@ def run_generate(depthmap_mode,
combine_output,
combine_output_axis,
gen_stereo,
gen_stereo_count,
gen_stereotb,
gen_anaglyph,
stereo_divergence,
Expand Down Expand Up @@ -1155,7 +1164,7 @@ def run_generate(depthmap_mode,
else:
background_removed_images = batched_background_removal(imageArr, background_removal_model)

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)
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_stereo_count, 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)

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

Expand Down Expand Up @@ -1211,10 +1220,11 @@ def on_ui_tabs():
with gr.Group():
with gr.Row():
gen_stereo = gr.Checkbox(label="Generate Stereo side-by-side image",value=False)
gen_stereo_count = gr.Slider(minimum=2, maximum=10, step=2, label="Side-by-side image count", value=2)
gen_stereotb = gr.Checkbox(label="Generate Stereo top-bottom image",value=False)
gen_anaglyph = gr.Checkbox(label="Generate Stereo anaglyph image (red/cyan)",value=False)
with gr.Row():
stereo_divergence = gr.Slider(minimum=0.05, maximum=10.005, step=0.01, label='Divergence (3D effect)', value=2.5)
stereo_divergence = gr.Slider(minimum=0.05, maximum=100.005, step=0.01, label='Divergence (3D effect)', value=2.5)
with gr.Row():
stereo_fill = gr.Dropdown(label="Gap fill technique", choices=['none', 'naive', 'naive_interpolating', 'polylines_soft', 'polylines_sharp'], value='polylines_sharp', type="index", elem_id="stereo_fill_type")
stereo_balance = gr.Slider(minimum=-1.0, maximum=1.0, step=0.05, label='Balance between eyes', value=0.0)
Expand Down Expand Up @@ -1297,6 +1307,7 @@ def on_ui_tabs():
combine_output,
combine_output_axis,
gen_stereo,
gen_stereo_count,
gen_stereotb,
gen_anaglyph,
stereo_divergence,
Expand Down