{"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":"import numpy as np\nimport pandas as pd\nimport json\nimport plotly.graph_objects as go\nimport plotly.io as pio\npio.templates.default = \"simple_white\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T16:36:57.612354Z","iopub.execute_input":"2023-05-22T16:36:57.613573Z","iopub.status.idle":"2023-05-22T16:36:59.351476Z","shell.execute_reply.started":"2023-05-22T16:36:57.613528Z","shell.execute_reply":"2023-05-22T16:36:59.350254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/asl-fingerspelling/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:38:30.248391Z","iopub.execute_input":"2023-05-22T16:38:30.249382Z","iopub.status.idle":"2023-05-22T16:38:30.411862Z","shell.execute_reply.started":"2023-05-22T16:38:30.249344Z","shell.execute_reply":"2023-05-22T16:38:30.410689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:38:32.244977Z","iopub.execute_input":"2023-05-22T16:38:32.245636Z","iopub.status.idle":"2023-05-22T16:38:32.275388Z","shell.execute_reply.started":"2023-05-22T16:38:32.245602Z","shell.execute_reply":"2023-05-22T16:38:32.274535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:38:34.476291Z","iopub.execute_input":"2023-05-22T16:38:34.47744Z","iopub.status.idle":"2023-05-22T16:38:34.515757Z","shell.execute_reply.started":"2023-05-22T16:38:34.477382Z","shell.execute_reply":"2023-05-22T16:38:34.51484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence_id = 1816796431\nfile_id = 5414471","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:38:38.708631Z","iopub.execute_input":"2023-05-22T16:38:38.709857Z","iopub.status.idle":"2023-05-22T16:38:38.714688Z","shell.execute_reply.started":"2023-05-22T16:38:38.709814Z","shell.execute_reply":"2023-05-22T16:38:38.713536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_to_sign = f\"/kaggle/input/asl-fingerspelling/train_landmarks/{file_id}.parquet\"\nsign = pd.read_parquet(path_to_sign)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:38:41.451907Z","iopub.execute_input":"2023-05-22T16:38:41.452707Z","iopub.status.idle":"2023-05-22T16:39:00.285472Z","shell.execute_reply.started":"2023-05-22T16:38:41.452665Z","shell.execute_reply":"2023-05-22T16:39:00.284078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sign","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:39:05.974184Z","iopub.execute_input":"2023-05-22T16:39:05.974632Z","iopub.status.idle":"2023-05-22T16:39:06.031749Z","shell.execute_reply.started":"2023-05-22T16:39:05.974595Z","shell.execute_reply":"2023-05-22T16:39:06.030667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(np.unique(sign.index))","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:39:09.611624Z","iopub.execute_input":"2023-05-22T16:39:09.612207Z","iopub.status.idle":"2023-05-22T16:39:09.626254Z","shell.execute_reply.started":"2023-05-22T16:39:09.612163Z","shell.execute_reply":"2023-05-22T16:39:09.624697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence = sign[sign.index == sequence_id]\nsequence","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:39:44.765045Z","iopub.execute_input":"2023-05-22T16:39:44.765469Z","iopub.status.idle":"2023-05-22T16:39:44.799065Z","shell.execute_reply.started":"2023-05-22T16:39:44.76544Z","shell.execute_reply":"2023-05-22T16:39:44.797714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_phrase(df, file_id, sequence_id):\n    return df[\n        np.logical_and(\n            df.file_id == file_id, \n            df.sequence_id == sequence_id\n        )\n    ].phrase.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:41:24.336309Z","iopub.execute_input":"2023-05-22T16:41:24.336813Z","iopub.status.idle":"2023-05-22T16:41:24.343182Z","shell.execute_reply.started":"2023-05-22T16:41:24.336749Z","shell.execute_reply":"2023-05-22T16:41:24.341983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef visualise2d_landmarks(parquet_df, title=\"\"):\n    connections = [  \n        [0, 1, 2, 3, 4,],\n        [0, 5, 6, 7, 8],\n        [0, 9, 10, 11, 12],\n        [0, 13, 14, 15, 16],\n        [0, 17, 18, 19, 20],\n\n        \n        [38, 36, 35, 34, 30, 31, 32, 33, 37],\n        [40, 39],\n        [52, 46, 50, 48, 46, 44, 42, 41, 43, 45, 47, 49, 45, 51],\n        [42, 54, 56, 58, 60, 62, 58],\n        [41, 53, 55, 57, 59, 61, 57],\n        [54, 53],\n\n        \n        [80, 81, 82, 83, 84, ],\n        [80, 85, 86, 87, 88],\n        [80, 89, 90, 91, 92],\n        [80, 93, 94, 95, 96],\n        [80, 97, 98, 99, 100], ]\n\n    parquet_df = map_new_to_old_style(parquet_df)\n    frames = sorted(set(parquet_df.frame))\n    first_frame = min(frames)\n    parquet_df['color'] = parquet_df.type.apply(lambda row: assign_color(row))\n    parquet_df['plot_order'] = parquet_df.apply(lambda row: assign_order(row), axis=1)\n    first_frame_df = parquet_df[parquet_df.frame == first_frame].copy()\n    first_frame_df = first_frame_df.sort_values([\"plot_order\"]).set_index('plot_order')\n\n\n    frames_l = []\n    for frame in frames:\n        filtered_df = parquet_df[parquet_df.frame == frame].copy()\n        filtered_df = filtered_df.sort_values([\"plot_order\"]).set_index(\"plot_order\")\n        traces = [go.Scatter(\n            x=filtered_df['x'],\n            y=filtered_df['y'],\n            mode='markers',\n            marker=dict(\n                color=filtered_df.color,\n                size=9))]\n\n        for i, seg in enumerate(connections):\n            trace = go.Scatter(\n                    x=filtered_df.loc[seg]['x'],\n                    y=filtered_df.loc[seg]['y'],\n                    mode='lines',\n            )\n            traces.append(trace)\n        frame_data = go.Frame(data=traces, traces = [i for i in range(17)])\n        frames_l.append(frame_data)\n\n    traces = [go.Scatter(\n        x=first_frame_df['x'],\n        y=first_frame_df['y'],\n        mode='markers',\n        marker=dict(\n            color=first_frame_df.color,\n            size=9\n        )\n    )]\n    for i, seg in enumerate(connections):\n        trace = go.Scatter(\n            x=first_frame_df.loc[seg]['x'],\n            y=first_frame_df.loc[seg]['y'],\n            mode='lines',\n            line=dict(\n                color='black',\n                width=2\n            )\n        )\n        traces.append(trace)\n    fig = go.Figure(\n        data=traces,\n        frames=frames_l\n    )\n\n\n    fig.update_layout(\n        width=500,\n        height=800,\n        scene={\n            'aspectmode': 'data',\n        },\n        updatemenus=[\n            {\n                \"buttons\": [\n                    {\n                        \"args\": [None, {\"frame\": {\"duration\": 100,\n                                                  \"redraw\": True},\n                                        \"fromcurrent\": True,\n                                        \"transition\": {\"duration\": 0}}],\n                        \"label\": \"&#9654;\",\n                        \"method\": \"animate\",\n                    },\n\n                ],\n                \"direction\": \"left\",\n                \"pad\": {\"r\": 100, \"t\": 100},\n                \"font\": {\"size\":30},\n                \"type\": \"buttons\",\n                \"x\": 0.1,\n                \"y\": 0,\n            }\n        ],\n    )\n    camera = dict(\n        up=dict(x=0, y=-1, z=0),\n        eye=dict(x=0, y=0, z=2.5)\n    )\n    fig.update_layout(title_text=title, title_x=0.5)\n    fig.update_layout(scene_camera=camera, showlegend=False)\n    fig.update_layout(xaxis = dict(visible=False),\n            yaxis = dict(visible=False),\n    )\n    fig.update_yaxes(autorange=\"reversed\")\n\n    fig.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:42:23.487055Z","iopub.execute_input":"2023-05-22T16:42:23.487514Z","iopub.status.idle":"2023-05-22T16:42:23.5073Z","shell.execute_reply.started":"2023-05-22T16:42:23.487473Z","shell.execute_reply":"2023-05-22T16:42:23.505825Z"},"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-05-22T16:42:53.382392Z","iopub.execute_input":"2023-05-22T16:42:53.382923Z","iopub.status.idle":"2023-05-22T16:42:53.395716Z","shell.execute_reply.started":"2023-05-22T16:42:53.382884Z","shell.execute_reply":"2023-05-22T16:42:53.393465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def assign_color(row):\n    if row == 'face':\n        return 'red'\n    elif 'hand' in row:\n        return 'dodgerblue'\n    else:\n        return 'green'\n\n# specifies the plotting order\ndef assign_order(row):\n    if row.type == 'face':\n        return row.landmark_index + 101\n    elif row.type == 'pose':\n        return row.landmark_index + 30\n    elif row.type == 'left_hand':\n        return row.landmark_index + 80\n    else:\n        return row.landmark_index","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:43:44.070701Z","iopub.execute_input":"2023-05-22T16:43:44.071125Z","iopub.status.idle":"2023-05-22T16:43:44.078525Z","shell.execute_reply.started":"2023-05-22T16:43:44.071094Z","shell.execute_reply":"2023-05-22T16:43:44.077485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence_phrase = get_phrase(train_df, file_id, sequence_id)\nvisualise2d_landmarks(sequence, f\"Phrase: {sequence_phrase}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T16:43:50.021518Z","iopub.execute_input":"2023-05-22T16:43:50.021995Z","iopub.status.idle":"2023-05-22T16:44:04.11685Z","shell.execute_reply.started":"2023-05-22T16:43:50.021954Z","shell.execute_reply":"2023-05-22T16:44:04.115801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"supplemental_df = pd.read_csv(\"/kaggle/input/asl-fingerspelling/supplemental_metadata.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:05:20.477073Z","iopub.execute_input":"2023-05-22T17:05:20.478044Z","iopub.status.idle":"2023-05-22T17:05:20.549742Z","shell.execute_reply.started":"2023-05-22T17:05:20.477984Z","shell.execute_reply":"2023-05-22T17:05:20.54858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"supplemental_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:05:33.810945Z","iopub.execute_input":"2023-05-22T17:05:33.812361Z","iopub.status.idle":"2023-05-22T17:05:33.827876Z","shell.execute_reply.started":"2023-05-22T17:05:33.812292Z","shell.execute_reply":"2023-05-22T17:05:33.826403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"supplemental_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:05:45.605044Z","iopub.execute_input":"2023-05-22T17:05:45.605505Z","iopub.status.idle":"2023-05-22T17:05:45.636375Z","shell.execute_reply.started":"2023-05-22T17:05:45.60547Z","shell.execute_reply":"2023-05-22T17:05:45.635418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence_id = 1535467051\nfile_id = 33432165","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:07:30.199667Z","iopub.execute_input":"2023-05-22T17:07:30.200156Z","iopub.status.idle":"2023-05-22T17:07:30.206042Z","shell.execute_reply.started":"2023-05-22T17:07:30.200125Z","shell.execute_reply":"2023-05-22T17:07:30.204999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_to_sign = f\"/kaggle/input/asl-fingerspelling/supplemental_landmarks/{file_id}.parquet\"\nsign = pd.read_parquet(path_to_sign)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:08:06.534808Z","iopub.execute_input":"2023-05-22T17:08:06.535381Z","iopub.status.idle":"2023-05-22T17:08:09.144885Z","shell.execute_reply.started":"2023-05-22T17:08:06.53533Z","shell.execute_reply":"2023-05-22T17:08:09.14382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sign","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:08:30.501013Z","iopub.execute_input":"2023-05-22T17:08:30.50325Z","iopub.status.idle":"2023-05-22T17:08:30.612027Z","shell.execute_reply.started":"2023-05-22T17:08:30.503173Z","shell.execute_reply":"2023-05-22T17:08:30.609914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(np.unique(sign.index))","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:08:42.002754Z","iopub.execute_input":"2023-05-22T17:08:42.004532Z","iopub.status.idle":"2023-05-22T17:08:42.021198Z","shell.execute_reply.started":"2023-05-22T17:08:42.004454Z","shell.execute_reply":"2023-05-22T17:08:42.019396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence = sign[sign.index == sequence_id]\nsequence","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:08:56.844331Z","iopub.execute_input":"2023-05-22T17:08:56.844858Z","iopub.status.idle":"2023-05-22T17:08:56.891502Z","shell.execute_reply.started":"2023-05-22T17:08:56.844818Z","shell.execute_reply":"2023-05-22T17:08:56.889155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence_phrase = get_phrase(supplemental_df, file_id, sequence_id)\nvisualise2d_landmarks(sequence, f\"Phrase: {sequence_phrase}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T17:09:18.31728Z","iopub.execute_input":"2023-05-22T17:09:18.317729Z","iopub.status.idle":"2023-05-22T17:09:34.645141Z","shell.execute_reply.started":"2023-05-22T17:09:18.317695Z","shell.execute_reply":"2023-05-22T17:09:34.643973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}