Skip to content

Commit c98cfa7

Browse files
committed
Move background_removed_images code from run_depthmap wrappers to run_depthmap
Large refactor part, may be broken
1 parent cd1d73e commit c98cfa7

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

scripts/depthmap.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,8 @@ def run(self, p, *inputs):
340340
continue
341341
inputimages.append(processed.images[count])
342342

343-
# TODO: this should not be here
344-
# remove on base image before depth calculation
345-
background_removed_images = []
346-
if inp['background_removal']:
347-
if inp['pre_depth_background_removal']:
348-
inputimages = batched_background_removal(inputimages, inp['background_removal_model'])
349-
background_removed_images = inputimages
350-
else:
351-
background_removed_images = batched_background_removal(inputimages, inp['background_removal_model'])
352-
353-
newmaps, mesh_fi, meshsimple_fi = run_depthmap(processed, p.outpath_samples, inputimages, None, inp,
354-
background_removed_images)
355-
for img in newmaps:
343+
outimages, mesh_fi, meshsimple_fi = run_depthmap(processed, p.outpath_samples, inputimages, None, inp)
344+
for img in outimages:
356345
processed.images.append(img)
357346

358347
return processed
@@ -370,7 +359,7 @@ def reload_sd_model():
370359
shared.sd_model.first_stage_model.to(devices.device)
371360

372361

373-
def run_depthmap(processed, outpath, inputimages, inputnames, inp, background_removed_images):
362+
def run_depthmap(processed, outpath, inputimages, inputnames, inp):
374363
if len(inputimages) == 0 or inputimages[0] is None:
375364
return [], []
376365

@@ -410,10 +399,22 @@ def run_depthmap(processed, outpath, inputimages, inputnames, inp, background_re
410399
custom_depthmap_img = inp["custom_depthmap_img"] if "custom_depthmap_img" in inp else None
411400
depthmap_batch_reuse = inp["depthmap_batch_reuse"] if "depthmap_batch_reuse" in inp else True
412401

402+
# TODO: run_depthmap should not save outputs nor assign filenames, it should be done by a wrapper.
403+
# Rationale: allowing webui-independent (stand-alone) wrapers
413404
print(f"\n{scriptname} {scriptversion} ({get_commit_hash()})")
414405

415406
unload_sd_model()
416407

408+
# TODO: this still should not be here
409+
background_removed_images = []
410+
# remove on base image before depth calculation
411+
if background_removal:
412+
if pre_depth_background_removal:
413+
inputimages = batched_background_removal(inputimages, background_removal_model)
414+
background_removed_images = inputimages
415+
else:
416+
background_removed_images = batched_background_removal(inputimages, background_removal_model)
417+
417418
meshsimple_fi = None
418419
mesh_fi = None
419420

@@ -422,8 +423,11 @@ def run_depthmap(processed, outpath, inputimages, inputnames, inp, background_re
422423

423424
# init torch device
424425
global device
426+
if depthmap_compute_device == 'GPU' and not torch.cuda.is_available():
427+
print('WARNING: Cuda device was not found, cpu will be used')
428+
depthmap_compute_device = 'CPU'
425429
if depthmap_compute_device == 'GPU':
426-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
430+
device = torch.device("cuda")
427431
else:
428432
device = torch.device("cpu")
429433
print("device: %s" % device)
@@ -831,22 +835,21 @@ def run_depthmap(processed, outpath, inputimages, inputnames, inp, background_re
831835
if gen_stereo:
832836
print("Generating stereoscopic images..")
833837

834-
stereomodes = stereo_modes
835838
stereoimages = create_stereoimages(inputimages[count], img_output, stereo_divergence, stereo_separation,
836-
stereomodes, stereo_balance, stereo_fill)
839+
stereo_modes, stereo_balance, stereo_fill)
837840

838841
for c in range(0, len(stereoimages)):
839842
outimages.append(stereoimages[c])
840843
if processed is not None:
841844
images.save_image(stereoimages[c], outpath, "", processed.all_seeds[count],
842845
processed.all_prompts[count], opts.samples_format, info=info, p=processed,
843-
suffix=f"_{stereomodes[c]}")
846+
suffix=f"_{stereo_modes[c]}")
844847
else:
845848
# from tab
846849
images.save_image(stereoimages[c], path=outpath, basename=basename, seed=None,
847850
prompt=None, extension=opts.samples_format, info=info, short_filename=True,
848851
no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None,
849-
forced_filename=None, suffix=f"_{stereomodes[c]}")
852+
forced_filename=None, suffix=f"_{stereo_modes[c]}")
850853

