forked from mmmmmm44/VTuber-Python-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacial_landmark.py
105 lines (77 loc) · 3.2 KB
/
facial_landmark.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
For finding the face and face landmarks for further manipulication
"""
import cv2
import mediapipe as mp
import numpy as np
class FaceMeshDetector:
def __init__(self,
static_image_mode=False,
max_num_faces=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5):
self.static_image_mode = static_image_mode
self.max_num_faces = max_num_faces
self.min_detection_confidence = min_detection_confidence
self.min_tracking_confidence = min_tracking_confidence
# Facemesh
self.mp_face_mesh = mp.solutions.face_mesh
# The object to do the stuffs
self.face_mesh = self.mp_face_mesh.FaceMesh(
self.static_image_mode,
self.max_num_faces,
True,
self.min_detection_confidence,
self.min_tracking_confidence
)
self.mp_drawing = mp.solutions.drawing_utils
self.drawing_spec = self.mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
def findFaceMesh(self, img, draw=True):
# convert the img from BRG to RGB
img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
img.flags.writeable = False
self.results = self.face_mesh.process(img)
# Draw the face mesh annotations on the image.
img.flags.writeable = True
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
self.imgH, self.imgW, self.imgC = img.shape
self.faces = []
if self.results.multi_face_landmarks:
for face_landmarks in self.results.multi_face_landmarks:
if draw:
self.mp_drawing.draw_landmarks(
image = img,
landmark_list = face_landmarks,
connections = self.mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec = self.drawing_spec,
connection_drawing_spec = self.drawing_spec)
face = []
for id, lmk in enumerate(face_landmarks.landmark):
x, y = int(lmk.x * self.imgW), int(lmk.y * self.imgH)
face.append([x, y])
# show the id of each point on the image
# cv2.putText(img, str(id), (x-4, y-4), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1, cv2.LINE_AA)
self.faces.append(face)
return img, self.faces
# sample run of the module
def main():
detector = FaceMeshDetector()
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, img = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
img, faces = detector.findFaceMesh(img)
# if faces:
# print(faces[0])
cv2.imshow('MediaPipe FaceMesh', img)
# press "q" to leave
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
if __name__ == "__main__":
# demo code
main()