Jacobmadwed commited on
Commit
c26f322
·
verified ·
1 Parent(s): 2e5ed25

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +26 -20
handler.py CHANGED
@@ -47,8 +47,8 @@ class GenerateImageRequest(BaseModel):
47
  seed: int
48
  enable_LCM: bool
49
  enhance_face_region: bool
50
- face_image_path: Optional[str] = None
51
- pose_image_path: Optional[str] = None
52
 
53
  class EndpointHandler:
54
  def __init__(self, model_dir):
@@ -197,38 +197,44 @@ class EndpointHandler:
197
  # apply the style template
198
  inputs, negative_prompt = apply_style(style_name, inputs, negative_prompt)
199
 
200
- # Load face image
201
- face_image_path = data.get("face_image_path", "raw/input.jpg")
202
- if os.path.exists(face_image_path):
203
- face_image = load_image(face_image_path)
204
- else:
205
- raise FileNotFoundError(f"Face image not found at path: {face_image_path}")
 
 
 
 
206
 
207
  face_image = resize_img(face_image, max_side=1024)
208
  face_image_cv2 = convert_from_image_to_cv2(face_image)
209
  height, width, _ = face_image_cv2.shape
210
 
211
- # Load pose image if provided
212
- pose_image_path = data.get("pose_image_path")
213
- if pose_image_path and os.path.exists(pose_image_path):
214
- pose_image = load_image(pose_image_path)
 
 
 
 
 
 
 
 
 
215
  pose_image = resize_img(pose_image, max_side=1024)
216
  img_controlnet = pose_image
217
  pose_image_cv2 = convert_from_image_to_cv2(pose_image)
218
 
219
  face_info = self.app.get(pose_image_cv2)
 
220
  face_info = face_info[-1]
221
  face_kps = draw_kps(pose_image, face_info["kps"])
222
 
223
  width, height = face_kps.size
224
- else:
225
- img_controlnet = face_image
226
-
227
- # Extract face features
228
- face_info = self.app.get(face_image_cv2)
229
- face_info = sorted(face_info, key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1])[-1] # only use the maximum face
230
- face_emb = face_info["embedding"]
231
- face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
232
 
233
  control_mask = np.zeros([height, width, 3])
234
  x1, y1, x2, y2 = face_info["bbox"]
 
47
  seed: int
48
  enable_LCM: bool
49
  enhance_face_region: bool
50
+ face_image_base64: str
51
+ pose_image_base64: Optional[str] = None
52
 
53
  class EndpointHandler:
54
  def __init__(self, model_dir):
 
197
  # apply the style template
198
  inputs, negative_prompt = apply_style(style_name, inputs, negative_prompt)
199
 
200
+ # Decode base64 image
201
+ face_image_base64 = data.get("face_image_base64")
202
+ face_image_data = base64.b64decode(face_image_base64)
203
+ face_image = Image.open(io.BytesIO(face_image_data))
204
+
205
+ pose_image_base64 = data.get("pose_image_base64")
206
+ pose_image = None
207
+ if pose_image_base64:
208
+ pose_image_data = base64.b64decode(pose_image_base64)
209
+ pose_image = Image.open(io.BytesIO(pose_image_data))
210
 
211
  face_image = resize_img(face_image, max_side=1024)
212
  face_image_cv2 = convert_from_image_to_cv2(face_image)
213
  height, width, _ = face_image_cv2.shape
214
 
215
+ # Extract face features
216
+ face_info = self.app.get(face_image_cv2)
217
+
218
+ face_info = sorted(
219
+ face_info,
220
+ key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
221
+ )[
222
+ -1
223
+ ] # only use the maximum face
224
+ face_emb = face_info["embedding"]
225
+ face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
226
+ img_controlnet = face_image
227
+ if pose_image:
228
  pose_image = resize_img(pose_image, max_side=1024)
229
  img_controlnet = pose_image
230
  pose_image_cv2 = convert_from_image_to_cv2(pose_image)
231
 
232
  face_info = self.app.get(pose_image_cv2)
233
+
234
  face_info = face_info[-1]
235
  face_kps = draw_kps(pose_image, face_info["kps"])
236
 
237
  width, height = face_kps.size
 
 
 
 
 
 
 
 
238
 
239
  control_mask = np.zeros([height, width, 3])
240
  x1, y1, x2, y2 = face_info["bbox"]