Spaces:
Sleeping
Sleeping
ekhatskevich
commited on
Commit
·
7ceb780
1
Parent(s):
db52967
update algo
Browse files
app.py
CHANGED
@@ -52,32 +52,38 @@ def create_face_mask(pil_image):
|
|
52 |
"""
|
53 |
Create a binary mask (PIL Image) from a PIL image by detecting the face region.
|
54 |
The mask will be white (255) on the detected face area and black (0) elsewhere.
|
|
|
55 |
"""
|
56 |
try:
|
57 |
-
# Convert PIL image to a numpy array in RGB format
|
58 |
image_np = np.array(pil_image.convert("RGB"))
|
59 |
# Convert to grayscale for face detection
|
60 |
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
61 |
|
62 |
-
# Load the Haar cascade for face detection
|
63 |
cascade_path = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
64 |
face_cascade = cv2.CascadeClassifier(cascade_path)
|
65 |
|
66 |
# Detect faces in the image
|
67 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
|
68 |
|
69 |
-
# Create an empty mask with the same dimensions as the image
|
70 |
mask = np.zeros_like(gray, dtype=np.uint8)
|
71 |
|
72 |
-
# For each detected face, draw
|
73 |
for (x, y, w, h) in faces:
|
74 |
-
# Optionally expand the bounding box slightly
|
75 |
padding = 0.2
|
76 |
x1 = max(0, int(x - w * padding))
|
77 |
y1 = max(0, int(y - h * padding))
|
78 |
x2 = min(gray.shape[1], int(x + w * (1 + padding)))
|
79 |
y2 = min(gray.shape[0], int(y + h * (1 + padding)))
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
return Image.fromarray(mask)
|
83 |
except Exception as e:
|
@@ -98,7 +104,7 @@ def face_swap_app(target_img, face_img):
|
|
98 |
reference_image=target_img,
|
99 |
edit_image=face_img,
|
100 |
edit_mask=edit_mask,
|
101 |
-
prompt="
|
102 |
output_height=1024,
|
103 |
output_width=1024,
|
104 |
sampler='flow_euler',
|
|
|
52 |
"""
|
53 |
Create a binary mask (PIL Image) from a PIL image by detecting the face region.
|
54 |
The mask will be white (255) on the detected face area and black (0) elsewhere.
|
55 |
+
An ellipse is used to better match the shape of a face.
|
56 |
"""
|
57 |
try:
|
58 |
+
# Convert the PIL image to a numpy array in RGB format
|
59 |
image_np = np.array(pil_image.convert("RGB"))
|
60 |
# Convert to grayscale for face detection
|
61 |
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
62 |
|
63 |
+
# Load the Haar cascade for face detection
|
64 |
cascade_path = cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
65 |
face_cascade = cv2.CascadeClassifier(cascade_path)
|
66 |
|
67 |
# Detect faces in the image
|
68 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
|
69 |
|
70 |
+
# Create an empty mask with the same dimensions as the grayscale image
|
71 |
mask = np.zeros_like(gray, dtype=np.uint8)
|
72 |
|
73 |
+
# For each detected face, draw an ellipse instead of a rectangle
|
74 |
for (x, y, w, h) in faces:
|
75 |
+
# Optionally expand the bounding box slightly for a better fit
|
76 |
padding = 0.2
|
77 |
x1 = max(0, int(x - w * padding))
|
78 |
y1 = max(0, int(y - h * padding))
|
79 |
x2 = min(gray.shape[1], int(x + w * (1 + padding)))
|
80 |
y2 = min(gray.shape[0], int(y + h * (1 + padding)))
|
81 |
+
|
82 |
+
# Calculate the center and axes for the ellipse
|
83 |
+
center = (int((x1 + x2) / 2), int((y1 + y2) / 2))
|
84 |
+
axes = (int((x2 - x1) / 2), int((y2 - y1) / 2))
|
85 |
+
# Draw a filled ellipse (white) on the mask
|
86 |
+
cv2.ellipse(mask, center, axes, 0, 0, 360, 255, -1)
|
87 |
|
88 |
return Image.fromarray(mask)
|
89 |
except Exception as e:
|
|
|
104 |
reference_image=target_img,
|
105 |
edit_image=face_img,
|
106 |
edit_mask=edit_mask,
|
107 |
+
prompt="retain face",
|
108 |
output_height=1024,
|
109 |
output_width=1024,
|
110 |
sampler='flow_euler',
|