61
61
62
62
whole_size_threshold = 1600 # R_max from the paper
63
63
pix2pixsize = 1024
64
- scriptname = "DepthMap v0.3.7 "
64
+ scriptname = "DepthMap v0.3.8 "
65
65
66
66
class Script (scripts .Script ):
67
67
def title (self ):
@@ -123,6 +123,8 @@ def ui(self, is_img2img):
123
123
with gr .Box ():
124
124
gr .HTML ("Information, comment and share @ <a href='https://github.com/thygate/stable-diffusion-webui-depthmap-script'>https://github.com/thygate/stable-diffusion-webui-depthmap-script</a>" )
125
125
126
+ gen_normal = gr .Checkbox (label = "Generate Normalmap (hidden! api only)" ,value = False , visible = False )
127
+
126
128
clipthreshold_far .change (
127
129
fn = lambda a , b : a if b < a else b ,
128
130
inputs = [clipthreshold_far , clipthreshold_near ],
@@ -135,10 +137,10 @@ def ui(self, is_img2img):
135
137
outputs = [clipthreshold_far ]
136
138
)
137
139
138
- 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_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 ]
140
+ 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_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 ]
139
141
140
142
# run from script in txt2img or img2img
141
- 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_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 ):
143
+ 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_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
144
143
145
# sd process
144
146
processed = processing .process_images (p )
@@ -161,14 +163,14 @@ def run(self, p, compute_device, model_type, net_width, net_height, match_size,
161
163
else :
162
164
background_removed_images = batched_background_removal (inputimages , background_removal_model )
163
165
164
- 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_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 )
166
+ 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_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 )
165
167
166
168
for img in newmaps :
167
169
processed .images .append (img )
168
170
169
171
return processed
170
172
171
- 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_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 ):
173
+ 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_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 ):
172
174
173
175
if len (inputimages ) == 0 or inputimages [0 ] == None :
174
176
return []
@@ -414,13 +416,19 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
414
416
img_output [bg_mask ] = far_value * far_value #255*255 or 0*0
415
417
416
418
#should this be optional
417
- images .save_image (background_removed_image , path = outpath , basename = 'depthmap' , 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 = "_background_removed" )
419
+ if (processed is not None ):
420
+ images .save_image (background_removed_image , outpath , "" , processed .all_seeds [count ], processed .all_prompts [count ], opts .samples_format , info = info , p = processed , suffix = "_background_removed" )
421
+ else :
422
+ images .save_image (background_removed_image , 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 = "_background_removed" )
418
423
outimages .append (background_removed_image )
419
424
if save_background_removal_masks :
420
425
bg_array = (1 - bg_mask .astype ('int8' ))* 255
421
426
mask_array = np .stack ( (bg_array , bg_array , bg_array , bg_array ), axis = 2 )
422
427
mask_image = Image .fromarray ( mask_array .astype (np .uint8 ))
423
- images .save_image (mask_image , path = outpath , basename = 'depthmap' , 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 = "_foreground_mask" )
428
+ if (processed is not None ):
429
+ images .save_image (mask_image , outpath , "" , processed .all_seeds [count ], processed .all_prompts [count ], opts .samples_format , info = info , p = processed , suffix = "_foreground_mask" )
430
+ else :
431
+ images .save_image (mask_image , 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 = "_foreground_mask" )
424
432
outimages .append (mask_image )
425
433
426
434
if not combine_output :
@@ -487,6 +495,31 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
487
495
if gen_anaglyph :
488
496
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" )
489
497
498
+
499
+
500
+ if gen_normal :
501
+ # taken from @graemeniedermayer, hidden, for api use only, will remove in future
502
+ # take gradients
503
+ zx = cv2 .Sobel (np .float64 (img_output ), cv2 .CV_64F , 1 , 0 , ksize = 3 )
504
+ zy = cv2 .Sobel (np .float64 (img_output ), cv2 .CV_64F , 0 , 1 , ksize = 3 )
505
+
506
+ # combine and normalize gradients.
507
+ normal = np .dstack ((zx , - zy , np .ones_like (img_output )))
508
+ n = np .linalg .norm (normal , axis = 2 )
509
+ normal [:, :, 0 ] /= n
510
+ normal [:, :, 1 ] /= n
511
+ normal [:, :, 2 ] /= n
512
+
513
+ # offset and rescale values to be in 0-255
514
+ normal += 1
515
+ normal /= 2
516
+ normal *= 255
517
+ normal = normal .astype (np .uint8 )
518
+
519
+ outimages .append (Image .fromarray (normal ))
520
+
521
+
522
+
490
523
print ("Done." )
491
524
492
525
except RuntimeError as e :
@@ -1110,7 +1143,7 @@ def run_generate(depthmap_mode,
1110
1143
else :
1111
1144
background_removed_images = batched_background_removal (imageArr , background_removal_model )
1112
1145
1113
- 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_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 )
1146
+ 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_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 )
1114
1147
1115
1148
return outputs , mesh_fi , plaintext_to_html ('info' ), ''
1116
1149
0 commit comments