1- from invokeai .backend .bria .controlnet_bria import BRIA_CONTROL_MODES
1+ import cv2
2+ import numpy as np
3+ from PIL import Image
24from pydantic import BaseModel , Field
3- from invokeai . invocation_api import ImageOutput , Classification
5+
46from invokeai .app .invocations .baseinvocation import (
57 BaseInvocation ,
68 BaseInvocationOutput ,
79 invocation ,
810 invocation_output ,
911)
10- from invokeai .app .invocations .fields import FieldDescriptions , ImageField , InputField , OutputField , UIType , WithBoard , WithMetadata
12+ from invokeai .app .invocations .fields import (
13+ FieldDescriptions ,
14+ ImageField ,
15+ InputField ,
16+ OutputField ,
17+ UIType ,
18+ WithBoard ,
19+ WithMetadata ,
20+ )
1121from invokeai .app .invocations .model import ModelIdentifierField
1222from invokeai .app .services .shared .invocation_context import InvocationContext
13- import numpy as np
14- import cv2
15- from PIL import Image
16-
23+ from invokeai .backend .bria .controlnet_aux .open_pose import Body , Face , Hand , OpenposeDetector
24+ from invokeai .backend .bria .controlnet_bria import BRIA_CONTROL_MODES
1725from invokeai .backend .image_util .depth_anything .depth_anything_pipeline import DepthAnythingPipeline
18- from invokeai .backend . bria . controlnet_aux . open_pose import OpenposeDetector , Body , Hand , Face
26+ from invokeai .invocation_api import Classification , ImageOutput
1927
2028DEPTH_SMALL_V2_URL = "depth-anything/Depth-Anything-V2-Small-hf"
2129HF_LLLYASVIEL = "https://huggingface.co/lllyasviel/Annotators/resolve/main/"
2230
31+
2332class BriaControlNetField (BaseModel ):
2433 image : ImageField = Field (description = "The control image" )
2534 model : ModelIdentifierField = Field (description = "The ControlNet model to use" )
2635 mode : BRIA_CONTROL_MODES = Field (description = "The mode of the ControlNet" )
2736 conditioning_scale : float = Field (description = "The weight given to the ControlNet" )
2837
38+
2939@invocation_output ("bria_controlnet_output" )
3040class BriaControlNetOutput (BaseInvocationOutput ):
3141 """Bria ControlNet info"""
@@ -49,12 +59,8 @@ class BriaControlNetInvocation(BaseInvocation, WithMetadata, WithBoard):
4959 control_model : ModelIdentifierField = InputField (
5060 description = FieldDescriptions .controlnet_model , ui_type = UIType .BriaControlNetModel
5161 )
52- control_mode : BRIA_CONTROL_MODES = InputField (
53- default = "depth" , description = "The mode of the ControlNet"
54- )
55- control_weight : float = InputField (
56- default = 1.0 , ge = - 1 , le = 2 , description = "The weight given to the ControlNet"
57- )
62+ control_mode : BRIA_CONTROL_MODES = InputField (default = "depth" , description = "The mode of the ControlNet" )
63+ control_weight : float = InputField (default = 1.0 , ge = - 1 , le = 2 , description = "The weight given to the ControlNet" )
5864
5965 def invoke (self , context : InvocationContext ) -> BriaControlNetOutput :
6066 image_in = resize_img (context .images .get_pil (self .control_image .image_name ))
@@ -70,7 +76,7 @@ def invoke(self, context: InvocationContext) -> BriaControlNetOutput:
7076 control_image = convert_to_grayscale (image_in )
7177 elif self .control_mode == "tile" :
7278 control_image = tile (16 , image_in )
73-
79+
7480 control_image = resize_img (control_image )
7581 image_dto = context .images .save (image = control_image )
7682 image_output = ImageOutput .build (image_dto )
@@ -99,6 +105,7 @@ def invoke(self, context: InvocationContext) -> BriaControlNetOutput:
99105 1.7708333333333333 : {"width" : 1360 , "height" : 768 },
100106}
101107
108+
102109def extract_depth (image : Image .Image , context : InvocationContext ):
103110 loaded_model = context .models .load_remote_model (DEPTH_SMALL_V2_URL , DepthAnythingPipeline .load_model )
104111
@@ -107,6 +114,7 @@ def extract_depth(image: Image.Image, context: InvocationContext):
107114 depth_map = depth_anything_detector .generate_depth (image )
108115 return depth_map
109116
117+
110118def extract_openpose (image : Image .Image , context : InvocationContext ):
111119 body_model = context .models .load_remote_model (f"{ HF_LLLYASVIEL } body_pose_model.pth" , Body )
112120 hand_model = context .models .load_remote_model (f"{ HF_LLLYASVIEL } hand_pose_model.pth" , Hand )
@@ -115,10 +123,10 @@ def extract_openpose(image: Image.Image, context: InvocationContext):
115123 with body_model as body_model , hand_model as hand_model , face_model as face_model :
116124 open_pose_model = OpenposeDetector (body_model , hand_model , face_model )
117125 processed_image_open_pose = open_pose_model (image , hand_and_face = True )
118-
126+
119127 processed_image_open_pose = processed_image_open_pose .resize (image .size )
120128 return processed_image_open_pose
121-
129+
122130
123131def extract_canny (input_image ):
124132 image = np .array (input_image )
@@ -130,13 +138,17 @@ def extract_canny(input_image):
130138
131139
132140def convert_to_grayscale (image ):
133- gray_image = image .convert ('L' ).convert (' RGB' )
141+ gray_image = image .convert ("L" ).convert (" RGB" )
134142 return gray_image
135143
144+
136145def tile (downscale_factor , input_image ):
137- control_image = input_image .resize ((input_image .size [0 ] // downscale_factor , input_image .size [1 ] // downscale_factor )).resize (input_image .size , Image .Resampling .NEAREST )
146+ control_image = input_image .resize (
147+ (input_image .size [0 ] // downscale_factor , input_image .size [1 ] // downscale_factor )
148+ ).resize (input_image .size , Image .Resampling .NEAREST )
138149 return control_image
139-
150+
151+
140152def resize_img (control_image ):
141153 image_ratio = control_image .width / control_image .height
142154 ratio = min (RATIO_CONFIGS_1024 .keys (), key = lambda k : abs (k - image_ratio ))
0 commit comments