Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3645038
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-04-11 13:35:40

def get_head_pose(shape):  # 头部姿态估计

    # (像素坐标集合)填写2D参考点

    # 17左眉左上角/21左眉右角/22右眉左上角/26右眉右上角/36左眼左上角/39左眼右上角/42右眼左上角/

    # 45右眼右上角/31鼻子左上角/35鼻子右上角/48左上角/54嘴右上角/57嘴中央下角/8下巴角

    image_pts = np.float32([shape[17], shape[21], shape[22], shape[26], shape[36],

                            shape[39], shape[42], shape[45], shape[31], shape[35],

                            shape[48], shape[54], shape[57], shape[8]])

    # solvePnP计算姿势——求解旋转和平移矩阵:

    # rotation_vec表示旋转矩阵,translation_vec表示平移矩阵,cam_matrixK矩阵对应,dist_coeffsD矩阵对应。

    _, rotation_vec, translation_vec = cv2.solvePnP(object_pts, image_pts, cam_matrix, dist_coeffs)

    # projectPoints重新投影误差:原2d点和重投影2d点的距离(输入3d点、相机内参、相机畸变、rt,输出重投影2d点)

    reprojectdst, _ = cv2.projectPoints(reprojectsrc, rotation_vec, translation_vec, cam_matrix, dist_coeffs)

    reprojectdst = tuple(map(tuple, reprojectdst.reshape(8, 2)))  # 82列显示

    # 计算欧拉角calc euler angle

    rotation_mat, _ = cv2.Rodrigues(rotation_vec)  # 罗德里格斯公式(将旋转矩阵转换为旋转向量)

    pose_mat = cv2.hconcat((rotation_mat, translation_vec))  # 水平拼接,vconcat垂直拼接

    # decomposeProjectionMatrix将投影矩阵分解为旋转矩阵和相机矩阵

    _, _, _, _, _, _, euler_angle =外汇跟单gendan5.com cv2.decomposeProjectionMatrix(pose_mat)

    pitch, yaw, roll = [math.radians(_) for _ in euler_angle]

    pitch = math.degrees(math.asin(math.sin(pitch)))

    roll = -math.degrees(math.asin(math.sin(roll)))

    yaw = math.degrees(math.asin(math.sin(yaw)))

    print('pitch:{}, yaw:{}, roll:{}'.format(pitch, yaw, roll))

    return reprojectdst, euler_angle  # 投影误差,欧拉角

def eye_aspect_ratio(eye):

    # 垂直眼标志(XY)坐标

    A = dist.euclidean(eye[1], eye[5])  # 计算两个集合之间的欧式距离

    B = dist.euclidean(eye[2], eye[4])

    # 计算水平之间的欧几里得距离

    # 水平眼标志(XY)坐标

    C = dist.euclidean(eye[0], eye[3])

    # 眼睛长宽比的计算

    ear = (A + B) / (2.0 * C)

    # 返回眼睛的长宽比

    return ear

def mouth_aspect_ratio(mouth):  # 嘴部

    A = np.linalg.norm(mouth[2] - mouth[9])  # 51, 59

    B = np.linalg.norm(mouth[4] - mouth[7])  # 53, 57

    C = np.linalg.norm(mouth[0] - mouth[6])  # 49, 55

    mar = (A + B) / (2.0 * C)

    return mar

阅读(590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~