【问题标题】:Calculate Z Rotation of a face in Tensorflow.js在 Tensorflow.js 中计算人脸的 Z 旋转
【发布时间】:2021-10-02 15:18:09
【问题描述】:

注意:本题NOTHING与 Three.js 无关,只是 Tensorflow.js 和 Trigonometry。

我正在尝试通过旋转我的脸来旋转 Three.js 中的 3D 对象。我用这个code by akhirai560 在 X 和 Y 轴上旋转。:

function normal(vec) {
  let norm = 0;
  for (const v of vec) {
    norm += v * v;
  }
  return Math.sqrt(norm);
}

function getHeadAnglesCos(keypoints) {
  // Vertical (Y-Axis) Rotation
  const faceVerticalCentralPoint = [
    0,
    (keypoints[10][1] + keypoints[152][1]) * 0.5,
    (keypoints[10][2] + keypoints[152][2]) * 0.5,
  ];
  const verticalAdjacent = keypoints[10][2] - faceVerticalCentralPoint[2];
  const verticalOpposite = keypoints[10][1] - faceVerticalCentralPoint[1];
  const verticalHypotenuse = normal([verticalAdjacent, verticalOpposite]);
  const verticalCos = verticalAdjacent / verticalHypotenuse;

  // Horizontal (X-Axis) Rotation
  const faceHorizontalCentralPoint = [
    (keypoints[226][0] + keypoints[446][0]) * 0.5,
    0,
    (keypoints[226][2] + keypoints[446][2]) * 0.5,
  ];
  const horizontalAdjacent = keypoints[226][2] - faceHorizontalCentralPoint[2];
  const horizontalOpposite = keypoints[226][0] - faceHorizontalCentralPoint[0];
  const horizontalHypotenuse = normal([horizontalAdjacent, horizontalOpposite]);
  const horizontalCos = horizontalAdjacent / horizontalHypotenuse;

  return [horizontalCos, verticalCos];
}

它通过找到这些点的 cos (original image source) 来计算旋转:

我还想计算 Z 轴旋转的 cos。谢谢!

【问题讨论】:

  • 如果你使用 ThreeJS 来建模人脸,为什么不直接使用 ThreeJS 来旋转网格呢?
  • @Trentium 这个问题与three.js无关,我不是在创建面部克隆。我只是用三角法计算面部的旋转。

标签: javascript trigonometry tensorflow.js


【解决方案1】:

我并不完全清楚问题的最终目标,但如果只是想绕 Z 轴旋转,下面的 sn-p 会有所帮助。

center = { x: 50, y: 50 };

points = [
  { x: 60, y:  60 },
  { x: 50, y: 100 }
]

function rotateAboutZ( xyRotationPoint, points, zRotation ) {

  let result = points.map( p => {
    // Adjust the point based on the center of rotation.
    let x = p.x - xyRotationPoint.x;
    let y = p.y - xyRotationPoint.y;
    // Calculate the radius and angle in preparation for rotation
    // about the Z axis.
    let radius = Math.sqrt( x * x + y * y );
    let angle = Math.atan2( y, x );
    // Adjust the angle by the requested rotation.
    let angleRotated = angle + zRotation;
    // Finally, calculate the new XY coordinates, and re-adjust 
    // based on the center of rotation.
    let xRotated = radius * Math.cos( angleRotated ) + xyRotationPoint.x;
    let yRotated = radius * Math.sin( angleRotated ) + xyRotationPoint.y;
    return { x: xRotated, y: yRotated };
  } );
  
  return result;

}

// Let's rotate the points by 180 degrees around the
// center point of (50,50).
let result = rotateAboutZ( center, points, Math.PI );
console.log( result );

【讨论】:

  • @Iwo 第 3 行的点只是一些随机测试点,它们被输入rotateAboutZ 函数。这两个测试点代表构成面部的实际点阵列。
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 2011-06-28
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2016-09-21
相关资源
最近更新 更多