Free Motion Capture for Everyone πβ¨
TypeScript
create head rotations in this ipynb
Jon913a9968f3b5
diff --git a/freemocap/pupil/__init__.py b/freemocap/pupil/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/jon_scratch/calculate_head_rotation_matrix.ipynb b/jon_scratch/calculate_head_rotation_matrix.ipynbindex 2494d6e0..dd9900b0 100644--- a/jon_scratch/calculate_head_rotation_matrix.ipynb+++ b/jon_scratch/calculate_head_rotation_matrix.ipynb@@ -17,20 +17,20 @@"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": "markdown",+ "metadata": {},+ "source": [+ "install requirements to Python kernel (I think a 'kernel' is roughly equivalent to a conda environment, i.e. telling the system which version of `python.exe` you want to use to evaluate this Python code )"+ ]+ },{"cell_type": "code",- "execution_count": 14,+ "execution_count": null,"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"- ]- }- ],+ "outputs": [],"source": [+ "#I should specify the versions to ensure future stability\n","!pip install numpy\n","!pip install matplotlib\n","!pip install pyqt5\n",@@ -53,30 +53,22 @@},{"cell_type": "code",- "execution_count": 15,+ "execution_count": null,"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"- ]- }- ],+ "outputs": [],"source": ["debug = True\n","# %matplotlib widget\n","import matplotlib.pyplot as plt\n",- "%matplotlib widget\n",- "# %matplotlib qt"+ "# %matplotlib widget\n",+ "\n",+ "%matplotlib qt \n",+ "#pops out a window for matplot lib plots. It'd be cool to figure out how to play animations inline"]},{"cell_type": "code",- "execution_count": 16,+ "execution_count": 25,"metadata": {},"outputs": [],"source": [@@ -88,7 +80,9 @@"\n","session_folder_path = freemocap_data_folder / session_id\n","\n",- "mediapipe_skeleton_data_path = session_folder_path / 'DataArrays'/ 'mediaPipeSkel_3d_smoothed.npy'"+ "data_arrays_path = session_folder_path / 'DataArrays'\n",+ "\n",+ "mediapipe_skeleton_data_path = data_arrays_path / 'mediaPipeSkel_3d_smoothed.npy'"]},{@@ -106,7 +100,7 @@},{"cell_type": "code",- "execution_count": 17,+ "execution_count": null,"metadata": {},"outputs": [],"source": [@@ -115,7 +109,7 @@},{"cell_type": "code",- "execution_count": 18,+ "execution_count": null,"metadata": {},"outputs": [],"source": [@@ -132,7 +126,7 @@},{"cell_type": "code",- "execution_count": 19,+ "execution_count": null,"metadata": {},"outputs": [],"source": [@@ -164,26 +158,9 @@},{"cell_type": "code",- "execution_count": 20,+ "execution_count": null,"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"- }- ],+ "outputs": [],"source": ["z_nose_xyz = nose_xyz - head_center_xyz\n","z_nose_x = z_nose_xyz[:,0]\n",@@ -207,7 +184,7 @@},{"cell_type": "code",- "execution_count": 21,+ "execution_count": null,"metadata": {},"outputs": [],"source": [@@ -231,26 +208,9 @@},{"cell_type": "code",- "execution_count": 22,+ "execution_count": null,"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"- }- ],+ "outputs": [],"source": ["x_hat_xyz = z_norm_nose_xyz\n","x_hat_x = x_hat_xyz[:,0]\n",@@ -269,26 +229,9 @@},{"cell_type": "code",- "execution_count": 23,+ "execution_count": null,"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"- }- ],+ "outputs": [],"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",@@ -308,26 +251,9 @@},{"cell_type": "code",- "execution_count": 24,+ "execution_count": null,"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"- }- ],+ "outputs": [],"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",@@ -347,20 +273,9 @@},{"cell_type": "code",- "execution_count": 25,+ "execution_count": null,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "4916\n",- "[[[nan nan nan]\n",- " [nan nan nan]\n",- " [nan nan nan]]]\n"- ]- }- ],+ "outputs": [],"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",@@ -378,26 +293,9 @@},{"cell_type": "code",- "execution_count": 26,+ "execution_count": null,"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"- }- ],+ "outputs": [],"source": ["test_point_xyz_og = np.array((1,1,1))\n","test_point_xyz_rot = np.empty(z_hat_xyz.shape)\n",@@ -410,87 +308,127 @@"test_point_xyz_rot"]},+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Visualize results! Press ESC to quit"+ ]+ },{"cell_type": "code",- "execution_count": 27,+ "execution_count": 17,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "You Pressed Escape!\n"+ ]+ }+ ],"source": [- " if debug:\n",+ "if debug:\n"," plt.close('all')\n"," fig = plt.figure(num=124)\n",+ " fig.suptitle('Press ESC to Quit')\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",+ " 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",+ " #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",+ " 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",+ " 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, 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",+ " 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,+ "execution_count": 24,+ "metadata": {},+ "outputs": [+ {+ "data": {+ "text/plain": [+ "(4916, 3, 3)"+ ]+ },+ "execution_count": 24,+ "metadata": {},+ "output_type": "execute_result"+ }+ ],+ "source": [+ "head_rotation_matricies_fr_row_col = np.squeeze(np.asarray(head_rotation_matricies)) #convert list to array and give a friendly squeeze to pop the extra 'singleton' dimension\n",+ "head_rotation_matricies_fr_row_col.shape"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 28,"metadata": {},"outputs": [],- "source": []+ "source": [+ "save_head_rot_path = data_arrays_path / 'mediaPipeSkel_3d_head_rotation_matricies_fr_row_col.npy'\n",+ "np.save(save_head_rot_path, head_rotation_matricies_fr_row_col)"+ ]},{"cell_type": "code",