Skip to content

Fixes #233

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

Merged
merged 2 commits into from
May 30, 2023
Merged

Fixes #233

Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def ensure(module_name, min_version=None):
if launch.is_installed(module_name):
if min_version is None or Version(importlib_metadata.version(module_name)) >= Version(min_version):
return
cmd = f'install "{module_name}>={min_version}"' if min_version is not None else f'install {module_name}'
msg = f'{module_name} {min_version + " " if min_version is not None else ""}requirement for depthmap script'
requirement = f'{module_name}>={min_version}' if min_version is not None else module_name
cmd = f'install "{requirement}"'
msg = f'{requirement} requirement for depthmap script'
launch.run_pip(cmd, msg)

if not launch.is_installed("timm"): #0.6.7
if not launch.is_installed("timm"): #0.6.7 # For midas
launch.run_pip('install --force-reinstall "timm==0.6.12"', "timm requirement for depthmap script")

ensure('matplotlib')
Expand Down
37 changes: 25 additions & 12 deletions scripts/depthmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import vispy
import trimesh
import math
import subprocess

sys.path.append('extensions/stable-diffusion-webui-depthmap-script/scripts')

Expand Down Expand Up @@ -84,6 +85,15 @@
depthmap_model_type = None
depthmap_deviceidx = None

def get_commit_hash():
try:
hash = subprocess.check_output([os.environ.get('GIT', "git"), "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
hash = hash[0:8]
return hash
except Exception:
return "<none>"
commit_hash = get_commit_hash()

def main_ui_panel(is_depth_tab):
with gr.Blocks():
with gr.Row():
Expand Down Expand Up @@ -296,7 +306,7 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
if len(inputimages) == 0 or inputimages[0] == None:
return [], []

print(f"\n{scriptname} {scriptversion}")
print(f"\n{scriptname} {scriptversion} ({commit_hash})")

# unload sd model
shared.sd_model.cond_stage_model.to(devices.cpu)
Expand Down Expand Up @@ -510,17 +520,22 @@ def run_depthmap(processed, outpath, inputimages, inputnames,

# filename
basename = 'depthmap'
batchdepthfn = None
# filenames in batch mode
if inputnames is not None:

# figuring out the name of custom DepthMap
custom_depthmap_fn = None # None means that DepthMap should be computed
# find filename if in the single image mode
if custom_depthmap and custom_depthmap_img is not None:
custom_depthmap_fn = custom_depthmap_img.name
# find filename if in batch mode
if inputnames is not None and depthmap_batch_reuse:
save_depth = True
if inputnames[count] is not None:
p = Path(inputnames[count])
basename = p.stem
if depthmap_batch_reuse and outpath != opts.outdir_extras_samples:
batchdepthfn = os.path.join(outpath, basename + '-0000.' + opts.samples_format)
if not os.path.isfile(batchdepthfn):
batchdepthfn = None
if outpath != opts.outdir_extras_samples:
custom_depthmap_fn = os.path.join(outpath, basename + '-0000.' + opts.samples_format)
if not os.path.isfile(custom_depthmap_fn):
custom_depthmap_fn = None

# override net size
if (match_size):
Expand All @@ -535,11 +550,9 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
img = cv2.cvtColor(np.asarray(inputimages[count]), cv2.COLOR_BGR2RGB) / 255.0

skipInvertAndSave = False
if (custom_depthmap and custom_depthmap_img != None) or batchdepthfn != None:
if batchdepthfn != None:
custom_depthmap_img = batchdepthfn
if custom_depthmap_fn is not None:
# use custom depthmap
dimg = Image.open(os.path.abspath(custom_depthmap_img.name))
dimg = Image.open(os.path.abspath(custom_depthmap_fn))
# resize if not same size as input
if dimg.width != inputimages[count].width or dimg.height != inputimages[count].height:
dimg = dimg.resize((inputimages[count].width, inputimages[count].height), Image.Resampling.LANCZOS)
Expand Down