{"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":"from pathlib import Path\nINPUT_DIR = Path('/kaggle/input/')\nASL_DIR = INPUT_DIR / 'asl-signs'\nimport json\n\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-30T06:53:55.783644Z","iopub.execute_input":"2023-03-30T06:53:55.784615Z","iopub.status.idle":"2023-03-30T06:53:55.816442Z","shell.execute_reply.started":"2023-03-30T06:53:55.784559Z","shell.execute_reply":"2023-03-30T06:53:55.814989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"face_landmarks = {\n    'outer_lip': np.array([0, 8, 9, 10, 63, 17, 43, 30, 62, 27, 5, 89, 123, 92, 105, 79, 124, 72, 71, 70, 0]),\n    'inner_lip': np.array([3, 26, 25, 24, 64, 23, 32, 29, 61, 28, 4, 90, 122, 91, 94, 85, 125, 86, 87, 88, 3]),\n    'right_eye': np.array([1, 7, 66, 55, 54, 53, 52, 51, 59, 39, 50, 49, 48, 42, 41, 57, 1]),\n    'left_eye': np.array([67, 69, 127, 116, 115, 114, 113, 112, 120, 101, 111, 110, 109, 104, 103, 118, 67]),\n    'lower_right_eyebrow': np.array([11, 13, 12, 19, 15]),\n    'upper_right_eyebrow': np.array([22, 18, 34, 20, 35]),\n    'lower_left_eyebrow': np.array([73, 75, 74, 81, 77]),\n    'upper_left_eyebrow': np.array([84, 80, 96, 82, 97])\n}\nhand_landmarks = {\n    'thumb': np.array([0,1,2,3,4]),\n    'index': np.array([0,5,6,7,8]),\n    'middle': np.array([0,9,10,11,12]),\n    'ring': np.array([0,13,14,15,16]),\n    'pinky': np.array([0,17,18,19,20])\n}\npose_landmarks = {\n    'wrist_to_wrist': np.array([16,14,12,11,13,15,])\n}","metadata":{"execution":{"iopub.status.busy":"2023-03-30T06:53:55.818625Z","iopub.execute_input":"2023-03-30T06:53:55.819007Z","iopub.status.idle":"2023-03-30T06:53:55.834024Z","shell.execute_reply.started":"2023-03-30T06:53:55.818971Z","shell.execute_reply":"2023-03-30T06:53:55.832611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"face_indices = np.unique(np.concatenate(list(face_landmarks.values())))\nhand_indices = np.unique(np.concatenate(list(hand_landmarks.values())))\npose_indices = np.unique(np.concatenate(list(pose_landmarks.values())))","metadata":{"execution":{"iopub.status.busy":"2023-03-30T06:53:55.836154Z","iopub.execute_input":"2023-03-30T06:53:55.837646Z","iopub.status.idle":"2023-03-30T06:53:55.848268Z","shell.execute_reply.started":"2023-03-30T06:53:55.837583Z","shell.execute_reply":"2023-03-30T06:53:55.847166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.concatenate([\n    face_indices,          # face\n    hand_indices+468,      # left hand\n    pose_indices+468+21,   # pose\n    hand_indices+468+21+33 # right hand\n])","metadata":{"execution":{"iopub.status.busy":"2023-03-30T06:53:55.851384Z","iopub.execute_input":"2023-03-30T06:53:55.852308Z","iopub.status.idle":"2023-03-30T06:53:55.858833Z","shell.execute_reply.started":"2023-03-30T06:53:55.852245Z","shell.execute_reply":"2023-03-30T06:53:55.857752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices","metadata":{"execution":{"iopub.status.busy":"2023-03-30T06:53:55.860368Z","iopub.execute_input":"2023-03-30T06:53:55.861022Z","iopub.status.idle":"2023-03-30T06:53:55.874752Z","shell.execute_reply.started":"2023-03-30T06:53:55.860978Z","shell.execute_reply":"2023-03-30T06:53:55.873402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('indices.npy', indices)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T06:54:13.999664Z","iopub.execute_input":"2023-03-30T06:54:14.000085Z","iopub.status.idle":"2023-03-30T06:54:14.006571Z","shell.execute_reply.started":"2023-03-30T06:54:14.000046Z","shell.execute_reply":"2023-03-30T06:54:14.005035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROWS_PER_FRAME = 543  # number of landmarks per frame\n\ndef load_relevant_data_subset(pq_path):\n    data_columns = ['x', 'y', 'z']\n    data = pd.read_parquet(pq_path, columns=data_columns)\n    n_frames = int(len(data) / ROWS_PER_FRAME)\n    data = data.values.reshape(n_frames, ROWS_PER_FRAME, len(data_columns))\n    return data.astype(np.float32)\n\ndef load_impt_data(pq_path):\n    data = load_relevant_data_subset(pq_path)\n    impt = data[:,indices].astype(np.float16)\n    del data\n    return impt","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.163110Z","iopub.execute_input":"2023-03-28T09:48:35.163636Z","iopub.status.idle":"2023-03-28T09:48:35.171483Z","shell.execute_reply.started":"2023-03-28T09:48:35.163590Z","shell.execute_reply":"2023-03-28T09:48:35.170137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signs_df = pd.read_csv(ASL_DIR / 'train.csv')\nsigns_df","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.173920Z","iopub.execute_input":"2023-03-28T09:48:35.174388Z","iopub.status.idle":"2023-03-28T09:48:35.411595Z","shell.execute_reply.started":"2023-03-28T09:48:35.174330Z","shell.execute_reply":"2023-03-28T09:48:35.410265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sign_to_label = pd.read_json(ASL_DIR / 'sign_to_prediction_index_map.json', typ='series', orient='index')\nsign_to_label","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.413375Z","iopub.execute_input":"2023-03-28T09:48:35.414447Z","iopub.status.idle":"2023-03-28T09:48:35.433730Z","shell.execute_reply.started":"2023-03-28T09:48:35.414384Z","shell.execute_reply":"2023-03-28T09:48:35.432565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signs_df['label'] = signs_df.sign.map(sign_to_label)\nsigns_df","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.436589Z","iopub.execute_input":"2023-03-28T09:48:35.437340Z","iopub.status.idle":"2023-03-28T09:48:35.464004Z","shell.execute_reply.started":"2023-03-28T09:48:35.437286Z","shell.execute_reply":"2023-03-28T09:48:35.462964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signs_df.to_csv('train.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.465524Z","iopub.execute_input":"2023-03-28T09:48:35.465897Z","iopub.status.idle":"2023-03-28T09:48:35.734478Z","shell.execute_reply.started":"2023-03-28T09:48:35.465846Z","shell.execute_reply":"2023-03-28T09:48:35.733099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {}\nfor path in signs_df.path:\n    out = Path(path)\n    data[out.stem] = load_impt_data(ASL_DIR / path)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T09:48:35.736091Z","iopub.execute_input":"2023-03-28T09:48:35.736427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savez('data.npz', **data)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmarks = dict()\nfor t in [\n    (face_landmarks, 0, 'face'),\n    (hand_landmarks, 468, 'left_hand'),\n    (pose_landmarks, 468+21, 'pose'),\n    (hand_landmarks, 468+21+33, 'right_hand')]:\n    for k,l in t[0].items():\n        name = t[2]+'_'+k\n        mask = np.expand_dims(indices, 1) == (l+t[1])\n        zeros = np.zeros(mask.shape)\n        zeros[:] = np.expand_dims(np.arange(len(indices)), 1)\n        landmarks[name] = np.max((zeros * mask), axis=0).astype(int).tolist()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('landmarks.json', 'w') as f:\n    json.dump(landmarks, f)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}