Update handler.py
Browse files- handler.py +16 -23
handler.py
CHANGED
|
@@ -40,7 +40,7 @@ class EndpointHandler:
|
|
| 40 |
else:
|
| 41 |
print(f"Model path {dir_path} exists. Skipping download.")
|
| 42 |
self.app = FaceAnalysis(name='antelopev2', root=model_dir, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 43 |
-
|
| 44 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 45 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 46 |
|
|
@@ -67,7 +67,7 @@ class EndpointHandler:
|
|
| 67 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 68 |
edges = cv2.Canny(image, t1, t2)
|
| 69 |
return Image.fromarray(edges, "L")
|
| 70 |
-
|
| 71 |
self.controlnet_map = {
|
| 72 |
"pose": controlnet_pose,
|
| 73 |
"canny": controlnet_canny
|
|
@@ -100,7 +100,7 @@ class EndpointHandler:
|
|
| 100 |
self.pipe.load_ip_adapter_instantid(face_adapter)
|
| 101 |
self.pipe.image_proj_model.to("cuda")
|
| 102 |
self.pipe.unet.to("cuda")
|
| 103 |
-
|
| 104 |
def __call__(self, data):
|
| 105 |
|
| 106 |
def convert_from_cv2_to_image(img: np.ndarray) -> Image:
|
|
@@ -151,15 +151,18 @@ class EndpointHandler:
|
|
| 151 |
style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
|
| 152 |
prompt = data.pop("inputs", "a man flying in the sky in Mars")
|
| 153 |
negative_prompt = data.pop("negative_prompt", "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green")
|
| 154 |
-
|
| 155 |
-
identitynet_strength_ratio = 0.8
|
| 156 |
-
adapter_strength_ratio = 0.8
|
| 157 |
-
pose_strength = 0.5
|
| 158 |
-
canny_strength = 0.3
|
| 159 |
-
num_steps = 5
|
| 160 |
-
guidance_scale = 3.0
|
| 161 |
-
controlnet_selection = ["pose", "canny"]
|
| 162 |
-
scheduler = "EulerDiscreteScheduler"
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
self.pipe.disable_lora()
|
| 165 |
scheduler_class_name = scheduler.split("-")[0]
|
|
@@ -183,11 +186,6 @@ class EndpointHandler:
|
|
| 183 |
# Extract face features
|
| 184 |
face_info = self.app.get(face_image_cv2)
|
| 185 |
|
| 186 |
-
# if len(face_info) == 0:
|
| 187 |
-
# raise gr.Error(
|
| 188 |
-
# f"Unable to detect a face in the image. Please upload a different photo with a clear face."
|
| 189 |
-
# )
|
| 190 |
-
|
| 191 |
face_info = sorted(
|
| 192 |
face_info,
|
| 193 |
key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
|
|
@@ -205,11 +203,6 @@ class EndpointHandler:
|
|
| 205 |
|
| 206 |
face_info = self.app.get(pose_image_cv2)
|
| 207 |
|
| 208 |
-
# if len(face_info) == 0:
|
| 209 |
-
# raise gr.Error(
|
| 210 |
-
# f"Cannot find any face in the reference image! Please upload another person image"
|
| 211 |
-
# )
|
| 212 |
-
|
| 213 |
face_info = face_info[-1]
|
| 214 |
face_kps = draw_kps(pose_image, face_info["kps"])
|
| 215 |
|
|
@@ -237,7 +230,7 @@ class EndpointHandler:
|
|
| 237 |
for s in controlnet_selection
|
| 238 |
]
|
| 239 |
|
| 240 |
-
generator = torch.Generator(device=device).manual_seed(
|
| 241 |
|
| 242 |
print("Start inference...")
|
| 243 |
print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
|
|
|
|
| 40 |
else:
|
| 41 |
print(f"Model path {dir_path} exists. Skipping download.")
|
| 42 |
self.app = FaceAnalysis(name='antelopev2', root=model_dir, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 43 |
+
|
| 44 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 45 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 46 |
|
|
|
|
| 67 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 68 |
edges = cv2.Canny(image, t1, t2)
|
| 69 |
return Image.fromarray(edges, "L")
|
| 70 |
+
|
| 71 |
self.controlnet_map = {
|
| 72 |
"pose": controlnet_pose,
|
| 73 |
"canny": controlnet_canny
|
|
|
|
| 100 |
self.pipe.load_ip_adapter_instantid(face_adapter)
|
| 101 |
self.pipe.image_proj_model.to("cuda")
|
| 102 |
self.pipe.unet.to("cuda")
|
| 103 |
+
|
| 104 |
def __call__(self, data):
|
| 105 |
|
| 106 |
def convert_from_cv2_to_image(img: np.ndarray) -> Image:
|
|
|
|
| 151 |
style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
|
| 152 |
prompt = data.pop("inputs", "a man flying in the sky in Mars")
|
| 153 |
negative_prompt = data.pop("negative_prompt", "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green")
|
| 154 |
+
|
| 155 |
+
identitynet_strength_ratio = data.pop("identitynet_strength_ratio", 0.8)
|
| 156 |
+
adapter_strength_ratio = data.pop("adapter_strength_ratio", 0.8)
|
| 157 |
+
pose_strength = data.pop("pose_strength", 0.5)
|
| 158 |
+
canny_strength = data.pop("canny_strength", 0.3)
|
| 159 |
+
num_steps = data.pop("num_inference_steps", 5)
|
| 160 |
+
guidance_scale = data.pop("guidance_scale", 3.0)
|
| 161 |
+
controlnet_selection = data.pop("controlnet_selection", ["pose", "canny"])
|
| 162 |
+
scheduler = data.pop("scheduler", "EulerDiscreteScheduler")
|
| 163 |
+
seed = data.pop("seed", 42)
|
| 164 |
+
enable_fast_inference = data.pop("enable_fast_inference", True)
|
| 165 |
+
enhance_non_face_region = data.pop("enhance_non_face_region", True)
|
| 166 |
|
| 167 |
self.pipe.disable_lora()
|
| 168 |
scheduler_class_name = scheduler.split("-")[0]
|
|
|
|
| 186 |
# Extract face features
|
| 187 |
face_info = self.app.get(face_image_cv2)
|
| 188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
face_info = sorted(
|
| 190 |
face_info,
|
| 191 |
key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
|
|
|
|
| 203 |
|
| 204 |
face_info = self.app.get(pose_image_cv2)
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
face_info = face_info[-1]
|
| 207 |
face_kps = draw_kps(pose_image, face_info["kps"])
|
| 208 |
|
|
|
|
| 230 |
for s in controlnet_selection
|
| 231 |
]
|
| 232 |
|
| 233 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 234 |
|
| 235 |
print("Start inference...")
|
| 236 |
print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
|