Commit
·
e3e308f
1
Parent(s):
9ab3234
no control net
Browse files- handler.py +80 -80
handler.py
CHANGED
|
@@ -57,19 +57,19 @@ class EndpointHandler():
|
|
| 57 |
face_adapter = f"./checkpoints/ip-adapter.bin"
|
| 58 |
controlnet_path = f"./checkpoints/ControlNetModel"
|
| 59 |
|
| 60 |
-
transform = Compose([
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
])
|
| 73 |
|
| 74 |
self.controlnet_identitynet = ControlNetModel.from_pretrained(
|
| 75 |
controlnet_path, torch_dtype=dtype
|
|
@@ -101,61 +101,61 @@ class EndpointHandler():
|
|
| 101 |
|
| 102 |
|
| 103 |
# controlnet-pose/canny/depth
|
| 104 |
-
controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
|
| 105 |
-
controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
|
| 106 |
-
controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
|
| 107 |
-
|
| 108 |
-
controlnet_pose = ControlNetModel.from_pretrained(
|
| 109 |
-
|
| 110 |
-
).to(device)
|
| 111 |
-
controlnet_canny = ControlNetModel.from_pretrained(
|
| 112 |
-
|
| 113 |
-
).to(device)
|
| 114 |
-
controlnet_depth = ControlNetModel.from_pretrained(
|
| 115 |
-
|
| 116 |
-
).to(device)
|
| 117 |
-
|
| 118 |
-
def get_canny_image(image, t1=100, t2=200):
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
def get_depth_map(image):
|
| 124 |
|
| 125 |
-
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
-
|
| 139 |
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
|
| 144 |
-
self.controlnet_map = {
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
}
|
| 149 |
|
| 150 |
-
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 151 |
-
depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(device).eval()
|
| 152 |
|
| 153 |
|
| 154 |
-
self.controlnet_map_fn = {
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
}
|
| 159 |
|
| 160 |
self.app = FaceAnalysis(name="buffalo_l", root="./", providers=["CPUExecutionProvider"])
|
| 161 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
|
@@ -166,10 +166,10 @@ class EndpointHandler():
|
|
| 166 |
|
| 167 |
adapter_strength_ratio = 0.8
|
| 168 |
identitynet_strength_ratio = 0.8
|
| 169 |
-
pose_strength = 0.4
|
| 170 |
-
canny_strength = 0.3
|
| 171 |
-
depth_strength = 0.5
|
| 172 |
-
controlnet_selection = ["pose", "canny", "depth"]
|
| 173 |
|
| 174 |
face_image_path = "https://i.ibb.co/SKg69dD/kaifu-resize.png"
|
| 175 |
pose_image_path = "https://i.ibb.co/ZSrQ8ZJ/pose.jpg"
|
|
@@ -276,27 +276,27 @@ class EndpointHandler():
|
|
| 276 |
control_mask[y1:y2, x1:x2] = 255
|
| 277 |
control_mask = Image.fromarray(control_mask.astype(np.uint8))
|
| 278 |
|
| 279 |
-
if len(controlnet_selection) > 0:
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
else:
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
generator = torch.Generator(device=device.type).manual_seed(3)
|
| 302 |
|
|
|
|
| 57 |
face_adapter = f"./checkpoints/ip-adapter.bin"
|
| 58 |
controlnet_path = f"./checkpoints/ControlNetModel"
|
| 59 |
|
| 60 |
+
# transform = Compose([
|
| 61 |
+
# Resize(
|
| 62 |
+
# width=518,
|
| 63 |
+
# height=518,
|
| 64 |
+
# resize_target=False,
|
| 65 |
+
# keep_aspect_ratio=True,
|
| 66 |
+
# ensure_multiple_of=14,
|
| 67 |
+
# resize_method='lower_bound',
|
| 68 |
+
# image_interpolation_method=cv2.INTER_CUBIC,
|
| 69 |
+
# ),
|
| 70 |
+
# NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 71 |
+
# PrepareForNet(),
|
| 72 |
+
# ])
|
| 73 |
|
| 74 |
self.controlnet_identitynet = ControlNetModel.from_pretrained(
|
| 75 |
controlnet_path, torch_dtype=dtype
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
# controlnet-pose/canny/depth
|
| 104 |
+
# controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
|
| 105 |
+
# controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
|
| 106 |
+
# controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
|
| 107 |
+
|
| 108 |
+
# controlnet_pose = ControlNetModel.from_pretrained(
|
| 109 |
+
# controlnet_pose_model, torch_dtype=dtype
|
| 110 |
+
# ).to(device)
|
| 111 |
+
# controlnet_canny = ControlNetModel.from_pretrained(
|
| 112 |
+
# controlnet_canny_model, torch_dtype=dtype
|
| 113 |
+
# ).to(device)
|
| 114 |
+
# controlnet_depth = ControlNetModel.from_pretrained(
|
| 115 |
+
# controlnet_depth_model, torch_dtype=dtype
|
| 116 |
+
# ).to(device)
|
| 117 |
+
|
| 118 |
+
# def get_canny_image(image, t1=100, t2=200):
|
| 119 |
+
# image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 120 |
+
# edges = cv2.Canny(image, t1, t2)
|
| 121 |
+
# return Image.fromarray(edges, "L")
|
| 122 |
|
| 123 |
+
# def get_depth_map(image):
|
| 124 |
|
| 125 |
+
# image = np.array(image) / 255.0
|
| 126 |
|
| 127 |
+
# h, w = image.shape[:2]
|
| 128 |
|
| 129 |
+
# image = transform({'image': image})['image']
|
| 130 |
+
# image = torch.from_numpy(image).unsqueeze(0).to("cuda")
|
| 131 |
|
| 132 |
+
# with torch.no_grad():
|
| 133 |
+
# depth = depth_anything(image)
|
| 134 |
|
| 135 |
+
# depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
|
| 136 |
+
# depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
| 137 |
|
| 138 |
+
# depth = depth.cpu().numpy().astype(np.uint8)
|
| 139 |
|
| 140 |
+
# depth_image = Image.fromarray(depth)
|
| 141 |
|
| 142 |
+
# return depth_image
|
| 143 |
|
| 144 |
+
# self.controlnet_map = {
|
| 145 |
+
# "pose": controlnet_pose,
|
| 146 |
+
# "canny": controlnet_canny,
|
| 147 |
+
# "depth": controlnet_depth,
|
| 148 |
+
# }
|
| 149 |
|
| 150 |
+
# openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 151 |
+
# depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(device).eval()
|
| 152 |
|
| 153 |
|
| 154 |
+
# self.controlnet_map_fn = {
|
| 155 |
+
# "pose": openpose,
|
| 156 |
+
# "canny": get_canny_image,
|
| 157 |
+
# "depth": get_depth_map,
|
| 158 |
+
# }
|
| 159 |
|
| 160 |
self.app = FaceAnalysis(name="buffalo_l", root="./", providers=["CPUExecutionProvider"])
|
| 161 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
| 166 |
|
| 167 |
adapter_strength_ratio = 0.8
|
| 168 |
identitynet_strength_ratio = 0.8
|
| 169 |
+
# pose_strength = 0.4
|
| 170 |
+
# canny_strength = 0.3
|
| 171 |
+
# depth_strength = 0.5
|
| 172 |
+
# controlnet_selection = ["pose", "canny", "depth"]
|
| 173 |
|
| 174 |
face_image_path = "https://i.ibb.co/SKg69dD/kaifu-resize.png"
|
| 175 |
pose_image_path = "https://i.ibb.co/ZSrQ8ZJ/pose.jpg"
|
|
|
|
| 276 |
control_mask[y1:y2, x1:x2] = 255
|
| 277 |
control_mask = Image.fromarray(control_mask.astype(np.uint8))
|
| 278 |
|
| 279 |
+
# if len(controlnet_selection) > 0:
|
| 280 |
+
# controlnet_scales = {
|
| 281 |
+
# "pose": pose_strength,
|
| 282 |
+
# "canny": canny_strength,
|
| 283 |
+
# "depth": depth_strength,
|
| 284 |
+
# }
|
| 285 |
+
# self.pipe.controlnet = MultiControlNetModel(
|
| 286 |
+
# [self.controlnet_identitynet]
|
| 287 |
+
# + [self.controlnet_map[s] for s in controlnet_selection]
|
| 288 |
+
# )
|
| 289 |
+
# control_scales = [float(identitynet_strength_ratio)] + [
|
| 290 |
+
# controlnet_scales[s] for s in controlnet_selection
|
| 291 |
+
# ]
|
| 292 |
+
# control_images = [face_kps] + [
|
| 293 |
+
# self.controlnet_map_fn[s](img_controlnet).resize((width, height))
|
| 294 |
+
# for s in controlnet_selection
|
| 295 |
+
# ]
|
| 296 |
+
# else:
|
| 297 |
+
self.pipe.controlnet = self.controlnet_identitynet
|
| 298 |
+
control_scales = float(identitynet_strength_ratio)
|
| 299 |
+
control_images = face_kps
|
| 300 |
|
| 301 |
generator = torch.Generator(device=device.type).manual_seed(3)
|
| 302 |
|