Skip to content

Commit 92cf04a

Browse files
committed
Minor cleanup
1 parent 3f7439c commit 92cf04a

File tree

3 files changed

+24
-39
lines changed

3 files changed

+24
-39
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# High Resolution Depth Maps for Stable Diffusion WebUI
2-
This program is an addon for [AUTOMATIC1111's Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) that creates depth maps. Using either generated or custom depth maps, it can also create 3D stereo image pairs (as side-by-side or anaglyph), normalmaps and 3D meshes. The outputs of the script can be viewed directly or used as an asset for a 3D engine. Please see [wiki](https://github.com/thygate/stable-diffusion-webui-depthmap-script/wiki/Viewing-Results) to learn more. The program has integration with [Rembg](https://github.com/danielgatis/rembg). It also supports batch processing, processing of videos, and can also be run in standalone mode, without Stable Diffusion WebUI.
2+
This program is an addon for [AUTOMATIC1111's Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) that creates depth maps. Using either generated or custom depth maps, it can also create 3D stereo image pairs (side-by-side or anaglyph), normalmaps and 3D meshes. The outputs of the script can be viewed directly or used as an asset for a 3D engine. Please see [wiki](https://github.com/thygate/stable-diffusion-webui-depthmap-script/wiki/Viewing-Results) to learn more. The program has integration with [Rembg](https://github.com/danielgatis/rembg). It also supports batch processing, processing of videos, and can also be run in standalone mode, without Stable Diffusion WebUI.
33

44
To generate realistic depth maps from individual images, this script uses code and models from the [MiDaS](https://github.com/isl-org/MiDaS) and [ZoeDepth](https://github.com/isl-org/ZoeDepth) repositories by Intel ISL, or LeReS from the [AdelaiDepth](https://github.com/aim-uofa/AdelaiDepth) repository by Advanced Intelligent Machines. Multi-resolution merging as implemented by [BoostingMonocularDepth](https://github.com/compphoto/BoostingMonocularDepth) is used to generate high resolution depth maps.
55

@@ -22,15 +22,15 @@ images generated by [@semjon00](https://github.com/semjon00) from CC0 photos, mo
2222

2323
## Install instructions
2424
### As extension
25-
The script can be installed directly from WebUI. Please navigate to `Extensions` tab, then click `Available`, `Load from` and then install the `Depth Maps` extension. Alternatively, the extension can be installed from URL: `https://github.com/thygate/stable-diffusion-webui-depthmap-script`.
25+
The script can be installed directly from WebUI. Please navigate to `Extensions` tab, then click `Available`, `Load from` and then install the `Depth Maps` extension. Alternatively, the extension can be installed from the URL: `https://github.com/thygate/stable-diffusion-webui-depthmap-script`.
2626

2727
### Updating
2828
In the WebUI, in the `Extensions` tab, in the `Installed` subtab, click `Check for Updates` and then `Apply and restart UI`.
2929

3030
### Standalone
3131
Clone the repository, install the requirements from `requirements.txt`, launch using `main.py`.
3232

33-
>Model weights will be downloaded automatically on their first use and saved to /models/midas, /models/leres and /models/pix2pix. Zoedepth models are stored in torch cache folder.
33+
>Model weights will be downloaded automatically on their first use and saved to /models/midas, /models/leres and /models/pix2pix. Zoedepth models are stored in the torch cache folder.
3434
3535

3636
## Usage
@@ -43,7 +43,7 @@ There are ten models available from the `Model` dropdown. For the first model, r
4343

4444
Net size can be set with `net width` and `net height`, or will be the same as the input image when `Match input size` is enabled. There is a trade-off between structural consistency and high-frequency details with respect to net size (see [observations](https://github.com/compphoto/BoostingMonocularDepth#observations)).
4545

46-
`Boost` will enable multi-resolution merging as implemented by [BoostingMonocularDepth](https://github.com/compphoto/BoostingMonocularDepth) and will significantly improve the results, mitigating the observations mentioned above, and the cost of much larger compute time. Best results with res101.
46+
`Boost` will enable multi-resolution merging as implemented by [BoostingMonocularDepth](https://github.com/compphoto/BoostingMonocularDepth) and will significantly improve the results, mitigating the observations mentioned above, at the cost of much larger compute time. Best results with res101.
4747

4848
`Clip and renormalize` allows for clipping the depthmap on the `near` and `far` side, the values in between will be renormalized to fit the available range. Set both values equal to get a b&w mask of a single depth plane at that value. This option works on the 16-bit depthmap and allows for 1000 steps to select the clip values.
4949

@@ -76,17 +76,17 @@ If you often get out of memory errors when computing a depthmap on GPU while usi
7676
* `Can I use this on existing images ?`
7777
- Yes, you can use the Depth tab to easily process existing images.
7878
- Another way of doing this would be to use img2img with denoising strength to 0. This will effectively skip stable diffusion and use the input image. You will still have to set the correct size, and need to select `Crop and resize` instead of `Just resize` when the input image resolution does not match the set size perfectly.
79-
* `Can I run this on Google Colab ?`
79+
* `Can I run this on Google Colab?`
8080
- You can run the MiDaS network on their colab linked here https://pytorch.org/hub/intelisl_midas_v2/
8181
- You can run BoostingMonocularDepth on their colab linked here : https://colab.research.google.com/github/compphoto/BoostingMonocularDepth/blob/main/Boostmonoculardepth.ipynb
8282
- Running this program on Colab is not officially supported, but it may work. Please look for more suitable ways of running this. If you still decide to try, standalone installation may be easier to manage.
8383
* `What other depth-related projects could I check out?`
8484
- Several [scripts](https://github.com/Extraltodeus?tab=repositories) by [@Extraltodeus](https://github.com/Extraltodeus) using depth maps.
85-
- Geo11 and [Depth3D](https://github.com/BlueSkyDefender/Depth3D) for playing existing games in 3D.
85+
- geo-11 and [Depth3D](https://github.com/BlueSkyDefender/Depth3D) for playing existing games in 3D.
8686
* `How can I know what changed in the new version of the script?`
8787
- You can see the git history log or refer to the `CHANGELOG.md` file.
8888

89-
### Feel free to comment and share in the discussions!
89+
### Feel free to comment and share in the discussions! Submitting issues and merge requests is heavilly appreciated!
9090

9191
## Acknowledgements
9292

src/common_ui.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def main_ui_panel(is_depth_tab):
122122
inp += go.GEN_SIMPLE_MESH, gr.Checkbox(label="Generate simple 3D mesh")
123123
with gr.Column(visible=False) as mesh_options:
124124
with gr.Row():
125-
gr.HTML(value="Generates fast, accurate only with ZoeDepth models and no boost, no custom maps")
125+
gr.HTML(value="Generates fast, accurate only with ZoeDepth models and no boost, no custom maps.")
126126
with gr.Row():
127127
inp += go.SIMPLE_MESH_OCCLUDE, gr.Checkbox(label="Remove occluded edges")
128128
inp += go.SIMPLE_MESH_SPHERICAL, gr.Checkbox(label="Equirectangular projection")
@@ -133,10 +133,10 @@ def main_ui_panel(is_depth_tab):
133133
inp += go.GEN_INPAINTED_MESH, gr.Checkbox(
134134
label="Generate 3D inpainted mesh")
135135
with gr.Column(visible=False) as inpaint_options_row_0:
136-
gr.HTML("Generation is sloooow, required for generating videos")
136+
gr.HTML("Generation is sloooow. Required for generating videos from mesh.")
137137
inp += go.GEN_INPAINTED_MESH_DEMOS, gr.Checkbox(
138138
label="Generate 4 demo videos with 3D inpainted mesh.")
139-
gr.HTML("More options for generating video can be found in the Generate video tab")
139+
gr.HTML("More options for generating video can be found in the Generate video tab.")
140140

141141
with gr.Box():
142142
# TODO: it should be clear from the UI that there is an option of the background removal
@@ -184,12 +184,14 @@ def update_default_net_size(model_type):
184184
inp[go.CLIPDEPTH_FAR].change(
185185
fn=lambda a, b: a if b < a else b,
186186
inputs=[inp[go.CLIPDEPTH_FAR], inp[go.CLIPDEPTH_NEAR]],
187-
outputs=[inp[go.CLIPDEPTH_NEAR]]
187+
outputs=[inp[go.CLIPDEPTH_NEAR]],
188+
show_progress=False
188189
)
189190
inp[go.CLIPDEPTH_NEAR].change(
190191
fn=lambda a, b: a if b > a else b,
191192
inputs=[inp[go.CLIPDEPTH_NEAR], inp[go.CLIPDEPTH_FAR]],
192-
outputs=[inp[go.CLIPDEPTH_FAR]]
193+
outputs=[inp[go.CLIPDEPTH_FAR]],
194+
show_progress=False
193195
)
194196

195197
inp.add_rule(stereo_options, 'visible-if', go.GEN_STEREO)
@@ -558,7 +560,7 @@ def run_generate(*inputs):
558560

559561
# Deciding what mesh to display (and if)
560562
display_mesh_fi = None
561-
if not backbone.get_opt('depthmap_script_show_3d', True):
563+
if backbone.get_opt('depthmap_script_show_3d', True):
562564
display_mesh_fi = mesh_simple_fi
563565
if backbone.get_opt('depthmap_script_show_3d_inpaint', True):
564566
if inpainted_mesh_fi is not None and len(inpainted_mesh_fi) > 0:

src/core.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
125125
if not inputdepthmaps_complete:
126126
print("Loading model(s) ..")
127127
model_holder.ensure_models(inp[go.MODEL_TYPE], device, inp[go.BOOST])
128-
model = model_holder.depth_model
129-
pix2pix_model = model_holder.pix2pix_model
130-
131128
print("Computing output(s) ..")
132129
# iterate over input images
133130
for count in trange(0, len(inputimages)):
@@ -231,25 +228,17 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
231228
yield count, 'foreground_mask', mask_image
232229

233230
# A weird quirk: if user tries to save depthmap, whereas custom depthmap is used,
234-
# depthmap will not be outputed, even if output_depth_combine is used.
231+
# custom depthmap will be outputed
235232
if inp[go.DO_OUTPUT_DEPTH]:
236-
if inputdepthmaps[count] is None:
237-
img_depth = cv2.bitwise_not(img_output) if inp[go.OUTPUT_DEPTH_INVERT] else img_output
238-
if inp[go.OUTPUT_DEPTH_COMBINE]:
239-
axis = 1 if inp[go.OUTPUT_DEPTH_COMBINE_AXIS] == 'Horizontal' else 0
240-
img_concat = Image.fromarray(np.concatenate(
241-
(inputimages[count], convert_i16_to_rgb(img_depth, inputimages[count])),
242-
axis=axis))
243-
yield count, 'concat_depth', img_concat
244-
else:
245-
yield count, 'depth', Image.fromarray(img_depth)
233+
img_depth = cv2.bitwise_not(img_output) if inp[go.OUTPUT_DEPTH_INVERT] else img_output
234+
if inp[go.OUTPUT_DEPTH_COMBINE]:
235+
axis = 1 if inp[go.OUTPUT_DEPTH_COMBINE_AXIS] == 'Horizontal' else 0
236+
img_concat = Image.fromarray(np.concatenate(
237+
(inputimages[count], convert_i16_to_rgb(img_depth, inputimages[count])),
238+
axis=axis))
239+
yield count, 'concat_depth', img_concat
246240
else:
247-
# TODO: make it better
248-
# Yes, this seems stupid, but this is, logically, what should happen -
249-
# and this improves clarity of some other code.
250-
# But we won't return it if there is only one image.
251-
if len(inputimages) > 1:
252-
yield count, 'depth', Image.fromarray(img_output)
241+
yield count, 'depth', Image.fromarray(img_depth)
253242

254243
if inp[go.GEN_STEREO]:
255244
# print("Generating stereoscopic image(s)..")
@@ -335,17 +324,11 @@ def core_generation_funnel(outpath, inputimages, inputdepthmaps, inputnames, inp
335324
if backbone.get_opt('depthmap_script_keepmodels', True):
336325
model_holder.offload() # Swap to CPU memory
337326
else:
338-
if 'model' in locals():
339-
del model
340-
if 'pix2pixmodel' in locals():
341-
del pix2pix_model
342327
model_holder.unload_models()
343-
344328
gc.collect()
345329
backbone.torch_gc()
346330

347331
# TODO: This should not be here
348-
mesh_fi = None
349332
if inp[go.GEN_INPAINTED_MESH]:
350333
try:
351334
mesh_fi = run_3dphoto(device, inpaint_imgs, inpaint_depths, inputnames, outpath,

0 commit comments

Comments
 (0)