Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,9 +21,8 @@ def remove_background(img: Image.Image) -> Image.Image:
|
|
| 21 |
|
| 22 |
def blur_faces_smooth(image: Image.Image) -> tuple[Image.Image, list]:
|
| 23 |
"""
|
| 24 |
-
Detects faces using Haar Cascades
|
| 25 |
-
|
| 26 |
-
eyebrows (and forehead/hair) are left completely sharp and untouched.
|
| 27 |
"""
|
| 28 |
rgba = image.convert("RGBA")
|
| 29 |
width, height = rgba.size
|
|
@@ -33,14 +32,20 @@ def blur_faces_smooth(image: Image.Image) -> tuple[Image.Image, list]:
|
|
| 33 |
open_cv_image = open_cv_image[:, :, ::-1].copy() # Convert RGB to BGR
|
| 34 |
gray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)
|
| 35 |
|
| 36 |
-
# Load frontal
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
if len(faces) == 0:
|
| 46 |
# Fallback: soft feathered blur on upper center if no faces detected
|
|
@@ -52,7 +57,7 @@ def blur_faces_smooth(image: Image.Image) -> tuple[Image.Image, list]:
|
|
| 52 |
cv2.ellipse(mask_np, (cx, cy), (rx, ry), 0, 0, 360, 255, -1)
|
| 53 |
mask = Image.fromarray(mask_np).filter(ImageFilter.GaussianBlur(radius=20))
|
| 54 |
|
| 55 |
-
blurred_img = rgba.filter(ImageFilter.GaussianBlur(radius=
|
| 56 |
return Image.composite(blurred_img, rgba, mask), []
|
| 57 |
|
| 58 |
# Create a smooth, feathered mask (0 = original, 255 = blurred)
|
|
@@ -75,8 +80,8 @@ def blur_faces_smooth(image: Image.Image) -> tuple[Image.Image, list]:
|
|
| 75 |
# Heavy blur on mask to feather edges completely smoothly
|
| 76 |
mask = mask.filter(ImageFilter.GaussianBlur(radius=10))
|
| 77 |
|
| 78 |
-
# Generate the blurred version of the face areas
|
| 79 |
-
blurred_img = rgba.filter(ImageFilter.GaussianBlur(radius=
|
| 80 |
|
| 81 |
# Composite the blurred face areas onto the original image
|
| 82 |
return Image.composite(blurred_img, rgba, mask), list(faces)
|
|
|
|
| 21 |
|
| 22 |
def blur_faces_smooth(image: Image.Image) -> tuple[Image.Image, list]:
|
| 23 |
"""
|
| 24 |
+
Detects faces using multiple Haar Cascades (frontal, alt, profile) to ensure
|
| 25 |
+
all faces in group/couple photos are found, and applies a heavy Gaussian blur.
|
|
|
|
| 26 |
"""
|
| 27 |
rgba = image.convert("RGBA")
|
| 28 |
width, height = rgba.size
|
|
|
|
| 32 |
open_cv_image = open_cv_image[:, :, ::-1].copy() # Convert RGB to BGR
|
| 33 |
gray = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2GRAY)
|
| 34 |
|
| 35 |
+
# Load frontal and profile cascades for maximum coverage
|
| 36 |
+
face_cascade_default = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 37 |
+
face_cascade_alt = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
|
| 38 |
+
profile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_profileface.xml')
|
| 39 |
+
|
| 40 |
+
faces_default = face_cascade_default.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=3, minSize=(20, 20))
|
| 41 |
+
faces_alt = face_cascade_alt.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=3, minSize=(20, 20))
|
| 42 |
+
faces_profile = profile_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=3, minSize=(20, 20))
|
| 43 |
+
|
| 44 |
+
# Combine detected faces
|
| 45 |
+
faces = []
|
| 46 |
+
if len(faces_default) > 0: faces.extend(faces_default)
|
| 47 |
+
if len(faces_alt) > 0: faces.extend(faces_alt)
|
| 48 |
+
if len(faces_profile) > 0: faces.extend(faces_profile)
|
| 49 |
|
| 50 |
if len(faces) == 0:
|
| 51 |
# Fallback: soft feathered blur on upper center if no faces detected
|
|
|
|
| 57 |
cv2.ellipse(mask_np, (cx, cy), (rx, ry), 0, 0, 360, 255, -1)
|
| 58 |
mask = Image.fromarray(mask_np).filter(ImageFilter.GaussianBlur(radius=20))
|
| 59 |
|
| 60 |
+
blurred_img = rgba.filter(ImageFilter.GaussianBlur(radius=25))
|
| 61 |
return Image.composite(blurred_img, rgba, mask), []
|
| 62 |
|
| 63 |
# Create a smooth, feathered mask (0 = original, 255 = blurred)
|
|
|
|
| 80 |
# Heavy blur on mask to feather edges completely smoothly
|
| 81 |
mask = mask.filter(ImageFilter.GaussianBlur(radius=10))
|
| 82 |
|
| 83 |
+
# Generate the heavily blurred version of the face areas
|
| 84 |
+
blurred_img = rgba.filter(ImageFilter.GaussianBlur(radius=35))
|
| 85 |
|
| 86 |
# Composite the blurred face areas onto the original image
|
| 87 |
return Image.composite(blurred_img, rgba, mask), list(faces)
|