Skip to content

Instantly share code, notes, and snippets.

@Roger8
Created July 17, 2019 03:07
Show Gist options
  • Save Roger8/7144cb012be670b230f88ca9cd63e536 to your computer and use it in GitHub Desktop.
Save Roger8/7144cb012be670b230f88ca9cd63e536 to your computer and use it in GitHub Desktop.
根据人脸3d landmarks 大概计算人脸方向

(code)[https://gist.github.com/zalo/71fbd5dbfe23cb46406d211b84be9f7e]

            # Construct a "rotation" matrix (strong simplification, might have shearing)
            rotationMatrix = np.empty((3,3))
            # 脸颊两点连线为x轴
            rotationMatrix[0,:] = (centered[16] - centered[0])/np.linalg.norm(centered[16] - centered[0])
            # 下巴眉心两点连线为y轴
            rotationMatrix[1,:] = (centered[8] - centered[27])/np.linalg.norm(centered[8] - centered[27])
            # 上面两条线所在平面的法向量为z轴( 所以用叉乘)
            rotationMatrix[2,:] = np.cross(rotationMatrix[0, :], rotationMatrix[1, :])
            # 所有点的中心为原点,由此构建一个三维坐标系
            
def RotMat2Euler(R):
    ''' 根据旋转矩阵计算各个轴旋转角度 '''
    E2 = -np.arcsin(R[0,2])   #
    E1 = np.arctan2(R[1,2]/np.cos(E2), R[2,2]/np.cos(E2)) * 180 / np.pi
    E3 = np.arctan2(R[0,1]/np.cos(E2), R[1,1]/np.cos(E2)) * 180 / np.pi
    E2 = E2* 180 / np.pi
    return (E1,E2,E3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment