-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacelandmark_media.py
51 lines (39 loc) · 1.74 KB
/
facelandmark_media.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
# IMPORTING LIBRARIES
import cv2
import mediapipe as mp
# INITIALIZING OBJECTS
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
drawing_spec = mp_drawing.DrawingSpec(thickness = 1, circle_radius = 1)
cap = cv2.VideoCapture(r'D:\computer_vision\data\171124_C1_HD_002.mp4')
# DETECT THE FACE LANDMARKS
with mp_face_mesh.FaceMesh(min_detection_confidence = 0.5, min_tracking_confidence = 0.5) as face_mesh:
while True:
success, image = cap.read()
# Flip the image horizontally and convert the color space from BGR to RGB
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance
image.flags.writeable = False
# Detect the face landmarks
results = face_mesh.process(image)
# To improve performance
image.flags.writeable = True
# Convert back to the BGR color space
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Draw the face mesh annotations on the image.
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image = image,
landmark_list = face_landmarks,
connections = mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec = None,
connection_drawing_spec = mp_drawing_styles
.get_default_face_mesh_tesselation_style())
# Display the image
cv2.imshow('MediaPipe FaceMesh', image)
# Terminate the process
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()