lattepanda mu 小主机组合 附加头部姿态检测演示和代码
之前帖子提到的usb显示器lattepanda mu+lite载板+usb显示器+自制3d打印支架
变成了完整的桌面小电脑
很适合跑一些写好的项目,比如这个mediapipe检测的头部姿态
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
[-Siny CosySinx Cosy*Cosx ]
:return: Angles in degrees for each axis
"""
x = math.atan2(rotation_matrix, rotation_matrix)
y = math.atan2(-rotation_matrix, math.sqrt(rotation_matrix ** 2 +
rotation_matrix ** 2))
z = math.atan2(rotation_matrix, rotation_matrix)
return np.array() * 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([
,
,
,
,
,
], 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 :
x, y = int(lm.x * w), int(lm.y * h)
face_coordination_in_image.append()
face_coordination_in_image = np.array(face_coordination_in_image,
dtype=np.float64)
# The camera matrix
focal_length = 1 * w
cam_matrix = np.array([,
,
])
# 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()
演示视频可见:
https://www.bilibili.com/video/BV1EZ421J7xr/?spm_id_from=333.999.0.0
页:
[1]