6161
6262whole_size_threshold = 1600 # R_max from the paper
6363pix2pixsize = 1024
64- scriptname = "DepthMap v0.3.7 "
64+ scriptname = "DepthMap v0.3.8 "
6565
6666class Script (scripts .Script ):
6767 def title (self ):
@@ -123,6 +123,8 @@ def ui(self, is_img2img):
123123 with gr .Box ():
124124 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>" )
125125
126+ gen_normal = gr .Checkbox (label = "Generate Normalmap (hidden! api only)" ,value = False , visible = False )
127+
126128 clipthreshold_far .change (
127129 fn = lambda a , b : a if b < a else b ,
128130 inputs = [clipthreshold_far , clipthreshold_near ],
@@ -135,10 +137,10 @@ def ui(self, is_img2img):
135137 outputs = [clipthreshold_far ]
136138 )
137139
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 ]
139141
140142 # 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 ):
142144
143145 # sd process
144146 processed = processing .process_images (p )
@@ -161,14 +163,14 @@ def run(self, p, compute_device, model_type, net_width, net_height, match_size,
161163 else :
162164 background_removed_images = batched_background_removal (inputimages , background_removal_model )
163165
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 )
165167
166168 for img in newmaps :
167169 processed .images .append (img )
168170
169171 return processed
170172
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 ):
172174
173175 if len (inputimages ) == 0 or inputimages [0 ] == None :
174176 return []
@@ -414,13 +416,19 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
414416 img_output [bg_mask ] = far_value * far_value #255*255 or 0*0
415417
416418 #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" )
418423 outimages .append (background_removed_image )
419424 if save_background_removal_masks :
420425 bg_array = (1 - bg_mask .astype ('int8' ))* 255
421426 mask_array = np .stack ( (bg_array , bg_array , bg_array , bg_array ), axis = 2 )
422427 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" )
424432 outimages .append (mask_image )
425433
426434 if not combine_output :
@@ -487,6 +495,31 @@ def run_depthmap(processed, outpath, inputimages, inputnames, compute_device, mo
487495 if gen_anaglyph :
488496 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" )
489497
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+
490523 print ("Done." )
491524
492525 except RuntimeError as e :
@@ -1110,7 +1143,7 @@ def run_generate(depthmap_mode,
11101143 else :
11111144 background_removed_images = batched_background_removal (imageArr , background_removal_model )
11121145
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 )
11141147
11151148 return outputs , mesh_fi , plaintext_to_html ('info' ), ''
11161149
0 commit comments