{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-13T22:36:22.126049Z","iopub.execute_input":"2023-04-13T22:36:22.127081Z","iopub.status.idle":"2023-04-13T22:36:22.134900Z","shell.execute_reply.started":"2023-04-13T22:36:22.127014Z","shell.execute_reply":"2023-04-13T22:36:22.133179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/asl-signs/train.csv')\ndfs = pd.read_parquet(\"/kaggle/input/asl-with-insights\")","metadata":{"execution":{"iopub.status.busy":"2023-04-13T22:36:22.138430Z","iopub.execute_input":"2023-04-13T22:36:22.139110Z","iopub.status.idle":"2023-04-13T22:36:22.427260Z","shell.execute_reply.started":"2023-04-13T22:36:22.139050Z","shell.execute_reply":"2023-04-13T22:36:22.425610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.animation import FuncAnimation\nfrom IPython.display import HTML\nimport matplotlib.pyplot as plt\n\n\ndir = '/kaggle/input/asl-signs'\nplt.rcParams[\"animation.html\"] = \"jshtml\"\nplt.rcParams['figure.dpi'] = 150\n\ndef 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\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-13T22:36:22.429423Z","iopub.execute_input":"2023-04-13T22:36:22.429862Z","iopub.status.idle":"2023-04-13T22:36:22.966367Z","shell.execute_reply.started":"2023-04-13T22:36:22.429822Z","shell.execute_reply":"2023-04-13T22:36:22.964924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def animate(path_to_sign, file_name):\n    sign = pd.read_parquet(f'{dir}/{path_to_sign}')\n    sign.y = sign.y * -1\n\n    def animation_frame(f):\n        frame = sign[sign.frame==f]  \n        ax.clear()\n\n        pose = frame[frame.type=='pose']\n        px, py = get_pose_points(pose)\n        for i in range(len(px)):\n            ax.plot(px[i], py[i])\n\n        face = frame[frame.type=='face'][['x', 'y']].values\n        ax.plot(face[:,0], face[:,1], '.')\n\n        for hand_type in ['left_hand', 'right_hand']:\n            try:\n                hand = frame[frame.type==f'{hand_type}']\n                x, y = get_hand_points(hand)\n                for i in range(len(x)):\n                    ax.plot(x[i], y[i])\n            except Exception as e:\n                print(e)   \n        plt.xlim(xmin, xmax)\n        plt.ylim(ymin, ymax)\n\n\n    ## These values set the limits on the graph to stabilize the video\n    xmin = sign.x.min() - 0.2\n    xmax = sign.x.max() + 0.2\n    ymin = sign.y.min() - 0.2\n    ymax = sign.y.max() + 0.2\n\n    fig, ax = plt.subplots()\n    l, = ax.plot([], [])\n    animation = FuncAnimation(fig, func=animation_frame, frames=sign.frame.unique())\n    animation.save(file_name, fps=10, extra_args=[\"-vcodec\", \"libx264\"])","metadata":{"execution":{"iopub.status.busy":"2023-04-13T22:36:22.970283Z","iopub.execute_input":"2023-04-13T22:36:22.970874Z","iopub.status.idle":"2023-04-13T22:36:22.986518Z","shell.execute_reply.started":"2023-04-13T22:36:22.970821Z","shell.execute_reply":"2023-04-13T22:36:22.984877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Video\n\ndf_blow = dfs[dfs['sign']=='blow']\ndf_max = df_blow[df_blow['frames']==df_blow['frames'].max()]\n\nanimate(df_max.iloc[0,0], f'animation.mp4')\nanimate(df_max.iloc[0,0], f'animation.mp4')\nanimate(df_max.iloc[0,0], f'animation.mp4')\n\nVideo(\"animation.mp4\", width=400, height=300)","metadata":{"execution":{"iopub.status.busy":"2023-04-13T22:41:33.851033Z","iopub.execute_input":"2023-04-13T22:41:33.851526Z","iopub.status.idle":"2023-04-13T22:42:33.975093Z","shell.execute_reply.started":"2023-04-13T22:41:33.851477Z","shell.execute_reply":"2023-04-13T22:42:33.973328Z"},"trusted":true},"execution_count":null,"outputs":[]}]}