You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于PyVista的3D人脸网格朝向相机对齐的旋转实现求助

3D人脸网格旋转对齐实现方案

核心思路

要实现双眼沿Y轴对齐并让人脸正对相机,需分两步完成旋转:

  • 第一步:将双眼连线旋转至平行于Y轴
  • 第二步:调整人脸朝向,确保正面正对Z轴方向(相机默认观测方向)

具体实现步骤

  1. 获取平移后的左眼、右眼坐标(此时鼻尖已在原点)
  2. 计算双眼连线向量,生成将其旋转到Y轴的旋转矩阵
  3. 基于双眼中点到鼻尖的向量修正人脸朝向,生成绕Y轴的旋转矩阵
  4. 合并两个旋转矩阵,应用到平移后的网格所有点

完整代码实现

import pyvista as pv
import numpy as np
import yaml
from scipy.spatial.transform import Rotation as R

# Path to the OBJ file
obj_file = "checkpoints/custom/results/examples/epoch_20_000000/000002.obj"

# Load the points from the YAML file
with open('selected_point_ids.yaml', 'r') as file:
    data = yaml.safe_load(file)
    point_ids = data['point_ids']

# Read the OBJ file
mesh = pv.read(obj_file)

# Retrieve the coordinates of the selected points from the mesh
selected_points = mesh.points[point_ids]

# Assume the points are [nose_tip, left_eye, right_eye]
nose_tip = selected_points[0]
left_eye = selected_points[1]
right_eye = selected_points[2]

# Translation: Move the nose tip to the origin (0, 0, 0)
translation_vector = -nose_tip
translated_mesh = mesh.copy()
translated_mesh.points = mesh.points + translation_vector

# 获取平移后的特征点坐标
translated_left_eye = left_eye + translation_vector
translated_right_eye = right_eye + translation_vector

# 1. 计算双眼连线向量并归一化
eye_line_vec = translated_right_eye - translated_left_eye
eye_line_vec = eye_line_vec / np.linalg.norm(eye_line_vec)

# 目标Y轴方向向量
target_y = np.array([0, 1, 0])

# 2. 生成将双眼连线转到Y轴的旋转矩阵
rotation_axis = np.cross(eye_line_vec, target_y)
# 处理双眼连线已平行于Y轴的特殊情况
if np.linalg.norm(rotation_axis) == 0:
    rotation_axis = np.array([1, 0, 0])
else:
    rotation_axis = rotation_axis / np.linalg.norm(rotation_axis)
rotation_angle = np.arccos(np.dot(eye_line_vec, target_y))

rot1 = R.from_rotvec(rotation_angle * rotation_axis)
rot1_matrix = rot1.as_matrix()

# 3. 修正人脸朝向,确保正面正对Z轴
eye_mid_point = (translated_left_eye + translated_right_eye) / 2
# 鼻尖指向双眼中点的反方向为人脸正面
face_front_vec = -eye_mid_point
# 应用第一步旋转后的正面向量
face_front_vec_rotated = rot1_matrix @ face_front_vec
face_front_vec_rotated = face_front_vec_rotated / np.linalg.norm(face_front_vec_rotated)

target_z = np.array([0, 0, 1])
# 取X-Z平面投影计算旋转角度
current_xz = np.array([face_front_vec_rotated[0], face_front_vec_rotated[2]])
current_xz = current_xz / np.linalg.norm(current_xz)
target_xz = np.array([0, 1])
angle_z = np.arccos(np.dot(current_xz, target_xz))
# 判断旋转方向,避免镜像
if current_xz[0] > 0:
    angle_z = -angle_z

rot2 = R.from_euler('y', angle_z, degrees=False)
rot2_matrix = rot2.as_matrix()

# 合并旋转矩阵
final_rot_matrix = rot2_matrix @ rot1_matrix

# 应用旋转到网格
aligned_mesh = translated_mesh.copy()
aligned_mesh.points = (final_rot_matrix @ aligned_mesh.points.T).T

# 可视化验证
plotter = pv.Plotter()
plotter.add_mesh(aligned_mesh, color='white')
plotter.add_axes()
plotter.show()

关键细节说明

  • 旋转顺序:先对齐双眼到Y轴,再绕Y轴调整朝向,保证最终姿态符合要求
  • 向量归一化:所有参与计算的向量均做归一化,避免尺度误差导致旋转错误
  • 特殊情况处理:当双眼连线已平行于Y轴时,手动指定旋转轴避免除以零异常

内容的提问来源于stack exchange,提问作者colt.exe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:23:12