Skip to content

Commit e20abe9

Browse files
committed
Minor fixes
1 parent 1065ec1 commit e20abe9

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

scripts/depthmap.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@
3737
import os
3838
import math
3939
import subprocess
40-
40+
import traceback
4141
import pathlib
4242
import os
43-
script_dir = os.path.dirname(os.path.realpath(__file__))
44-
extension_dir = pathlib.Path(script_dir).parent
45-
sys.path.append(extension_dir)
43+
44+
# Not sure if this is needed
45+
try:
46+
script_dir = os.path.dirname(os.path.realpath(__file__))
47+
extension_dir = pathlib.Path(script_dir).parent
48+
sys.path.append(extension_dir)
49+
except:
50+
sys.path.append('extensions/stable-diffusion-webui-depthmap-script')
4651

4752

4853
from scripts.stereoimage_generation import create_stereoimages
@@ -91,14 +96,21 @@
9196
depthmap_model_type = None
9297
depthmap_deviceidx = None
9398

99+
commit_hash = None # TODO: understand why it would spam to stderr if changed to ... = get_commit_hash()
94100
def get_commit_hash():
95-
try:
96-
hash = subprocess.check_output([os.environ.get('GIT', "git"), "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
97-
hash = hash[0:8]
98-
return hash
99-
except Exception:
100-
return "<none>"
101-
commit_hash = get_commit_hash()
101+
global commit_hash
102+
if commit_hash is None:
103+
try:
104+
commit_hash = subprocess.check_output(
105+
[os.environ.get('GIT', "git"), "rev-parse", "HEAD"],
106+
cwd=pathlib.Path.cwd().joinpath('extensions/stable-diffusion-webui-depthmap-script/'),
107+
shell=False,
108+
stderr=subprocess.DEVNULL,
109+
encoding='utf8').strip()[0:8]
110+
except Exception:
111+
commit_hash = "<none>"
112+
return commit_hash
113+
102114

103115
def main_ui_panel(is_depth_tab):
104116
with gr.Blocks():
@@ -335,8 +347,8 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
335347

336348
if len(inputimages) == 0 or inputimages[0] == None:
337349
return [], []
338-
339-
print(f"\n{scriptname} {scriptversion} ({commit_hash})")
350+
351+
print(f"\n{scriptname} {scriptversion} ({get_commit_hash()})")
340352

341353
unload_sd_model()
342354

@@ -688,8 +700,10 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
688700
if opts.samples_format == "png":
689701
try:
690702
images.save_image(Image.fromarray(img_output), outpath, "", processed.all_seeds[count], processed.all_prompts[count], opts.samples_format, info=info, p=processed, suffix="_depth")
691-
except ValueError as ve:
692-
if not ('image has wrong mode' in str(ve) or 'cannot write mode I;16 as JPEG' in str(ve)): raise ve
703+
except Exception as ve:
704+
if not ('image has wrong mode' in str(ve) or 'I;16' in str(ve)): raise ve
705+
print('Catched exception: image has wrong mode!')
706+
traceback.print_exc()
693707
else:
694708
images.save_image(Image.fromarray(img_output2), outpath, "", processed.all_seeds[count], processed.all_prompts[count], opts.samples_format, info=info, p=processed, suffix="_depth")
695709
elif save_depth:
@@ -698,8 +712,10 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
698712
if opts.samples_format == "png":
699713
try:
700714
images.save_image(Image.fromarray(img_output), 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)
701-
except ValueError as ve:
702-
if not ('image has wrong mode' in str(ve) or 'cannot write mode I;16 as JPEG' in str(ve)): raise ve
715+
except Exception as ve:
716+
if not ('image has wrong mode' in str(ve) or 'I;16' in str(ve)): raise ve
717+
print('Catched exception: image has wrong mode!')
718+
traceback.print_exc()
703719
else:
704720
images.save_image(Image.fromarray(img_output2), 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)
705721
else:

0 commit comments

Comments
 (0)