File size: 1,248 Bytes
b10a28a
 
 
 
f62bf7a
 
 
5dca91c
 
 
f62bf7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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