import cv2 import mediapipe as mp import numpy as np mp_face_mesh = mp.solutions.face_mesh def get_forehead_mask(img): with mp_face_mesh.FaceMesh(static_image_mode=True) as face_mesh: results = face_mesh.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) mask = np.zeros(img.shape[:2], dtype=np.uint8) if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: points = [] for i in [10, 338, 297, 332, 284, 251, 389, 356, 454]: lm = face_landmarks.landmark[i] x = int(lm.x * img.shape[1]) y = int(lm.y * img.shape[0]) points.append((x, y)) points = np.array([points], dtype=np.int32) cv2.fillPoly(mask, points, 255) return mask