-
Notifications
You must be signed in to change notification settings - Fork 325
/
mediapipe_facemesh.py
69 lines (61 loc) · 2.23 KB
/
mediapipe_facemesh.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
# mediapipe version of facemesh for verification
import sys
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
# Get input file name
args = sys.argv
if len(args) >= 2:
file = args[1]
else:
file = "man.jpg"
# Video mode
if ".mp4" in file:
capture = cv2.VideoCapture(file)
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
writer = cv2.VideoWriter("mediapipe.mp4", cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), frameSize=(f_w, f_h), fps=20)
# Open mediapipe
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
refine_landmarks=False, # True for attention model
min_detection_confidence=0.5) as face_mesh:
if ".mp4" in file:
# Video mode
while(True):
ret, image = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
annotated_image = image.copy()
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing.DrawingSpec(
color=(0,255,0), thickness=1))
writer.write(annotated_image)
cv2.imshow('frame', annotated_image)
capture.release()
if writer is not None:
writer.release()
else:
# Image mode
image = cv2.imread(file)
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
annotated_image = image.copy()
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing.DrawingSpec(
color=(0,255,0), thickness=1))
cv2.imwrite('output_mediapipe.png', annotated_image)