Skip to content

Instantly share code, notes, and snippets.

@aredden
Last active March 7, 2022 04:28
Show Gist options
  • Select an option

  • Save aredden/d85dc10737f35bd396f0fb95febe0c3f to your computer and use it in GitHub Desktop.

Select an option

Save aredden/d85dc10737f35bd396f0fb95febe0c3f to your computer and use it in GitHub Desktop.
Non-rectangular face removal using cv2 convextHull, mediapipe FaceMesh, and assorted numpy magic.
import cv2
import numpy as np
import mediapipe as mp
image = cv2.imread("./input.png")
mask = np.zeros_like(image)
segm = mp.solutions.mediapipe.python.solutions.face_mesh.FaceMesh(
True,
min_detection_confidence=0.6
)
landmarks = segm.process(image).multi_face_landmarks
h,w = image.shape[:2]
landmark_array = np.empty((0, 2), np.uint8)
for i, landmark in enumerate(landmarks[0].landmark):
landmark_x = min(int(landmark.x * w), w - 1)
landmark_y = min(int(landmark.y * h), h - 1)
landmark_point = [np.array((landmark_x, landmark_y))]
landmark_array = np.append(landmark_array, landmark_point, axis=0)
convexhull = cv2.convexHull(landmark_array)
cv2.fillConvexPoly(mask, convexhull, (255, 255, 255))
image[mask[:,:,0]!=0] = [255, 255, 255]
cv2.imwrite("output.png", image)
segm.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment