{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import necessary packages and define the necessary functions","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom IPython.display import HTML\nimport random\n%matplotlib inline\n\n\ndef plot_landmarks(parquet, sign):\n    \n    '''Prepare data for the plots'''\n    #define the frames\n    frames = parquet.frame.drop_duplicates().sort_values().values\n    \n    #min max values\n    xmin = parquet.x.min()\n    xmax = parquet.x.max()\n    ymin = parquet.y.min()\n    ymax = parquet.y.max()\n    zmin = parquet.z.min()\n    zmax = parquet.z.max()\n    \n    #right_hand data\n    parquet_right = parquet.loc[parquet.type=='right_hand']\n    \n    landmark_time_series_right = []\n    for landmark in range(21):\n        landmark_i_ts = []\n        for frame in frames:\n            x, y,z = tuple(parquet_right.loc[(parquet_right.frame==frame) &\n                                        (parquet_right.landmark_index==landmark),\n                                           ['x', 'y','z']].values[0])\n            landmark_i_ts.append((x,y,z))\n        landmark_time_series_right.append(landmark_i_ts)\n        \n    #left_hand data\n    parquet_left = parquet.loc[parquet.type=='left_hand']\n    \n    landmark_time_series_left = []\n    for landmark in range(21):\n        landmark_i_ts = []\n        for frame in frames:\n            x, y,z = tuple(parquet_left.loc[(parquet_left.frame==frame) &\n                                        (parquet_left.landmark_index==landmark),\n                                           ['x', 'y','z']].values[0])\n            landmark_i_ts.append((x,y,z))\n        landmark_time_series_left.append(landmark_i_ts)\n        \n    #pose data\n    parquet_pose = parquet.loc[parquet.type=='pose']\n    \n    landmark_time_series_pose = []\n    for landmark in range(33):\n        landmark_i_ts = []\n        for frame in frames:\n            x, y,z = tuple(parquet_pose.loc[(parquet_pose.frame==frame) &\n                                        (parquet_pose.landmark_index==landmark),\n                                           ['x', 'y','z']].values[0])\n            landmark_i_ts.append((x,y,z))\n        landmark_time_series_pose.append(landmark_i_ts)\n        \n    #face data\n    parquet_face = parquet.loc[parquet.type=='face']\n    \n    landmark_time_series_face = []\n    for landmark in range(468):\n        landmark_i_ts = []\n        for frame in frames:\n            x, y,z = tuple(parquet_face.loc[(parquet_face.frame==frame) &\n                                        (parquet_face.landmark_index==landmark),\n                                           ['x', 'y','z']].values[0])\n            landmark_i_ts.append((x,y,z))\n        landmark_time_series_face.append(landmark_i_ts)\n\n    '''3D plot'''\n    # create a figure and axis for the plot\n    fig1 = plt.figure()\n    ax1 = fig1.add_subplot(111, projection='3d')\n\n    # function that updates the scatter plot for each frame\n    def update1(i):\n        ax1.clear()\n        x = [p[i][0] for p in landmark_time_series_face]\n        y = [p[i][1] for p in landmark_time_series_face]\n        z = [p[i][2] for p in landmark_time_series_face]\n        ax1.scatter(x, y, z, label='face')\n\n        x = [p[i][0] for p in landmark_time_series_pose]\n        y = [p[i][1] for p in landmark_time_series_pose]\n        z = [p[i][2] for p in landmark_time_series_pose]\n        ax1.scatter(x, y, z, label='pose')\n\n        x = [p[i][0] for p in landmark_time_series_right]\n        y = [p[i][1] for p in landmark_time_series_right]\n        z = [p[i][2] for p in landmark_time_series_right]\n        ax1.scatter(x, y, z, label='right hand')\n\n        x = [p[i][0] for p in landmark_time_series_left]\n        y = [p[i][1] for p in landmark_time_series_left]\n        z = [p[i][2] for p in landmark_time_series_left]\n        ax1.scatter(x, y, z, label='left hand')\n\n        ax1.set_xlim(xmin, xmax)\n        ax1.set_ylim(ymin, ymax)\n        ax1.set_zlim(zmin, zmax)\n        ax1.legend()\n        ax1.set_title(sign + ' -- Time step {}'.format(i))\n\n    # create an animation with a frame for each time step\n    anim_3D = FuncAnimation(fig1, update1, frames=len(landmark_time_series_left[0]))\n\n\n    '''2D plot'''\n    # create a figure and axis for the plot\n    fig2, ax2 = plt.subplots()\n\n    # function that updates the scatter plot for each frame\n    def update2(i):\n        ax2.clear()\n        x = [p[i][0] for p in landmark_time_series_face]\n        y = [p[i][1] for p in landmark_time_series_face]\n        ax2.scatter(x, y, label='face')\n\n        x = [p[i][0] for p in landmark_time_series_pose]\n        y = [p[i][1] for p in landmark_time_series_pose]\n        ax2.scatter(x, y, label='pose')\n\n        x = [p[i][0] for p in landmark_time_series_left]\n        y = [p[i][1] for p in landmark_time_series_left]\n        ax2.scatter(x, y, label='left hand')\n\n        x = [p[i][0] for p in landmark_time_series_right]\n        y = [p[i][1] for p in landmark_time_series_right]\n        ax2.scatter(x, y, label='right hand')\n        \n        ax2.set_xlim(xmin, xmax)\n        ax2.set_ylim(ymin, ymax)\n        ax2.legend()\n        ax2.set_title(sign + ' -- Time step {}'.format(i))\n\n    # create an animation with a frame for each time step\n    anim_2D = FuncAnimation(fig2, update2, frames=len(landmark_time_series_face[0]))\n    return anim_3D, anim_2D\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-03T14:37:03.247972Z","iopub.execute_input":"2023-03-03T14:37:03.248447Z","iopub.status.idle":"2023-03-03T14:37:03.299531Z","shell.execute_reply.started":"2023-03-03T14:37:03.248392Z","shell.execute_reply":"2023-03-03T14:37:03.298403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/asl-signs/train.csv')\nrandom_int = random.randint(train.index[0], train.index[-1]+1)\nrandom_path = train.loc[random_int, 'path']\nrandom_sign = train.loc[random_int, 'sign']\nrandom_parquet = pd.read_parquet(os.path.join('../input/asl-signs/', random_path))\nrandom_parquet","metadata":{"execution":{"iopub.status.busy":"2023-03-03T14:37:03.373667Z","iopub.execute_input":"2023-03-03T14:37:03.374719Z","iopub.status.idle":"2023-03-03T14:37:03.626551Z","shell.execute_reply.started":"2023-03-03T14:37:03.374673Z","shell.execute_reply":"2023-03-03T14:37:03.625386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Animated 2D plot","metadata":{}},{"cell_type":"code","source":"anim_3D, anim_2D = plot_landmarks(random_parquet,random_sign)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T14:37:03.628699Z","iopub.execute_input":"2023-03-03T14:37:03.629325Z","iopub.status.idle":"2023-03-03T14:39:06.224632Z","shell.execute_reply.started":"2023-03-03T14:37:03.629286Z","shell.execute_reply":"2023-03-03T14:39:06.223426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(anim_2D.to_jshtml())","metadata":{"execution":{"iopub.status.busy":"2023-03-03T14:39:06.226283Z","iopub.execute_input":"2023-03-03T14:39:06.227257Z","iopub.status.idle":"2023-03-03T14:39:46.248343Z","shell.execute_reply.started":"2023-03-03T14:39:06.227217Z","shell.execute_reply":"2023-03-03T14:39:46.245954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Animated 3D plot","metadata":{}},{"cell_type":"code","source":"HTML(anim_3D.to_jshtml())","metadata":{"execution":{"iopub.status.busy":"2023-03-03T14:39:46.250687Z","iopub.execute_input":"2023-03-03T14:39:46.251078Z","iopub.status.idle":"2023-03-03T14:40:26.960609Z","shell.execute_reply.started":"2023-03-03T14:39:46.251040Z","shell.execute_reply":"2023-03-03T14:40:26.959517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}