-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathestimator.py
92 lines (70 loc) · 3.25 KB
/
estimator.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
import math
import cv2
import mediapipe as mp
import numpy as np
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5,
min_tracking_confidence=0.5)
cap = cv2.VideoCapture(0)
def rotation_matrix_to_angles(rotation_matrix):
"""
Calculate Euler angles from rotation matrix.
:param rotation_matrix: A 3*3 matrix with the following structure
[Cosz*Cosy Cosz*Siny*Sinx - Sinz*Cosx Cosz*Siny*Cosx + Sinz*Sinx]
[Sinz*Cosy Sinz*Siny*Sinx + Sinz*Cosx Sinz*Siny*Cosx - Cosz*Sinx]
[ -Siny CosySinx Cosy*Cosx ]
:return: Angles in degrees for each axis
"""
x = math.atan2(rotation_matrix[2, 1], rotation_matrix[2, 2])
y = math.atan2(-rotation_matrix[2, 0], math.sqrt(rotation_matrix[0, 0] ** 2 +
rotation_matrix[1, 0] ** 2))
z = math.atan2(rotation_matrix[1, 0], rotation_matrix[0, 0])
return np.array([x, y, z]) * 180. / math.pi
while cap.isOpened():
success, image = cap.read()
# Convert the color space from BGR to RGB and get Mediapipe results
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_mesh.process(image)
# Convert the color space from RGB to BGR to display well with Opencv
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
face_coordination_in_real_world = np.array([
[285, 528, 200],
[285, 371, 152],
[197, 574, 128],
[173, 425, 108],
[360, 574, 128],
[391, 425, 108]
], dtype=np.float64)
h, w, _ = image.shape
face_coordination_in_image = []
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
if idx in [1, 9, 57, 130, 287, 359]:
x, y = int(lm.x * w), int(lm.y * h)
face_coordination_in_image.append([x, y])
face_coordination_in_image = np.array(face_coordination_in_image,
dtype=np.float64)
# The camera matrix
focal_length = 1 * w
cam_matrix = np.array([[focal_length, 0, w / 2],
[0, focal_length, h / 2],
[0, 0, 1]])
# The Distance Matrix
dist_matrix = np.zeros((4, 1), dtype=np.float64)
# Use solvePnP function to get rotation vector
success, rotation_vec, transition_vec = cv2.solvePnP(
face_coordination_in_real_world, face_coordination_in_image,
cam_matrix, dist_matrix)
# Use Rodrigues function to convert rotation vector to matrix
rotation_matrix, jacobian = cv2.Rodrigues(rotation_vec)
result = rotation_matrix_to_angles(rotation_matrix)
for i, info in enumerate(zip(('pitch', 'yaw', 'roll'), result)):
k, v = info
text = f'{k}: {int(v)}'
cv2.putText(image, text, (20, i*30 + 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 0, 200), 2)
cv2.imshow('Head Pose Angles', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()