Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -88,28 +88,23 @@ def extract_head_crop(image_pil, face_bbox):
|
|
| 88 |
crop_y1 = max(0, int(cy - crop_size/1.2)) # Più spazio sopra per i capelli
|
| 89 |
crop_x2 = min(image_pil.width, int(cx + crop_size/1.5))
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
crop_y2 = min(image_pil.height, int(y2 + h * 0.45))
|
| 94 |
|
| 95 |
return image_pil.crop((crop_x1, crop_y1, crop_x2, crop_y2))
|
| 96 |
|
| 97 |
-
def
|
| 98 |
-
"""
|
| 99 |
width, height = image_rgba.size
|
| 100 |
-
|
| 101 |
-
if
|
| 102 |
return image_rgba
|
| 103 |
|
| 104 |
r, g, b, a = image_rgba.split()
|
| 105 |
alpha_np = np.array(a)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
gradient = np.tile(gradient[:, None], (1, width))
|
| 110 |
-
|
| 111 |
-
# Applica il gradiente alla parte bassa del canale alpha
|
| 112 |
-
alpha_np[-fade_height:, :] = (alpha_np[-fade_height:, :] * gradient).astype(np.uint8)
|
| 113 |
|
| 114 |
a = Image.fromarray(alpha_np)
|
| 115 |
image_rgba.putalpha(a)
|
|
@@ -155,11 +150,11 @@ def generate_avatar(user_image, selected_style):
|
|
| 155 |
user_faces = face_app.get(cv_user_image)
|
| 156 |
user_face = sorted(user_faces, key=lambda x: (x.bbox[2]-x.bbox[0])*(x.bbox[3]-x.bbox[1]))[-1]
|
| 157 |
|
| 158 |
-
# 4. Ritaglio, rimozione sfondo e
|
| 159 |
print("Estrazione della testa con Rembg...")
|
| 160 |
cropped_user = extract_head_crop(user_image, user_face.bbox)
|
| 161 |
head_rgba = rembg.remove(cropped_user, session=rembg_session)
|
| 162 |
-
head_rgba =
|
| 163 |
|
| 164 |
# 5. Analisi della testa scontornata per trovare le nuove coordinate del volto
|
| 165 |
head_rgb_cv = cv2.cvtColor(np.array(head_rgba.convert("RGB")), cv2.COLOR_RGB2BGR)
|
|
|
|
| 88 |
crop_y1 = max(0, int(cy - crop_size/1.2)) # Più spazio sopra per i capelli
|
| 89 |
crop_x2 = min(image_pil.width, int(cx + crop_size/1.5))
|
| 90 |
|
| 91 |
+
# Taglio ancora più drastico: fermiamoci poco sotto il mento per escludere a priori qualsiasi spalla
|
| 92 |
+
crop_y2 = min(image_pil.height, int(y2 + h * 0.35))
|
|
|
|
| 93 |
|
| 94 |
return image_pil.crop((crop_x1, crop_y1, crop_x2, crop_y2))
|
| 95 |
|
| 96 |
+
def cut_bottom(image_rgba, cut_height_ratio=0.20):
|
| 97 |
+
"""Rende completamente trasparente la base dell'immagine (Hard Cut) per eliminare il collo inferiore."""
|
| 98 |
width, height = image_rgba.size
|
| 99 |
+
cut_height = int(height * cut_height_ratio)
|
| 100 |
+
if cut_height == 0:
|
| 101 |
return image_rgba
|
| 102 |
|
| 103 |
r, g, b, a = image_rgba.split()
|
| 104 |
alpha_np = np.array(a)
|
| 105 |
|
| 106 |
+
# Taglio netto: zero alpha nella parte inferiore
|
| 107 |
+
alpha_np[-cut_height:, :] = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
a = Image.fromarray(alpha_np)
|
| 110 |
image_rgba.putalpha(a)
|
|
|
|
| 150 |
user_faces = face_app.get(cv_user_image)
|
| 151 |
user_face = sorted(user_faces, key=lambda x: (x.bbox[2]-x.bbox[0])*(x.bbox[3]-x.bbox[1]))[-1]
|
| 152 |
|
| 153 |
+
# 4. Ritaglio, rimozione sfondo e Hard Cut del collo
|
| 154 |
print("Estrazione della testa con Rembg...")
|
| 155 |
cropped_user = extract_head_crop(user_image, user_face.bbox)
|
| 156 |
head_rgba = rembg.remove(cropped_user, session=rembg_session)
|
| 157 |
+
head_rgba = cut_bottom(head_rgba, cut_height_ratio=0.20) # Taglio netto trasparente
|
| 158 |
|
| 159 |
# 5. Analisi della testa scontornata per trovare le nuove coordinate del volto
|
| 160 |
head_rgb_cv = cv2.cvtColor(np.array(head_rgba.convert("RGB")), cv2.COLOR_RGB2BGR)
|