Skip to content

Commit 41b77cd

Browse files
fix: minor fixes to types in the DA Detector
1 parent 6f77477 commit 41b77cd

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

invokeai/backend/image_util/depth_anything/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
config = InvokeAIAppConfig.get_config()
1919

20+
DEPTH_ANYTHING_MODEL_SIZES = Literal["large", "base", "small"]
21+
2022
DEPTH_ANYTHING_MODELS = {
2123
"large": {
2224
"url": "https://huggingface.co/spaces/LiheYoung/Depth-Anything/resolve/main/checkpoints/depth_anything_vitl14.pth?download=true",
@@ -53,9 +55,9 @@
5355
class DepthAnythingDetector:
5456
def __init__(self) -> None:
5557
self.model = None
56-
self.model_size: Union[Literal["large", "base", "small"], None] = None
58+
self.model_size: Union[DEPTH_ANYTHING_MODEL_SIZES, None] = None
5759

58-
def load_model(self, model_size=Literal["large", "base", "small"]):
60+
def load_model(self, model_size: DEPTH_ANYTHING_MODEL_SIZES = "small"):
5961
DEPTH_ANYTHING_MODEL_PATH = pathlib.Path(config.models_path / DEPTH_ANYTHING_MODELS[model_size]["local"])
6062
if not DEPTH_ANYTHING_MODEL_PATH.exists():
6163
download_with_progress_bar(DEPTH_ANYTHING_MODELS[model_size]["url"], DEPTH_ANYTHING_MODEL_PATH)
@@ -84,16 +86,19 @@ def to(self, device):
8486
self.model.to(device)
8587
return self
8688

87-
def __call__(self, image, resolution=512):
88-
image = np.array(image, dtype=np.uint8)
89-
image = image[:, :, ::-1] / 255.0
89+
def __call__(self, image: Image.Image, resolution: int = 512):
90+
if self.model is None:
91+
raise Exception("Depth Anything Model not loaded")
92+
93+
np_image = np.array(image, dtype=np.uint8)
94+
np_image = np_image[:, :, ::-1] / 255.0
9095

91-
image_height, image_width = image.shape[:2]
92-
image = transform({"image": image})["image"]
93-
image = torch.from_numpy(image).unsqueeze(0).to(choose_torch_device())
96+
image_height, image_width = np_image.shape[:2]
97+
np_image = transform({"image": image})["image"]
98+
tensor_image = torch.from_numpy(np_image).unsqueeze(0).to(choose_torch_device())
9499

95100
with torch.no_grad():
96-
depth = self.model(image)
101+
depth = self.model(tensor_image)
97102
depth = F.interpolate(depth[None], (image_height, image_width), mode="bilinear", align_corners=False)[0, 0]
98103
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
99104

0 commit comments

Comments
 (0)