Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

calculate head rotation vectors from mediapipe 3d data

Joned51b0b632a4
diff --git a/jon_scratch/calculate_head_rotation_matrix.ipynb b/jon_scratch/calculate_head_rotation_matrix.ipynb
new file mode 100644
index 00000000..2494d6e0
--- /dev/null
+++ b/jon_scratch/calculate_head_rotation_matrix.ipynb
@@ -0,0 +1,528 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Calculate head rotation matricies\n",
+ "\n",
+ "calculate orthonormal basis vectors of the head on each frame, stack them on top of each other to make 3x3 rotation matricies, because apparently that's how that works?!\n",
+ "\n",
+ "1 - calculate `head_center_xyz` as mean of left and right ear markers\n",
+ "2 - define x+ as vector between `head_center_xyz` and `nose_xyz`- normalize to get `x_hat`\n",
+ "3 - define y+ as vector between `head_center_xyz` and `left_ear_xyz` - normalize to get `y_hat_temp` (will be replaced later, to ensure orthogonal \\)\n",
+ "4 - define z+ as cross product of `x_hat` and `y_hat`\n",
+ "5 - replace `y_hat_temp` with `z_hat` crossed wtih `x_hat` (Pretty sure that makes it right handed...)\n",
+ "\n",
+ "Stack x_hat, y_hat, and z_hat on top of each other, and that's your head rotation matrix on that frame\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: keyboard in c:\\users\\jonma\\miniconda3\\envs\\humon-env\\lib\\site-packages (0.13.5)\n"
+ ]
+ }
+ ],
+ "source": [
+ "!pip install numpy\n",
+ "!pip install matplotlib\n",
+ "!pip install pyqt5\n",
+ "!pip install keyboard"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\n",
+ "import numpy as np\n",
+ "from pathlib import Path\n",
+ "\n",
+ "import matplotlib.pyplot as plt\n",
+ "import keyboard"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: pyqt5 in c:\\users\\jonma\\miniconda3\\envs\\humon-env\\lib\\site-packages (5.15.6)\n",
+ "Requirement already satisfied: PyQt5-Qt5>=5.15.2 in c:\\users\\jonma\\miniconda3\\envs\\humon-env\\lib\\site-packages (from pyqt5) (5.15.2)\n",
+ "Requirement already satisfied: PyQt5-sip<13,>=12.8 in c:\\users\\jonma\\miniconda3\\envs\\humon-env\\lib\\site-packages (from pyqt5) (12.10.1)\n"
+ ]
+ }
+ ],
+ "source": [
+ "debug = True\n",
+ "# %matplotlib widget\n",
+ "import matplotlib.pyplot as plt\n",
+ "%matplotlib widget\n",
+ "# %matplotlib qt"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "\n",
+ "### get yr paths right\n",
+ "session_id = 'sesh_2022-02-15_11_54_28_pupil_maybe'\n",
+ "\n",
+ "freemocap_data_folder = Path('C:/Users/jonma/Dropbox/FreeMoCapProject/FreeMocap_Data')\n",
+ "\n",
+ "session_folder_path = freemocap_data_folder / session_id\n",
+ "\n",
+ "mediapipe_skeleton_data_path = session_folder_path / 'DataArrays'/ 'mediaPipeSkel_3d_smoothed.npy'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\n",
+ " ### load in data\n",
+ "\n",
+ "- full body 3d kinematic data - `mediapipe_skeleton_frame_mar_xyz`\n",
+ " - type: numpy array\n",
+ " - dimensions: [frame_number, tracked_point_number, dimension (xyz)]\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "mediapipe_skeleton_fr_mar_xyz = np.load(mediapipe_skeleton_data_path)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "nose_id= 0\n",
+ "right_ear_id = 8\n",
+ "left_ear_id = 7\n",
+ "\n",
+ "nose_xyz = mediapipe_skeleton_fr_mar_xyz[:, nose_id, :]\n",
+ "right_ear_xyz = mediapipe_skeleton_fr_mar_xyz[:, right_ear_id, :]\n",
+ "left_ear_xyz = mediapipe_skeleton_fr_mar_xyz[:, left_ear_id, :]\n",
+ "\n",
+ "head_center_xyz = (right_ear_xyz + left_ear_xyz) / 2 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fig = plt.figure(num=123)\n",
+ "ax = fig.add_subplot(111)\n",
+ "ax.plot(nose_xyz[:,0], label='nose_x')\n",
+ "ax.plot(nose_xyz[:,1], label='nose_y')\n",
+ "ax.plot(nose_xyz[:,2], label='nose_z')\n",
+ "ax.plot(right_ear_xyz[:,0], label='right_ear_x')\n",
+ "ax.plot(right_ear_xyz[:,1], label='right_ear_y')\n",
+ "ax.plot(right_ear_xyz[:,2], label='right_ear_z')\n",
+ "ax.plot(left_ear_xyz[:,0], label='left_ear_x')\n",
+ "ax.plot(left_ear_xyz[:,1], label='left_ear_y')\n",
+ "ax.plot(left_ear_xyz[:,2], label='left_ear_z')\n",
+ "ax.plot(head_center_xyz[:,0], label='head_center_x')\n",
+ "ax.plot(head_center_xyz[:,1], label='head_center_y')\n",
+ "ax.plot(head_center_xyz[:,2], label='head_center_z')\n",
+ "ax.legend()\n",
+ "plt.show()\n",
+ "plt.pause(.1)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "zero everything out, aka pin it to the origin, aka \"subtract the position of the thing you want to be the origin from everything else\", aka \"do the thing that makes the X,Y,Z coordinates of the thing that should be (0,0,0) equal to (0,0,0)\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " ...,\n",
+ " [ 12.47374782, 69.0853272 , -22.37502357],\n",
+ " [ 14.10629544, 60.84139366, -6.04679083],\n",
+ " [ 25.76475466, 63.75538109, -21.2651289 ]])"
+ ]
+ },
+ "execution_count": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "z_nose_xyz = nose_xyz - head_center_xyz\n",
+ "z_nose_x = z_nose_xyz[:,0]\n",
+ "z_nose_y = z_nose_xyz[:,1]\n",
+ "z_nose_z = z_nose_xyz[:,2]\n",
+ "\n",
+ "z_left_ear_xyz = left_ear_xyz - head_center_xyz\n",
+ "z_left_ear_x = z_left_ear_xyz[:,0]\n",
+ "z_left_ear_y = z_left_ear_xyz[:,1]\n",
+ "z_left_ear_z = z_left_ear_xyz[:,2]\n",
+ "\n",
+ "z_nose_xyz"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Normalize so they are unit vectors"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "z_norm_nose_xyz = z_nose_xyz / np.linalg.norm(z_nose_xyz, axis=1)[:,None]\n",
+ "z_norm_nose_x = z_norm_nose_xyz[:,0]\n",
+ "z_norm_nose_y = z_norm_nose_xyz[:,1]\n",
+ "z_norm_nose_z = z_norm_nose_xyz[:,2]\n",
+ "\n",
+ "z_norm_left_ear_xyz = z_left_ear_xyz / np.linalg.norm(z_left_ear_xyz, axis=1)[:,None]\n",
+ "z_norm_left_ear_x = z_norm_left_ear_xyz[:,0]\n",
+ "z_norm_left_ear_y = z_norm_left_ear_xyz[:,1]\n",
+ "z_norm_left_ear_z = z_norm_left_ear_xyz[:,2]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "define x_hat as the zero'd out nose vector (i.e. the +X axis of the head starts at the head center and goes out the person's nose)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " ...,\n",
+ " [ 0.16929195, 0.93761632, -0.30367067],\n",
+ " [ 0.22481111, 0.96962534, -0.09636731],\n",
+ " [ 0.35795511, 0.8857668 , -0.29544087]])"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "x_hat_xyz = z_norm_nose_xyz\n",
+ "x_hat_x = x_hat_xyz[:,0]\n",
+ "x_hat_y = x_hat_xyz[:,1]\n",
+ "x_hat_z = x_hat_xyz[:,2]\n",
+ "\n",
+ "x_hat_xyz"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "define z as x cross y"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " ...,\n",
+ " [ 0.07844496, -0.31995954, -0.9441781 ],\n",
+ " [ 0.23993877, -0.15094018, -0.95898199],\n",
+ " [ 0.15626882, -0.36877272, -0.91628966]])"
+ ]
+ },
+ "execution_count": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "z_hat_xyz = np.cross(x_hat_xyz, z_norm_left_ear_xyz) #create `z_hat` a vector perpendicular to `x_hat` and the vector from the nose to the left ear(i.e. pseudo y_hat)\n",
+ "z_hat_xyz = z_hat_xyz / np.linalg.norm(z_hat_xyz, axis=1)[:,None] #not sure if this needs normalizing, but it couldn't hurt\n",
+ "z_hat_x = z_hat_xyz[:,0]\n",
+ "z_hat_y = z_hat_xyz[:,1]\n",
+ "z_hat_z = z_hat_xyz[:,2]\n",
+ "\n",
+ "z_hat_xyz"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "define y_hat as z_hat cross with x_hat (this ensures that they are all orthogonal, where as the original vector to the left ear might not have been )"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " ...,\n",
+ " [ 0.98243912, -0.13602032, 0.12771785],\n",
+ " [ 0.94439894, -0.19246755, 0.26658374],\n",
+ " [ 0.9205695 , -0.28182237, 0.27042181]])"
+ ]
+ },
+ "execution_count": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "y_hat_xyz = np.cross(z_hat_xyz, z_norm_nose_xyz)\n",
+ "y_hat_xyz = y_hat_xyz / np.linalg.norm(y_hat_xyz, axis=1)[:,None] #not sure if this needs normalizing, but it couldn't hurt\n",
+ "y_hat_x = y_hat_xyz[:,0]\n",
+ "y_hat_y = y_hat_xyz[:,1]\n",
+ "y_hat_z = y_hat_xyz[:,2]\n",
+ "\n",
+ "y_hat_xyz"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "create head rotation matricies by stacking x_hat_xyz, y_hat_xyz, and z_hat_xyz"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "4916\n",
+ "[[[nan nan nan]\n",
+ " [nan nan nan]\n",
+ " [nan nan nan]]]\n"
+ ]
+ }
+ ],
+ "source": [
+ "head_rotation_matricies = [np.dstack((this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz))\n",
+ " for this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz in zip(x_hat_xyz, y_hat_xyz, z_hat_xyz)]\n",
+ "\n",
+ "print(len(head_rotation_matricies))\n",
+ "print(head_rotation_matricies[0])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "make a point in head space and rotate it with the head rotation matricies, as a test of these strange mathemagics"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " [ nan, nan, nan],\n",
+ " ...,\n",
+ " [ 1.23017602, 0.48163646, -1.12013092],\n",
+ " [ 1.40914882, 0.62621761, -0.78876556],\n",
+ " [ 1.43479342, 0.23517171, -0.94130872]])"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "test_point_xyz_og = np.array((1,1,1))\n",
+ "test_point_xyz_rot = np.empty(z_hat_xyz.shape)\n",
+ "\n",
+ "\n",
+ "for frame_number in range(len(head_rotation_matricies)):\n",
+ " test_point_xyz_rot[frame_number,:] = head_rotation_matricies[frame_number] @ test_point_xyz_og\n",
+ "\n",
+ "\n",
+ "test_point_xyz_rot"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ " if debug:\n",
+ " plt.close('all')\n",
+ " fig = plt.figure(num=124)\n",
+ " ax = fig.add_subplot(111, projection='3d')\n",
+ "\n",
+ " ax_range = 2\n",
+ "\n",
+ "\n",
+ " for frame_number in range(1200, z_nose_y.shape[0]):\n",
+ " ax.clear()\n",
+ "\n",
+ "\n",
+ " #plot head orthonormal basis vectors\n",
+ " ax.plot(x_hat_x[frame_number-30:frame_number],\n",
+ " x_hat_y[frame_number-30:frame_number],\n",
+ " x_hat_z[frame_number-30:frame_number], 'r-')\n",
+ "\n",
+ " ax.plot(y_hat_x[frame_number-30:frame_number],\n",
+ " y_hat_y[frame_number-30:frame_number],\n",
+ " y_hat_z[frame_number-30:frame_number], 'g-')\n",
+ "\n",
+ " ax.plot(z_hat_x[frame_number-30:frame_number],\n",
+ " z_hat_y[frame_number-30:frame_number],\n",
+ " z_hat_z[frame_number-30:frame_number], 'b-')\n",
+ " \n",
+ " ax.plot([0, x_hat_x[frame_number]],\n",
+ " [0, x_hat_y[frame_number]], \n",
+ " [0, x_hat_z[frame_number]], 'r-o',label='x_hat')\n",
+ "\n",
+ " ax.plot([0, y_hat_x[frame_number]],\n",
+ " [0, y_hat_y[frame_number]],\n",
+ " [0, y_hat_z[frame_number]], 'g-o',label='y_hat')\n",
+ " \n",
+ " ax.plot([0, z_hat_x[frame_number]],\n",
+ " [0, z_hat_y[frame_number]],\n",
+ " [0, z_hat_z[frame_number]], 'b-o',label='z_hat')\n",
+ " # original nose and ear directions (ear should be slightly diff from y_hat)]\\\n",
+ " ax.plot([0, z_nose_x[frame_number]],\n",
+ " [0, z_nose_y[frame_number]], \n",
+ " [0, z_nose_z[frame_number]], 'r:o',label='z_nose')\n",
+ "\n",
+ " ax.plot([0, z_left_ear_x[frame_number]],\n",
+ " [0, z_left_ear_y[frame_number]],\n",
+ " [0, z_left_ear_z[frame_number]], 'g:o',label='z_left_ear')\n",
+ "\n",
+ " # plot test point rotated with head rotation matricies\n",
+ "\n",
+ " ax.plot(test_point_xyz_rot[frame_number-30:frame_number, 0],\n",
+ " test_point_xyz_rot[frame_number-30:frame_number, 1],\n",
+ " test_point_xyz_rot[frame_number-30:frame_number, 2], 'k-')\n",
+ " \n",
+ " ax.plot([0, test_point_xyz_rot[frame_number,0]],\n",
+ " [0, test_point_xyz_rot[frame_number,1]],\n",
+ " [0, test_point_xyz_rot[frame_number,2]], 'k-o',label='test point')\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ " ax.set_xlim([-ax_range, ax_range])\n",
+ " ax.set_ylim([-ax_range, ax_range])\n",
+ " ax.set_zlim([-ax_range, ax_range])\n",
+ " ax.legend()\n",
+ " plt.pause(.01)\n",
+ " if keyboard.is_pressed('esc'): \n",
+ " print('You Pressed Escape!')\n",
+ " break # finishing the loop\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "interpreter": {
+ "hash": "4ef2e72e5b895021b427c707c9f05b4880b5a5a00e886b84de197a42e54c7f32"
+ },
+ "kernelspec": {
+ "display_name": "Python 3.9.12 ('humon-env')",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.9.12"
+ },
+ "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}