11# Author: thygate
22# https://github.com/thygate/stable-diffusion-webui-depthmap-script
33
4- import modules .scripts as scripts
4+ from operator import getitem
5+ from pathlib import Path
6+
57import gradio as gr
8+ from PIL import Image
9+ from numba import njit , prange
10+ from torchvision .transforms import Compose , transforms
611
7- from modules .call_queue import wrap_gradio_gpu_call , wrap_queued_call , wrap_gradio_call
8- from modules .ui import plaintext_to_html
9- from modules import processing , images , shared , sd_samplers , devices
10- from modules .processing import create_infotext , process_images , Processed
11- from modules .shared import opts , cmd_opts , state , Options
12+ import modules .scripts as scripts
13+ from modules import processing , images , shared , devices
1214from modules import script_callbacks
15+ from modules .call_queue import wrap_gradio_gpu_call
1316from modules .images import get_next_sequence_number
14- from numba import njit , prange
15- from torchvision .transforms import Compose , transforms
16- from PIL import Image
17- from pathlib import Path
18- from operator import getitem
19- from tqdm import trange
20- from functools import reduce
21- from skimage .transform import resize
22- from trimesh import transformations
17+ from modules .processing import create_infotext
18+ from modules .shared import opts , cmd_opts
19+ from modules .ui import plaintext_to_html
20+
21+ try :
22+ from tqdm import trange
23+ except :
24+ from builtins import range as trange
2325
2426import sys
2527import torch , gc
26- import torch .nn as nn
2728import cv2
2829import os .path
29- import contextlib
30- import matplotlib .pyplot as plt
3130import numpy as np
3231import skimage .measure
3332import copy
3433import platform
35- import vispy
36- import trimesh
37- import os
3834import math
3935import subprocess
4036import traceback
@@ -90,9 +86,7 @@ def ensure_gradio_temp_directory():
9086from dzoedepth .utils .misc import colorize
9187from dzoedepth .utils .geometry import depth_to_points , create_triangles
9288
93- # background removal
94- from rembg import new_session , remove
95-
89+ # TODO: next two should not be here
9690whole_size_threshold = 1600 # R_max from the paper
9791pix2pixsize = 1024
9892scriptname = "DepthMap"
@@ -109,8 +103,6 @@ def ensure_gradio_temp_directory():
109103depthmap_deviceidx = None
110104
111105commit_hash = None # TODO: understand why it would spam to stderr if changed to ... = get_commit_hash()
112-
113-
114106def get_commit_hash ():
115107 global commit_hash
116108 if commit_hash is None :
@@ -467,7 +459,8 @@ def run_depthmap(processed, outpath, inputimages, inputnames, inp):
467459 try :
468460 if loadmodels and not (custom_depthmap and custom_depthmap_img != None ):
469461 # TODO: loading model should be separated into a function that would return the model
470- # and the parameters needed. The rest of the run_depthmap should not depend on what specific model
462+ # and the parameters (or maybe even functions) needed.
463+ # The rest of the run_depthmap should not depend on what specific model
471464 # is actually used for the generation.
472465 print ("Loading model weights from " , end = " " )
473466
@@ -1066,6 +1059,7 @@ def run_3dphoto(device, img_rgb, img_depth, inputnames, outpath, inpaint_vids, v
10661059
10671060def run_3dphoto_videos (mesh_fi , basename , outpath , num_frames , fps , crop_border , traj_types , x_shift_range ,
10681061 y_shift_range , z_shift_range , video_postfix , vid_dolly , vid_format , vid_ssaa ):
1062+ import vispy
10691063 if platform .system () == 'Windows' :
10701064 vispy .use (app = 'PyQt5' )
10711065 elif platform .system () == 'Darwin' :
@@ -1445,6 +1439,7 @@ def custom_depthmap_visibility(v):
14451439# TODO: code borrowed from the internet to be marked as such and to reside in separate files
14461440
14471441def batched_background_removal (inimages , model_name ):
1442+ from rembg import new_session , remove
14481443 print ('creating background masks' )
14491444 outimages = []
14501445
@@ -1531,6 +1526,7 @@ def estimateleres(img, model, w, h):
15311526
15321527
15331528def estimatemidas (img , model , w , h , resize_mode , normalization ):
1529+ import contextlib
15341530 # init transform
15351531 transform = Compose (
15361532 [
@@ -2176,6 +2172,7 @@ def depth_edges_mask(depth):
21762172
21772173
21782174def create_mesh (image , depth , keep_edges = False , spherical = False ):
2175+ import trimesh
21792176 maxsize = 1024
21802177 if hasattr (opts , 'depthmap_script_mesh_maxsize' ):
21812178 maxsize = opts .depthmap_script_mesh_maxsize
@@ -2205,7 +2202,7 @@ def create_mesh(image, depth, keep_edges=False, spherical=False):
22052202 angle = math .pi / 2
22062203 direction = [1 , 0 , 0 ]
22072204 center = [0 , 0 , 0 ]
2208- rot_matrix = transformations .rotation_matrix (angle , direction , center )
2205+ rot_matrix = trimesh . transformations .rotation_matrix (angle , direction , center )
22092206 mesh .apply_transform (rot_matrix )
22102207
22112208 return mesh
0 commit comments