fix: use cv2.convexHull() (#358)

This commit is contained in:
Won-Kyu Park
2023-10-10 23:47:25 +09:00
committed by GitHub
parent d47983a151
commit fb3334dc8b

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
from functools import partial
import cv2
import numpy as np
from PIL import Image, ImageDraw
@@ -67,23 +68,6 @@ def mediapipe_face_detection(
return PredictOutput(bboxes=bboxes, masks=masks, preview=preview)
def get_convexhull(points: np.ndarray) -> list[tuple[int, int]]:
"""
Parameters
----------
points: An ndarray of shape (n, 2) containing the 2D points.
Returns
-------
list[tuple[int, int]]: Input for the draw.polygon function
"""
from scipy.spatial import ConvexHull
hull = ConvexHull(points)
vertices = hull.vertices
return list(zip(points[vertices, 0], points[vertices, 1]))
def mediapipe_face_mesh(image: Image.Image, confidence: float = 0.3) -> PredictOutput:
import mediapipe as mp
@@ -114,8 +98,8 @@ def mediapipe_face_mesh(image: Image.Image, confidence: float = 0.3) -> PredictO
connection_drawing_spec=drawing_styles.get_default_face_mesh_tesselation_style(),
)
points = np.array([(land.x * w, land.y * h) for land in landmarks.landmark])
outline = get_convexhull(points)
points = np.intp([(land.x * w, land.y * h) for land in landmarks.landmark])
outline = cv2.convexHull(points).reshape(-1).tolist()
mask = Image.new("L", image.size, "black")
draw = ImageDraw.Draw(mask)
@@ -152,11 +136,11 @@ def mediapipe_face_mesh_eyes_only(
masks = []
for landmarks in pred.multi_face_landmarks:
points = np.array([(land.x * w, land.y * h) for land in landmarks.landmark])
points = np.intp([(land.x * w, land.y * h) for land in landmarks.landmark])
left_eyes = points[left_idx]
right_eyes = points[right_idx]
left_outline = get_convexhull(left_eyes)
right_outline = get_convexhull(right_eyes)
left_outline = cv2.convexHull(left_eyes).reshape(-1).tolist()
right_outline = cv2.convexHull(right_eyes).reshape(-1).tolist()
mask = Image.new("L", image.size, "black")
draw = ImageDraw.Draw(mask)