Skip to content

Commit daf899f

Browse files
fix: Move the manual image resizing out of the depth anything pipeline
1 parent 13fb2d1 commit daf899f

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

invokeai/app/invocations/controlnet_image_processors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ def run_processor(self, image: Image.Image) -> Image.Image:
594594

595595

596596
DEPTH_ANYTHING_MODEL_SIZES = Literal["large", "base", "small", "small_v2"]
597+
# DepthAnything V2 Small model is licensed under Apache 2.0 but not the base and large models.
597598
DEPTH_ANYTHING_MODELS = {
598599
"large": "LiheYoung/depth-anything-large-hf",
599600
"base": "LiheYoung/depth-anything-base-hf",
@@ -627,7 +628,13 @@ def load_depth_anything(model_path: Path):
627628
source=DEPTH_ANYTHING_MODELS[self.model_size], loader=load_depth_anything
628629
) as depth_anything_detector:
629630
assert isinstance(depth_anything_detector, DepthAnythingPipeline)
630-
return depth_anything_detector.generate_depth(image, self.resolution)
631+
depth_map = depth_anything_detector.generate_depth(image)
632+
633+
# Resizing to user target specified size
634+
new_height = int(image.size[1] * (self.resolution / image.size[0]))
635+
depth_map = depth_map.resize((self.resolution, new_height))
636+
637+
return depth_map
631638

632639

633640
@invocation(

invokeai/backend/image_util/depth_anything/depth_anything_pipeline.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, cast
1+
from typing import Optional
22

33
import torch
44
from PIL import Image
@@ -14,13 +14,9 @@ class DepthAnythingPipeline(RawModel):
1414
def __init__(self, pipeline: DepthEstimationPipeline) -> None:
1515
self.pipeline = pipeline
1616

17-
def generate_depth(self, image: Image.Image, resolution: int = 512):
18-
image_width, image_height = image.size
17+
def generate_depth(self, image: Image.Image) -> Image.Image:
1918
depth_map = self.pipeline(image)["depth"]
2019
assert isinstance(depth_map, Image.Image)
21-
22-
new_height = int(image_height * (resolution / image_width))
23-
depth_map = depth_map.resize((resolution, new_height))
2420
return depth_map
2521

2622
def to(self, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None):

0 commit comments

Comments
 (0)