{"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":"Credit to: \n- https://www.kaggle.com/code/danielpeshkov/animated-data-visualization\n- https://www.kaggle.com/code/leonidkulyk/eda-aslfr-animated-visualization","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom IPython.display import HTML\n\ntr = pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-09T06:34:10.416227Z","iopub.execute_input":"2023-07-09T06:34:10.416608Z","iopub.status.idle":"2023-07-09T06:34:10.483611Z","shell.execute_reply.started":"2023-07-09T06:34:10.416581Z","shell.execute_reply":"2023-07-09T06:34:10.482661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def map_new_to_old_style(sequence):\n    types = []\n    landmark_indexes = []\n    for column in list(sequence.columns)[1:544]:\n        parts = column.split(\"_\")\n        if len(parts) == 4:\n            types.append(parts[1] + \"_\" + parts[2])\n        else:\n            types.append(parts[1])\n\n        landmark_indexes.append(int(parts[-1]))\n\n    data = {\n        \"frame\": [],\n        \"type\": [],\n        \"landmark_index\": [],\n        \"x\": [],\n        \"y\": [],\n        \"z\": []\n    }\n\n    for index, row in sequence.iterrows():\n        data[\"frame\"] += [int(row.frame)]*543\n        data[\"type\"] += types\n        data[\"landmark_index\"] += landmark_indexes\n\n        for _type, landmark_index in zip(types, landmark_indexes):\n            data[\"x\"].append(row[f\"x_{_type}_{landmark_index}\"])\n            data[\"y\"].append(row[f\"y_{_type}_{landmark_index}\"])\n            data[\"z\"].append(row[f\"z_{_type}_{landmark_index}\"])\n\n    return pd.DataFrame.from_dict(data)","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:27:51.503295Z","iopub.execute_input":"2023-07-09T06:27:51.503649Z","iopub.status.idle":"2023-07-09T06:27:51.511714Z","shell.execute_reply.started":"2023-07-09T06:27:51.503623Z","shell.execute_reply":"2023-07-09T06:27:51.511029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Choose path\npath = '/kaggle/input/asl-fingerspelling/train_landmarks/1019715464.parquet'\npq = pd.read_parquet(path)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequences = list(pq.index.unique())","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:44:42.503767Z","iopub.execute_input":"2023-07-09T06:44:42.504119Z","iopub.status.idle":"2023-07-09T06:44:42.510552Z","shell.execute_reply.started":"2023-07-09T06:44:42.504094Z","shell.execute_reply":"2023-07-09T06:44:42.509278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SELECT = 4 # select sequence from parquet file\nselect_seq = sequences[SELECT]\nseq = pq.loc[select_seq] # First seq\nseq = map_new_to_old_style(seq)\nseq.y = seq.y * -1\nseq","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:52:27.913887Z","iopub.execute_input":"2023-07-09T06:52:27.91424Z","iopub.status.idle":"2023-07-09T06:52:28.910784Z","shell.execute_reply.started":"2023-07-09T06:52:27.914215Z","shell.execute_reply":"2023-07-09T06:52:28.909567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr[tr['sequence_id']==select_seq]","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:52:31.118341Z","iopub.execute_input":"2023-07-09T06:52:31.118659Z","iopub.status.idle":"2023-07-09T06:52:31.129328Z","shell.execute_reply.started":"2023-07-09T06:52:31.118637Z","shell.execute_reply":"2023-07-09T06:52:31.128263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_hand_points(hand):\n    x = [[hand.iloc[0].x, hand.iloc[1].x, hand.iloc[2].x, hand.iloc[3].x, hand.iloc[4].x], # Thumb\n         [hand.iloc[5].x, hand.iloc[6].x, hand.iloc[7].x, hand.iloc[8].x], # Index\n         [hand.iloc[9].x, hand.iloc[10].x, hand.iloc[11].x, hand.iloc[12].x], \n         [hand.iloc[13].x, hand.iloc[14].x, hand.iloc[15].x, hand.iloc[16].x], \n         [hand.iloc[17].x, hand.iloc[18].x, hand.iloc[19].x, hand.iloc[20].x], \n         [hand.iloc[0].x, hand.iloc[5].x, hand.iloc[9].x, hand.iloc[13].x, hand.iloc[17].x, hand.iloc[0].x]]\n\n    y = [[hand.iloc[0].y, hand.iloc[1].y, hand.iloc[2].y, hand.iloc[3].y, hand.iloc[4].y],  #Thumb\n         [hand.iloc[5].y, hand.iloc[6].y, hand.iloc[7].y, hand.iloc[8].y], # Index\n         [hand.iloc[9].y, hand.iloc[10].y, hand.iloc[11].y, hand.iloc[12].y], \n         [hand.iloc[13].y, hand.iloc[14].y, hand.iloc[15].y, hand.iloc[16].y], \n         [hand.iloc[17].y, hand.iloc[18].y, hand.iloc[19].y, hand.iloc[20].y], \n         [hand.iloc[0].y, hand.iloc[5].y, hand.iloc[9].y, hand.iloc[13].y, hand.iloc[17].y, hand.iloc[0].y]] \n    return x, y\n\ndef get_pose_points(pose):\n    x = [[pose.iloc[8].x, pose.iloc[6].x, pose.iloc[5].x, pose.iloc[4].x, pose.iloc[0].x, pose.iloc[1].x, pose.iloc[2].x, pose.iloc[3].x, pose.iloc[7].x], \n         [pose.iloc[10].x, pose.iloc[9].x], \n         [pose.iloc[22].x, pose.iloc[16].x, pose.iloc[20].x, pose.iloc[18].x, pose.iloc[16].x, pose.iloc[14].x, pose.iloc[12].x, \n          pose.iloc[11].x, pose.iloc[13].x, pose.iloc[15].x, pose.iloc[17].x, pose.iloc[19].x, pose.iloc[15].x, pose.iloc[21].x], \n         [pose.iloc[12].x, pose.iloc[24].x, pose.iloc[26].x, pose.iloc[28].x, pose.iloc[30].x, pose.iloc[32].x, pose.iloc[28].x], \n         [pose.iloc[11].x, pose.iloc[23].x, pose.iloc[25].x, pose.iloc[27].x, pose.iloc[29].x, pose.iloc[31].x, pose.iloc[27].x], \n         [pose.iloc[24].x, pose.iloc[23].x]\n        ]\n\n    y = [[pose.iloc[8].y, pose.iloc[6].y, pose.iloc[5].y, pose.iloc[4].y, pose.iloc[0].y, pose.iloc[1].y, pose.iloc[2].y, pose.iloc[3].y, pose.iloc[7].y], \n         [pose.iloc[10].y, pose.iloc[9].y], \n         [pose.iloc[22].y, pose.iloc[16].y, pose.iloc[20].y, pose.iloc[18].y, pose.iloc[16].y, pose.iloc[14].y, pose.iloc[12].y, \n          pose.iloc[11].y, pose.iloc[13].y, pose.iloc[15].y, pose.iloc[17].y, pose.iloc[19].y, pose.iloc[15].y, pose.iloc[21].y], \n         [pose.iloc[12].y, pose.iloc[24].y, pose.iloc[26].y, pose.iloc[28].y, pose.iloc[30].y, pose.iloc[32].y, pose.iloc[28].y], \n         [pose.iloc[11].y, pose.iloc[23].y, pose.iloc[25].y, pose.iloc[27].y, pose.iloc[29].y, pose.iloc[31].y, pose.iloc[27].y], \n         [pose.iloc[24].y, pose.iloc[23].y]\n        ]\n    return x, y","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:52:33.569236Z","iopub.execute_input":"2023-07-09T06:52:33.569564Z","iopub.status.idle":"2023-07-09T06:52:33.589745Z","shell.execute_reply.started":"2023-07-09T06:52:33.569541Z","shell.execute_reply":"2023-07-09T06:52:33.58879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def animation_frame(f):\n    frame = seq[seq.frame==f]\n    left = frame[frame.type=='left_hand']\n    right = frame[frame.type=='right_hand']\n    pose = frame[frame.type=='pose']\n    face = frame[frame.type=='face'][['x', 'y']].values\n    lx, ly = get_hand_points(left)\n    rx, ry = get_hand_points(right)\n    px, py = get_pose_points(pose)\n    ax.clear()\n    ax.plot(face[:,0], face[:,1], '.')\n    for i in range(len(lx)):\n        ax.plot(lx[i], ly[i])\n    for i in range(len(rx)):\n        ax.plot(rx[i], ry[i])\n    for i in range(len(px)):\n        ax.plot(px[i], py[i])\n    plt.xlim(xmin, xmax)\n    plt.ylim(ymin, ymax)","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:52:35.847923Z","iopub.execute_input":"2023-07-09T06:52:35.848323Z","iopub.status.idle":"2023-07-09T06:52:35.855608Z","shell.execute_reply.started":"2023-07-09T06:52:35.848295Z","shell.execute_reply":"2023-07-09T06:52:35.854811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The phrase being shown here is: {tr[tr['sequence_id']==select_seq].phrase.iloc[0]}\")\n\n## These values set the limits on the graph to stabilize the video\nxmin = seq.x.min() - 0.2\nxmax = seq.x.max() + 0.2\nymin = seq.y.min() - 0.2\nymax = seq.y.max() + 0.2\n\nfig, ax = plt.subplots()\nl, = ax.plot([], [])\nanimation = FuncAnimation(fig, func=animation_frame, frames=seq.frame.unique())\n\nHTML(animation.to_html5_video())","metadata":{"execution":{"iopub.status.busy":"2023-07-09T06:52:37.781646Z","iopub.execute_input":"2023-07-09T06:52:37.781976Z","iopub.status.idle":"2023-07-09T06:52:57.815881Z","shell.execute_reply.started":"2023-07-09T06:52:37.781951Z","shell.execute_reply":"2023-07-09T06:52:57.814891Z"},"trusted":true},"execution_count":null,"outputs":[]}]}