altayavci commited on
Commit
037f136
1 Parent(s): f10d591

Update segmentation.py

Browse files
Files changed (1) hide show
  1. 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 : Image):
73
- image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_BGR2RGB)
 
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
- cropped_face = np.ones_like(np.array(pil_image))*255
81
- cropped_face[y:y+h, x:x+w] = image[y:y+h, x:x+w]
82
-
83
- img = cv2.cvtColor(cropped_face, cv2.COLOR_BGR2RGB)
84
- return Image.fromarray(img.astype("uint8")).convert("RGB")
 
 
 
 
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