2
2
# initial implementation by Gregg Helt, 2023
3
3
# heavily leverages controlnet_aux package: https://github.com/patrickvonplaten/controlnet_aux
4
4
from builtins import bool , float
5
- from pathlib import Path
6
5
from typing import Dict , List , Literal , Union
7
6
8
7
import cv2
21
20
from controlnet_aux .util import HWC3 , ade_palette
22
21
from PIL import Image
23
22
from pydantic import BaseModel , Field , field_validator , model_validator
23
+ from transformers import pipeline
24
24
25
25
from invokeai .app .invocations .baseinvocation import (
26
26
BaseInvocation ,
44
44
from invokeai .app .services .shared .invocation_context import InvocationContext
45
45
from invokeai .app .util .controlnet_utils import CONTROLNET_MODE_VALUES , CONTROLNET_RESIZE_VALUES , heuristic_resize
46
46
from invokeai .backend .image_util .canny import get_canny_edges
47
- from invokeai .backend .image_util .depth_anything import DEPTH_ANYTHING_MODELS , DepthAnythingDetector
48
47
from invokeai .backend .image_util .dw_openpose import DWPOSE_MODELS , DWOpenposeDetector
49
48
from invokeai .backend .image_util .hed import HEDProcessor
50
49
from invokeai .backend .image_util .lineart import LineartProcessor
51
50
from invokeai .backend .image_util .lineart_anime import LineartAnimeProcessor
52
51
from invokeai .backend .image_util .util import np_to_pil , pil_to_np
53
- from invokeai .backend .util .devices import TorchDevice
54
52
55
53
56
54
class ControlField (BaseModel ):
@@ -593,14 +591,19 @@ def run_processor(self, image: Image.Image) -> Image.Image:
593
591
594
592
595
593
DEPTH_ANYTHING_MODEL_SIZES = Literal ["large" , "base" , "small" ]
594
+ DEPTH_ANYTHING_MODELS = {
595
+ "large" : "LiheYoung/depth-anything-large-hf" ,
596
+ "base" : "LiheYoung/depth-anything-base-hf" ,
597
+ "small" : "depth-anything/Depth-Anything-V2-Small-hf" ,
598
+ }
596
599
597
600
598
601
@invocation (
599
602
"depth_anything_image_processor" ,
600
603
title = "Depth Anything Processor" ,
601
604
tags = ["controlnet" , "depth" , "depth anything" ],
602
605
category = "controlnet" ,
603
- version = "1.1.2 " ,
606
+ version = "1.1.3 " ,
604
607
)
605
608
class DepthAnythingImageProcessorInvocation (ImageProcessorInvocation ):
606
609
"""Generates a depth map based on the Depth Anything algorithm"""
@@ -611,17 +614,9 @@ class DepthAnythingImageProcessorInvocation(ImageProcessorInvocation):
611
614
resolution : int = InputField (default = 512 , ge = 1 , description = FieldDescriptions .image_res )
612
615
613
616
def run_processor (self , image : Image .Image ) -> Image .Image :
614
- def loader (model_path : Path ):
615
- return DepthAnythingDetector .load_model (
616
- model_path , model_size = self .model_size , device = TorchDevice .choose_torch_device ()
617
- )
618
-
619
- with self ._context .models .load_remote_model (
620
- source = DEPTH_ANYTHING_MODELS [self .model_size ], loader = loader
621
- ) as model :
622
- depth_anything_detector = DepthAnythingDetector (model , TorchDevice .choose_torch_device ())
623
- processed_image = depth_anything_detector (image = image , resolution = self .resolution )
624
- return processed_image
617
+ depth_anything_pipeline = pipeline (task = "depth-estimation" , model = DEPTH_ANYTHING_MODELS [self .model_size ])
618
+ depth_map = depth_anything_pipeline (image )["depth" ]
619
+ return depth_map
625
620
626
621
627
622
@invocation (
0 commit comments