ekhatskevich commited on
Commit
62816e0
·
1 Parent(s): 3d23955

fix: change to tensors

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -2,6 +2,7 @@ import cv2
2
  import numpy as np
3
  from PIL import Image
4
  import os
 
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download, snapshot_download
7
 
@@ -84,6 +85,14 @@ def create_face_mask(pil_image):
84
  print(f"Error: {e}")
85
  raise ValueError('A very specific bad thing happened.')
86
 
 
 
 
 
 
 
 
 
87
  def face_swap_app(target_img, face_img):
88
  if target_img is None or face_img is None:
89
  raise ValueError("Both a target image and a face image must be provided.")
@@ -92,11 +101,14 @@ def face_swap_app(target_img, face_img):
92
  target_img = target_img.convert("RGB")
93
  face_img = face_img.convert("RGB")
94
 
 
 
 
95
  edit_mask = create_face_mask(face_img)
96
 
97
  output_img, edit_image, change_image, mask, seed = ace_infer(
98
- reference_image=target_img,
99
- edit_image=face_img,
100
  edit_mask=edit_mask,
101
  prompt="Face swap",
102
  output_height=1024,
 
2
  import numpy as np
3
  from PIL import Image
4
  import os
5
+ from torchvision import transforms
6
  import gradio as gr
7
  from huggingface_hub import hf_hub_download, snapshot_download
8
 
 
85
  print(f"Error: {e}")
86
  raise ValueError('A very specific bad thing happened.')
87
 
88
+ def pil_to_tensor(image):
89
+ # Convert a PIL image to a torch tensor scaled to [-1, 1]
90
+ transform = transforms.Compose([
91
+ transforms.ToTensor(), # Converts PIL to tensor with values in [0,1]
92
+ transforms.Lambda(lambda t: 2 * t - 1) # Scale to [-1, 1] if required by your model
93
+ ])
94
+ return transform(image)
95
+
96
  def face_swap_app(target_img, face_img):
97
  if target_img is None or face_img is None:
98
  raise ValueError("Both a target image and a face image must be provided.")
 
101
  target_img = target_img.convert("RGB")
102
  face_img = face_img.convert("RGB")
103
 
104
+ target_tensor = pil_to_tensor(target_img)
105
+ face_tensor = pil_to_tensor(face_img)
106
+
107
  edit_mask = create_face_mask(face_img)
108
 
109
  output_img, edit_image, change_image, mask, seed = ace_infer(
110
+ reference_image=target_tensor,
111
+ edit_image=face_tensor,
112
  edit_mask=edit_mask,
113
  prompt="Face swap",
114
  output_height=1024,