Spaces:
Configuration error
Configuration error
Update segmentation.py
Browse files- segmentation.py +17 -10
segmentation.py
CHANGED
@@ -69,19 +69,26 @@ def get_blurred_mask(img: Image, body_part_id: int):
|
|
69 |
return dilated_mask_blurred
|
70 |
|
71 |
|
72 |
-
def get_cropped_face(pil_image
|
73 |
-
|
|
|
74 |
face_casc = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
75 |
-
|
76 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
77 |
faces = face_casc.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
|
|
|
|
|
|
78 |
x, y, w, h = faces[0]
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
|
|
|
69 |
return dilated_mask_blurred
|
70 |
|
71 |
|
72 |
+
def get_cropped_face(pil_image: Image):
|
73 |
+
face = get_cropped(pil_image, 11, False)
|
74 |
+
image = np.array(face)
|
75 |
face_casc = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
76 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGBA2GRAY)
|
|
|
77 |
faces = face_casc.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
78 |
+
|
79 |
+
if len(faces) == 0:
|
80 |
+
return pil_image
|
81 |
x, y, w, h = faces[0]
|
82 |
+
cropped_face = image[y:y+h, x:x+w]
|
83 |
+
|
84 |
+
result = Image.new('RGBA', pil_image.size, (255, 255, 255, 0))
|
85 |
+
face_pil = Image.fromarray(cropped_face)
|
86 |
+
if face_pil.size != (w, h):
|
87 |
+
face_pil = face_pil.resize((w, h))
|
88 |
+
|
89 |
+
result.paste(face_pil, (x, y))
|
90 |
+
|
91 |
+
return result
|
92 |
|
93 |
|
94 |
|