{"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":"## Summary\n- Maximum label length is 31\n- Most sequence has frame number of about 400\n- It seems that most of the information is coming from right hand\n- Most sequences have about 300 frames with some information on the right hand\n\n","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-05-29T02:25:47.620112Z","iopub.execute_input":"2023-05-29T02:25:47.620502Z","iopub.status.idle":"2023-05-29T02:25:47.650776Z","shell.execute_reply.started":"2023-05-29T02:25:47.620474Z","shell.execute_reply":"2023-05-29T02:25:47.649654Z"}}},{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        continue\n        print(os.path.join(dirname, filename))\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":{"execution":{"iopub.status.busy":"2023-06-03T22:02:53.538821Z","iopub.execute_input":"2023-06-03T22:02:53.539369Z","iopub.status.idle":"2023-06-03T22:02:53.550604Z","shell.execute_reply.started":"2023-06-03T22:02:53.539328Z","shell.execute_reply":"2023-06-03T22:02:53.548981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport time\n%matplotlib inline\nimport pandas as pd\nimport pyarrow.parquet as pq","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:02:56.932712Z","iopub.execute_input":"2023-06-03T22:02:56.933114Z","iopub.status.idle":"2023-06-03T22:02:57.084553Z","shell.execute_reply.started":"2023-06-03T22:02:56.933084Z","shell.execute_reply":"2023-06-03T22:02:57.08279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Label statistics","metadata":{}},{"cell_type":"code","source":"label_path = '/kaggle/input/asl-fingerspelling/train.csv'\ndf_label = pd.read_csv(label_path)\ndf_label.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:01.841319Z","iopub.execute_input":"2023-06-03T22:03:01.84193Z","iopub.status.idle":"2023-06-03T22:03:02.012269Z","shell.execute_reply.started":"2023-06-03T22:03:01.841869Z","shell.execute_reply":"2023-06-03T22:03:02.01082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_label['phrase_length'] = df_label['phrase'].str.len()\ndf_label['phrase_length'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:02.230048Z","iopub.execute_input":"2023-06-03T22:03:02.230433Z","iopub.status.idle":"2023-06-03T22:03:02.273491Z","shell.execute_reply.started":"2023-06-03T22:03:02.230404Z","shell.execute_reply":"2023-06-03T22:03:02.27251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df_label['phrase_length'])\nplt.xlabel('Phrase length')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:02.9113Z","iopub.execute_input":"2023-06-03T22:03:02.91173Z","iopub.status.idle":"2023-06-03T22:03:03.108815Z","shell.execute_reply.started":"2023-06-03T22:03:02.911695Z","shell.execute_reply":"2023-06-03T22:03:03.107746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the landmarks","metadata":{}},{"cell_type":"code","source":"# define a list of pairs of hand and pose landmarks\nhand_connections = [[0,1],[0,5],[0,17],\n                    [1,2],[2,3],[3,4],\n                    [5,6],[5,9],[6,7],[7,8],\n                    [9,10],[9,13],[10,11],[11,12],\n                    [13,14],[13,17],[14,15],[15,16],\n                    [17,18],[18,19],[19,20]\n                    ]\npose_connections = [[0,1],[0,4],\n                    [1,2],[2,3],[3,7],[4,5],[5,6],[6,8],\n                    [9,10],\n                    [11,12],[11,13],[11,23],[13,15],[15,17],[15,19],[15,21],[17,19],\n                    [12,14],[12,24],[14,16],[16,18],[16,20],[16,22],[18,20],\n                    [23,24],[23,25],[25,27],[27,29],[27,31],[29,31],\n                    [24,26],[26,28],[28,30],[28,32],[30,32]\n                    ]\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:05.318709Z","iopub.execute_input":"2023-06-03T22:03:05.319065Z","iopub.status.idle":"2023-06-03T22:03:05.327889Z","shell.execute_reply.started":"2023-06-03T22:03:05.319038Z","shell.execute_reply":"2023-06-03T22:03:05.326805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/asl-fingerspelling/train_landmarks/1358493307.parquet'\ndf = pq.read_table(file_path).to_pandas()\nsequence_ids = df.index.unique()\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:05.674837Z","iopub.execute_input":"2023-06-03T22:03:05.675375Z","iopub.status.idle":"2023-06-03T22:03:09.019927Z","shell.execute_reply.started":"2023-06-03T22:03:05.675344Z","shell.execute_reply":"2023-06-03T22:03:09.01853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.animation\nimport numpy as np\n%matplotlib inline\n\ndef animate_sequence(df, sequence_id, hand_connections, pose_connections):\n    df_seq = df[df.index == sequence_id]\n    right_hand_x = df_seq[df_seq.columns[df_seq.columns.str.contains('x_right_hand')]].to_numpy()\n    right_hand_y = 2-df_seq[df_seq.columns[df_seq.columns.str.contains('y_right_hand')]].to_numpy()\n    left_hand_x = df_seq[df_seq.columns[df_seq.columns.str.contains('x_left_hand')]].to_numpy()\n    left_hand_y = 2-df_seq[df_seq.columns[df_seq.columns.str.contains('y_left_hand')]].to_numpy()\n    pose_x = df_seq[df_seq.columns[df_seq.columns.str.contains('x_pose')]].to_numpy()\n    pose_y = 2-df_seq[df_seq.columns[df_seq.columns.str.contains('y_pose')]].to_numpy()\n\n    all_x = df_seq[df_seq.columns[df_seq.columns.str.contains('x_')]].to_numpy()\n    all_y = 2-df_seq[df_seq.columns[df_seq.columns.str.contains('y_')]].to_numpy()\n   \n    fig, ax = plt.subplots( figsize = (3, 6))\n    # get min and max of non-na values\n    min_x = np.nanmin(all_x)\n    max_x = np.nanmax(all_x)\n    min_y = np.nanmin(all_y)\n    max_y = np.nanmax(all_y)\n    pad = 0.1\n\n    ax.axis([min_x-pad, max_x+pad, min_y-pad, max_y+pad])\n    landmarks = ax.scatter([], [], color=\"green\", s=10)\n    # set line plot for each hand connection\n    right_lines = [ax.plot([], [], color=\"red\")[0] for _ in range(len(hand_connections))]\n    left_lines = [ax.plot([], [], color=\"blue\")[0] for _ in range(len(hand_connections))]\n    pose_lines = [ax.plot([], [], color=\"cyan\")[0] for _ in range(len(pose_connections))]\n    # get title object\n    title = ax.set_title('')\n    ax.set_xticks([])\n    ax.set_yticks([])\n\n    def animate(t):\n        # set new title\n        title.set_text('{}/{}'.format(t, len(df_seq)))\n        landmarks.set_offsets(np.c_[all_x[t], all_y[t]])\n        for c, (i, j) in enumerate(pose_connections):\n            x_points = pose_x[t][[i,j]]\n            y_points = pose_y[t][[i,j]]\n            pose_lines[c].set_data(x_points, y_points)\n        for c, (i, j) in enumerate(hand_connections):\n\n            x_points = right_hand_x[t][[i,j]]\n            y_points = right_hand_y[t][[i,j]]\n            right_lines[c].set_data(x_points, y_points)\n            x_points = left_hand_x[t][[i,j]]\n            y_points = left_hand_y[t][[i,j]]\n            left_lines[c].set_data(x_points, y_points)\n\n\n    ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(df_seq), interval=200)\n    plt.close(fig)\n    from IPython.display import HTML\n    return HTML(ani.to_jshtml())\n    #return ani\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:09.02524Z","iopub.execute_input":"2023-06-03T22:03:09.026402Z","iopub.status.idle":"2023-06-03T22:03:09.05999Z","shell.execute_reply.started":"2023-06-03T22:03:09.026349Z","shell.execute_reply":"2023-06-03T22:03:09.058399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"animate_sequence(df, sequence_ids[1], hand_connections, pose_connections)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:09.061783Z","iopub.execute_input":"2023-06-03T22:03:09.062512Z","iopub.status.idle":"2023-06-03T22:03:16.506679Z","shell.execute_reply.started":"2023-06-03T22:03:09.062469Z","shell.execute_reply":"2023-06-03T22:03:16.505871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"animate_sequence(df, sequence_ids[2], hand_connections, pose_connections)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:16.509014Z","iopub.execute_input":"2023-06-03T22:03:16.509361Z","iopub.status.idle":"2023-06-03T22:03:20.369621Z","shell.execute_reply.started":"2023-06-03T22:03:16.509333Z","shell.execute_reply":"2023-06-03T22:03:20.368799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"animate_sequence(df, sequence_ids[3], hand_connections, pose_connections)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:20.371026Z","iopub.execute_input":"2023-06-03T22:03:20.371293Z","iopub.status.idle":"2023-06-03T22:03:30.446Z","shell.execute_reply.started":"2023-06-03T22:03:20.37127Z","shell.execute_reply":"2023-06-03T22:03:30.445156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Valid landmark coordinates\nSome coordinates are `nan`, so let's see how much is valid ","metadata":{}},{"cell_type":"code","source":"import glob\nfrom os import path\n\ndata_dir = '/kaggle/input/asl-fingerspelling/train_landmarks/'\nparquet_files = glob.glob(path.join(data_dir, '*.parquet'))\nprint(len(parquet_files))","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:30.447062Z","iopub.execute_input":"2023-06-03T22:03:30.447574Z","iopub.status.idle":"2023-06-03T22:03:30.45695Z","shell.execute_reply.started":"2023-06-03T22:03:30.447546Z","shell.execute_reply":"2023-06-03T22:03:30.455599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm\n\ndf_counts = pd.DataFrame()\nfor parquet_file in tqdm.tqdm(parquet_files):\n    df = pq.read_table(parquet_file).to_pandas()\n    valid_counts = df.groupby(df.index).count()\n    df_counts = pd.concat([df_counts, valid_counts], axis=0)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:03:30.459063Z","iopub.execute_input":"2023-06-03T22:03:30.459538Z","iopub.status.idle":"2023-06-03T22:09:41.663094Z","shell.execute_reply.started":"2023-06-03T22:03:30.459507Z","shell.execute_reply":"2023-06-03T22:09:41.661776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames = df_counts['frame']\ndf_x = df_counts[df_counts.columns[df_counts.columns.str.contains('x_')]]\ndf_type = df_x.groupby(lambda x: '_'.join(x.split('_')[1:-1]), axis=1).mean()\ndf_sum = df_type.sum(axis=0)/frames.sum()\nfig, ax = plt.subplots(1, 1, figsize = (8, 4))\nax.bar(df_sum.index, df_sum.values)\n#ax.set_xticklabels(df_sum.index, rotation = 90)\nax.set_ylabel('Average portion of valid values')\nax.set_title('Average portion of the frames with valid values')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:10:09.362013Z","iopub.execute_input":"2023-06-03T22:10:09.362422Z","iopub.status.idle":"2023-06-03T22:10:09.699801Z","shell.execute_reply.started":"2023-06-03T22:10:09.362393Z","shell.execute_reply":"2023-06-03T22:10:09.697546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see, face and pose landmarks are almost always there, while left hand is not often present, \nand right hand present about half the time. As we can see in the visualization, it is likely due to \nthe times between signs.","metadata":{}},{"cell_type":"code","source":"frames.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:10:11.775995Z","iopub.execute_input":"2023-06-03T22:10:11.776392Z","iopub.status.idle":"2023-06-03T22:10:11.794206Z","shell.execute_reply.started":"2023-06-03T22:10:11.776358Z","shell.execute_reply":"2023-06-03T22:10:11.792509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(frames)\nplt.xlabel('Frame lengths')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:10:12.912188Z","iopub.execute_input":"2023-06-03T22:10:12.912648Z","iopub.status.idle":"2023-06-03T22:10:13.094398Z","shell.execute_reply.started":"2023-06-03T22:10:12.912614Z","shell.execute_reply":"2023-06-03T22:10:13.093395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How many frames have no information on right hand?","metadata":{}},{"cell_type":"code","source":"selected_columns = ['x_right_hand_0', 'y_right_hand_0', 'z_right_hand_0',\n                    'x_right_hand_1', 'y_right_hand_1', 'z_right_hand_1',\n                    'x_right_hand_2', 'y_right_hand_2', 'z_right_hand_2',\n                    'x_right_hand_3', 'y_right_hand_3', 'z_right_hand_3',\n                    'x_right_hand_4', 'y_right_hand_4', 'z_right_hand_4',\n                    'x_right_hand_5', 'y_right_hand_5', 'z_right_hand_5',\n                    'x_right_hand_6', 'y_right_hand_6', 'z_right_hand_6',\n                    'x_right_hand_7', 'y_right_hand_7', 'z_right_hand_7',\n                    'x_right_hand_8', 'y_right_hand_8', 'z_right_hand_8',\n                    'x_right_hand_9', 'y_right_hand_9', 'z_right_hand_9',\n                    'x_right_hand_10', 'y_right_hand_10', 'z_right_hand_10',\n                    'x_right_hand_11', 'y_right_hand_11', 'z_right_hand_11',\n                    'x_right_hand_12', 'y_right_hand_12', 'z_right_hand_12',\n                    'x_right_hand_13', 'y_right_hand_13', 'z_right_hand_13',\n                    'x_right_hand_14', 'y_right_hand_14', 'z_right_hand_14',\n                    'x_right_hand_15', 'y_right_hand_15', 'z_right_hand_15',\n                    'x_right_hand_16', 'y_right_hand_16', 'z_right_hand_16',\n                    'x_right_hand_17', 'y_right_hand_17', 'z_right_hand_17',\n                    'x_right_hand_18', 'y_right_hand_18', 'z_right_hand_18',\n                    'x_right_hand_19', 'y_right_hand_19', 'z_right_hand_19',\n                    'x_right_hand_20', 'y_right_hand_20', 'z_right_hand_20',\n                    ]","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:10:16.174808Z","iopub.execute_input":"2023-06-03T22:10:16.175491Z","iopub.status.idle":"2023-06-03T22:10:16.18128Z","shell.execute_reply.started":"2023-06-03T22:10:16.175458Z","shell.execute_reply":"2023-06-03T22:10:16.180597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_parquet(parquet_files[0], columns = selected_columns)\ndf = df.dropna(how='all')\ndf.groupby(df.index).size()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:14:43.009571Z","iopub.execute_input":"2023-06-03T22:14:43.009954Z","iopub.status.idle":"2023-06-03T22:14:43.158236Z","shell.execute_reply.started":"2023-06-03T22:14:43.009926Z","shell.execute_reply":"2023-06-03T22:14:43.156608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_counts_right = []\nfor parquet_file in tqdm.tqdm(parquet_files):\n    df = pd.read_parquet(parquet_files[0], columns = selected_columns)\n    df = df.dropna(how='all')\n    df_counts_right += list(df.groupby(df.index).size())\n","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:17:53.482541Z","iopub.execute_input":"2023-06-03T22:17:53.483788Z","iopub.status.idle":"2023-06-03T22:18:01.208915Z","shell.execute_reply.started":"2023-06-03T22:17:53.483732Z","shell.execute_reply":"2023-06-03T22:18:01.207906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df_counts_right)\nplt.xlabel('# of frames with right hand information')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-03T22:18:01.210784Z","iopub.execute_input":"2023-06-03T22:18:01.211635Z","iopub.status.idle":"2023-06-03T22:18:01.559178Z","shell.execute_reply.started":"2023-06-03T22:18:01.211571Z","shell.execute_reply":"2023-06-03T22:18:01.557977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}