Update handler.py
Browse files- handler.py +10 -12
handler.py
CHANGED
|
@@ -9,7 +9,6 @@ import diffusers
|
|
| 9 |
from diffusers.utils import load_image
|
| 10 |
from diffusers.models import ControlNetModel
|
| 11 |
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
|
| 12 |
-
from insightface.app import FaceAnalysis
|
| 13 |
from style_template import styles
|
| 14 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
| 15 |
from controlnet_aux import OpenposeDetector
|
|
@@ -21,6 +20,7 @@ import base64
|
|
| 21 |
import io
|
| 22 |
import json
|
| 23 |
from transformers import CLIPProcessor, CLIPModel
|
|
|
|
| 24 |
|
| 25 |
# global variable
|
| 26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -58,15 +58,13 @@ class EndpointHandler:
|
|
| 58 |
controlnet_model = hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/diffusion_pytorch_model.safetensors", local_dir=os.path.join(model_dir, "checkpoints"))
|
| 59 |
face_adapter = hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir=os.path.join(model_dir, "checkpoints"))
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
self.app = FaceAnalysis(name='antelopev2', root=model_dir, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
| 68 |
|
| 69 |
-
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 70 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 71 |
|
| 72 |
# Path to InstantID models
|
|
@@ -235,8 +233,8 @@ class EndpointHandler:
|
|
| 235 |
face_image_cv2 = convert_from_image_to_cv2(face_image)
|
| 236 |
height, width, _ = face_image_cv2.shape
|
| 237 |
|
| 238 |
-
# Extract face features
|
| 239 |
-
face_info = self.
|
| 240 |
|
| 241 |
face_info = sorted(
|
| 242 |
face_info,
|
|
@@ -252,7 +250,7 @@ class EndpointHandler:
|
|
| 252 |
img_controlnet = pose_image
|
| 253 |
pose_image_cv2 = convert_from_image_to_cv2(pose_image)
|
| 254 |
|
| 255 |
-
face_info = self.
|
| 256 |
|
| 257 |
face_info = face_info[-1]
|
| 258 |
face_kps = draw_kps(pose_image, face_info["kps"])
|
|
|
|
| 9 |
from diffusers.utils import load_image
|
| 10 |
from diffusers.models import ControlNetModel
|
| 11 |
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
|
|
|
|
| 12 |
from style_template import styles
|
| 13 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
| 14 |
from controlnet_aux import OpenposeDetector
|
|
|
|
| 20 |
import io
|
| 21 |
import json
|
| 22 |
from transformers import CLIPProcessor, CLIPModel
|
| 23 |
+
import pickle
|
| 24 |
|
| 25 |
# global variable
|
| 26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 58 |
controlnet_model = hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/diffusion_pytorch_model.safetensors", local_dir=os.path.join(model_dir, "checkpoints"))
|
| 59 |
face_adapter = hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir=os.path.join(model_dir, "checkpoints"))
|
| 60 |
|
| 61 |
+
# Load the new model
|
| 62 |
+
senet_model_path = os.path.join(model_dir, "models", "senet50.pkl")
|
| 63 |
+
if not os.path.exists(senet_model_path):
|
| 64 |
+
print(f"Model path {senet_model_path} does not exist. Please ensure the model is available.")
|
| 65 |
+
with open(senet_model_path, 'rb') as f:
|
| 66 |
+
self.senet_model = pickle.load(f)
|
|
|
|
| 67 |
|
|
|
|
| 68 |
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 69 |
|
| 70 |
# Path to InstantID models
|
|
|
|
| 233 |
face_image_cv2 = convert_from_image_to_cv2(face_image)
|
| 234 |
height, width, _ = face_image_cv2.shape
|
| 235 |
|
| 236 |
+
# Extract face features using the new model
|
| 237 |
+
face_info = self.senet_model.get(face_image_cv2)
|
| 238 |
|
| 239 |
face_info = sorted(
|
| 240 |
face_info,
|
|
|
|
| 250 |
img_controlnet = pose_image
|
| 251 |
pose_image_cv2 = convert_from_image_to_cv2(pose_image)
|
| 252 |
|
| 253 |
+
face_info = self.senet_model.get(pose_image_cv2)
|
| 254 |
|
| 255 |
face_info = face_info[-1]
|
| 256 |
face_kps = draw_kps(pose_image, face_info["kps"])
|