851854
if gen_normal:
852855
# taken from @graemeniedermayer, hidden, for api use only, will remove in future.
@@ -1254,30 +1257,30 @@ def run_generate(*inputs):
12541257
depthmap_input_image = inputs['depthmap_input_image']
12551258
depthmap_batch_output_dir = inputs['depthmap_batch_output_dir']
12561259

1257-
imageArr = []
1260+
inputimages = []
12581261
# Also keep track of original file names
1259-
imageNameArr = []
1260-
outputs = []
1262+
inputnames = []
1263+
outimages = []
12611264

12621265
if depthmap_mode == '0': # Single image
1263-
imageArr.append(depthmap_input_image)
1264-
imageNameArr.append(None)
1266+
inputimages.append(depthmap_input_image)
1267+
inputnames.append(None)
12651268
if depthmap_mode == '1': # Batch Process
12661269
# convert files to pillow images
12671270
for img in image_batch:
12681271
image = Image.open(os.path.abspath(img.name))
1269-
imageArr.append(image)
1270-
imageNameArr.append(os.path.splitext(img.orig_name)[0])
1272+
inputimages.append(image)
1273+
inputnames.append(os.path.splitext(img.orig_name)[0])
12711274
elif depthmap_mode == '2': # Batch from Directory
12721275
assert not shared.cmd_opts.hide_ui_dir_config, '--hide-ui-dir-config option must be disabled'
12731276
if depthmap_batch_input_dir == '':
1274-
return outputs, "Please select an input directory.", ''
1277+
return outimages, "Please select an input directory.", ''
12751278
image_list = shared.listfiles(depthmap_batch_input_dir)
12761279
for img in image_list:
12771280
try:
12781281
image = Image.open(img)
1279-
imageArr.append(image)
1280-
imageNameArr.append(img)
1282+
inputimages.append(image)
1283+
inputnames.append(img)
12811284
except Exception:
12821285
print(f'Failed to load {img}, ignoring.')
12831286

@@ -1286,26 +1289,16 @@ def run_generate(*inputs):
12861289
else:
12871290
outpath = opts.outdir_samples or opts.outdir_extras_samples
12881291

1289-
# TODO: this should not be here
1290-
background_removed_images = []
1291-
if inputs['background_removal']:
1292-
if inputs['pre_depth_background_removal']:
1293-
imageArr = batched_background_removal(imageArr, inputs['background_removal_model'])
1294-
background_removed_images = imageArr
1295-
else:
1296-
background_removed_images = batched_background_removal(imageArr, inputs['background_removal_model'])
1297-
outputs, mesh_fi, meshsimple_fi = run_depthmap(None, outpath, imageArr, imageNameArr, inputs,
1298-
background_removed_images)
1292+
outimages, mesh_fi, meshsimple_fi = run_depthmap(None, outpath, inputimages, inputnames, inputs)
12991293

13001294
# use inpainted 3d mesh to show in 3d model output when enabled in settings
13011295
if hasattr(opts, 'depthmap_script_show_3d_inpaint') and opts.depthmap_script_show_3d_inpaint and mesh_fi != None and len(mesh_fi) > 0:
13021296
meshsimple_fi = mesh_fi
1303-
1304-
# don't show 3dmodel when disabled in settings
1297+
# however, don't show 3dmodel when disabled in settings
13051298
if hasattr(opts, 'depthmap_script_show_3d') and not opts.depthmap_script_show_3d:
13061299
meshsimple_fi = None
13071300

1308-
return outputs, mesh_fi, meshsimple_fi, plaintext_to_html('info'), ''
1301+
return outimages, mesh_fi, meshsimple_fi, plaintext_to_html('info'), ''
13091302

13101303

13111304
def unload_models():

0 commit comments

Comments
 (0)