Spaces:
Build error
Build error
| import os | |
| os.environ["MEDIAPIPE_DISABLE_GPU"] = "1" | |
| import cv2 as cv | |
| import mediapipe as mp | |
| assert hasattr(mp, "solutions"), "MediaPipe solutions not available" | |
| class FaceMeshGenerator: | |
| def __init__(self, static_mode=False, max_faces=1, refine_landmarks=True): | |
| self.mp_face_mesh = mp.solutions.face_mesh | |
| self.face_mesh = self.mp_face_mesh.FaceMesh( | |
| static_image_mode=static_mode, | |
| max_num_faces=max_faces, | |
| refine_landmarks=refine_landmarks, | |
| min_detection_confidence=0.5, | |
| min_tracking_confidence=0.5 | |
| ) | |
| def create_face_mesh(self, frame, draw=True): | |
| img_rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB) | |
| results = self.face_mesh.process(img_rgb) | |
| landmarks = [] | |
| if results.multi_face_landmarks: | |
| for face_landmarks in results.multi_face_landmarks: | |
| h, w, _ = frame.shape | |
| for lm in face_landmarks.landmark: | |
| x, y = int(lm.x * w), int(lm.y * h) | |
| landmarks.append((x, y)) | |
| if draw: | |
| for point in landmarks: | |
| cv.circle(frame, point, 1, (0, 255, 0), cv.FILLED) | |
| return frame, landmarks | |