cddb07a176
docs / deploy (push) Has been cancelled
docs / changes (push) Has been cancelled
docs / check-and-build (push) Has been cancelled
build container image / cpu (push) Has been cancelled
build container image / cuda (push) Has been cancelled
build container image / rocm (push) Has been cancelled
frontend checks / frontend-checks (push) Has been cancelled
frontend tests / frontend-tests (push) Has been cancelled
lfs checks / lfs-check (push) Has been cancelled
python checks / python-checks (push) Has been cancelled
python tests / py3.12: macos-default (push) Has been cancelled
python tests / py3.11: windows-cpu (push) Has been cancelled
python tests / py3.12: windows-cpu (push) Has been cancelled
python tests / py3.11: linux-cpu (push) Has been cancelled
typegen checks / typegen-checks (push) Has been cancelled
uv lock checks / uv-lock-checks (push) Has been cancelled
openapi checks / openapi-checks (push) Has been cancelled
python tests / py3.11: macos-default (push) Has been cancelled
python tests / py3.12: linux-cpu (push) Has been cancelled
46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
from typing import Literal
|
|
|
|
from invokeai.app.invocations.baseinvocation import BaseInvocation, invocation
|
|
from invokeai.app.invocations.fields import ImageField, InputField, WithBoard, WithMetadata
|
|
from invokeai.app.invocations.primitives import ImageOutput
|
|
from invokeai.app.services.shared.invocation_context import InvocationContext
|
|
from invokeai.backend.image_util.depth_anything.depth_anything_pipeline import DepthAnythingPipeline
|
|
|
|
DEPTH_ANYTHING_MODEL_SIZES = Literal["large", "base", "small", "small_v2"]
|
|
# DepthAnything V2 Small model is licensed under Apache 2.0 but not the base and large models.
|
|
DEPTH_ANYTHING_MODELS = {
|
|
"large": "LiheYoung/depth-anything-large-hf",
|
|
"base": "LiheYoung/depth-anything-base-hf",
|
|
"small": "LiheYoung/depth-anything-small-hf",
|
|
"small_v2": "depth-anything/Depth-Anything-V2-Small-hf",
|
|
}
|
|
|
|
|
|
@invocation(
|
|
"depth_anything_depth_estimation",
|
|
title="Depth Anything Depth Estimation",
|
|
tags=["controlnet", "depth", "depth anything"],
|
|
category="controlnet_preprocessors",
|
|
version="1.0.0",
|
|
)
|
|
class DepthAnythingDepthEstimationInvocation(BaseInvocation, WithMetadata, WithBoard):
|
|
"""Generates a depth map using a Depth Anything model."""
|
|
|
|
image: ImageField = InputField(description="The image to process")
|
|
model_size: DEPTH_ANYTHING_MODEL_SIZES = InputField(
|
|
default="small_v2", description="The size of the depth model to use"
|
|
)
|
|
|
|
def invoke(self, context: InvocationContext) -> ImageOutput:
|
|
model_url = DEPTH_ANYTHING_MODELS[self.model_size]
|
|
image = context.images.get_pil(self.image.image_name, "RGB")
|
|
|
|
loaded_model = context.models.load_remote_model(model_url, DepthAnythingPipeline.load_model)
|
|
|
|
with loaded_model as depth_anything_detector:
|
|
assert isinstance(depth_anything_detector, DepthAnythingPipeline)
|
|
depth_map = depth_anything_detector.generate_depth(image)
|
|
|
|
image_dto = context.images.save(image=depth_map)
|
|
return ImageOutput.build(image_dto)
|