lattepanda mu 小主机组合 附加头部姿态检测演示和代码

2024-05-032930
AI 快速预览详细 收起
本文介绍了如何使用LattePanda MU小主机组合,结合USB显示器和3D打印支架,搭建一个桌面小电脑,并通过Mediapipe库实现头部姿态检测。项目展示了具体的硬件清单、最终效果以及难度等级。适用于STEM教育和AI启蒙玩具,让孩子在编程中获得成就感。
之前帖子提到的usb显示器
lattepanda mu+lite载板+usb显示器+自制3D打印支架
变成了完整的桌面小电脑
lattepanda mu 小主机组合 附加头部姿态检测演示和代码图1
很适合跑一些写好的项目,比如这个mediapipe检测的头部姿态
  1. import math
  2. import cv2
  3. import mediapipe as mp
  4. import numpy as np
  5. mp_face_mesh = mp.solutions.face_mesh
  6. face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5,
  7.                                   min_tracking_confidence=0.5)
  8. cap = cv2.VideoCapture(0)
  9. def rotation_matrix_to_angles(rotation_matrix):
  10.     """
  11.     Calculate Euler angles from rotation matrix.
  12.     :param rotation_matrix: A 3*3 matrix with the following structure
  13.     [Cosz*Cosy  Cosz*Siny*Sinx - Sinz*Cosx  Cosz*Siny*Cosx + Sinz*Sinx]
  14.     [Sinz*Cosy  Sinz*Siny*Sinx + Sinz*Cosx  Sinz*Siny*Cosx - Cosz*Sinx]
  15.     [  -Siny             CosySinx                   Cosy*Cosx         ]
  16.     :return: Angles in degrees for each axis
  17.     """
  18.     x = math.atan2(rotation_matrix[2, 1], rotation_matrix[2, 2])
  19.     y = math.atan2(-rotation_matrix[2, 0], math.sqrt(rotation_matrix[0, 0] ** 2 +
  20.                                                      rotation_matrix[1, 0] ** 2))
  21.     z = math.atan2(rotation_matrix[1, 0], rotation_matrix[0, 0])
  22.     return np.array([x, y, z]) * 180. / math.pi
  23. while cap.isOpened():
  24.     success, image = cap.read()
  25.     # Convert the color space from BGR to RGB and get Mediapipe results
  26.     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  27.     results = face_mesh.process(image)
  28.     # Convert the color space from RGB to BGR to display well with Opencv
  29.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  30.     face_coordination_in_real_world = np.array([
  31.         [285, 528, 200],
  32.         [285, 371, 152],
  33.         [197, 574, 128],
  34.         [173, 425, 108],
  35.         [360, 574, 128],
  36.         [391, 425, 108]
  37.     ], dtype=np.float64)
  38.     h, w, _ = image.shape
  39.     face_coordination_in_image = []
  40.     if results.multi_face_landmarks:
  41.         for face_landmarks in results.multi_face_landmarks:
  42.             for idx, lm in enumerate(face_landmarks.landmark):
  43.                 if idx in [1, 9, 57, 130, 287, 359]:
  44.                     x, y = int(lm.x * w), int(lm.y * h)
  45.                     face_coordination_in_image.append([x, y])
  46.             face_coordination_in_image = np.array(face_coordination_in_image,
  47.                                                   dtype=np.float64)
  48.             # The camera matrix
  49.             focal_length = 1 * w
  50.             cam_matrix = np.array([[focal_length, 0, w / 2],
  51.                                    [0, focal_length, h / 2],
  52.                                    [0, 0, 1]])
  53.             # The Distance Matrix
  54.             dist_matrix = np.zeros((4, 1), dtype=np.float64)
  55.             # Use solvePnP function to get rotation vector
  56.             success, rotation_vec, transition_vec = cv2.solvePnP(
  57.                 face_coordination_in_real_world, face_coordination_in_image,
  58.                 cam_matrix, dist_matrix)
  59.             # Use Rodrigues function to convert rotation vector to matrix
  60.             rotation_matrix, jacobian = cv2.Rodrigues(rotation_vec)
  61.             result = rotation_matrix_to_angles(rotation_matrix)
  62.             for i, info in enumerate(zip(('pitch', 'yaw', 'roll'), result)):
  63.                 k, v = info
  64.                 text = f'{k}: {int(v)}'
  65.                 cv2.putText(image, text, (20, i*30 + 20),
  66.                             cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 0, 200), 2)
  67.     cv2.imshow('Head Pose Angles', image)
  68.     if cv2.waitKey(5) & 0xFF == 27:
  69.         break
  70. cap.release()
复制代码
演示视频可见:

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -