{"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":"# ASL Finger Signs 2D Slide Show","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport json\nimport pyarrow.parquet as pq\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib import animation, rc","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:51.147529Z","iopub.execute_input":"2023-05-13T08:41:51.148053Z","iopub.status.idle":"2023-05-13T08:41:51.155722Z","shell.execute_reply.started":"2023-05-13T08:41:51.14801Z","shell.execute_reply":"2023-05-13T08:41:51.15417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')\ndisplay(train)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:51.158403Z","iopub.execute_input":"2023-05-13T08:41:51.159344Z","iopub.status.idle":"2023-05-13T08:41:51.295865Z","shell.execute_reply.started":"2023-05-13T08:41:51.159286Z","shell.execute_reply":"2023-05-13T08:41:51.29487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"participant_id=sorted(train['participant_id'].unique().tolist())\nprint(participant_id)\nprint(len(participant_id))\nsequence_id=sorted(train['sequence_id'].unique().tolist())\nprint(len(sequence_id))","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:51.297575Z","iopub.execute_input":"2023-05-13T08:41:51.298432Z","iopub.status.idle":"2023-05-13T08:41:51.320006Z","shell.execute_reply.started":"2023-05-13T08:41:51.298392Z","shell.execute_reply":"2023-05-13T08:41:51.318514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_file_path = \"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\"\nwith open(json_file_path) as f:\n    normal_mapping = json.load(f)\n#print(normal_mapping)\nreverse_mapping = {v: k for k, v in normal_mapping.items()}\n#print(reverse_mapping)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:51.32162Z","iopub.execute_input":"2023-05-13T08:41:51.322131Z","iopub.status.idle":"2023-05-13T08:41:51.329705Z","shell.execute_reply.started":"2023-05-13T08:41:51.322091Z","shell.execute_reply":"2023-05-13T08:41:51.328649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize paraquet image","metadata":{}},{"cell_type":"code","source":"!rm -rf 2D\n!mkdir 2D","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:51.332756Z","iopub.execute_input":"2023-05-13T08:41:51.333956Z","iopub.status.idle":"2023-05-13T08:41:53.73058Z","shell.execute_reply.started":"2023-05-13T08:41:51.333911Z","shell.execute_reply":"2023-05-13T08:41:53.729047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 'file_id'==5414471\n* 'sequence_id'==1816796431","metadata":{}},{"cell_type":"code","source":"train0=train[train['file_id']==5414471][train['sequence_id']==1816796431]\ndisplay(train0)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:53.732618Z","iopub.execute_input":"2023-05-13T08:41:53.733739Z","iopub.status.idle":"2023-05-13T08:41:53.755387Z","shell.execute_reply.started":"2023-05-13T08:41:53.733682Z","shell.execute_reply":"2023-05-13T08:41:53.754046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Many sequences are in one paraquet file.","metadata":{}},{"cell_type":"code","source":"table = pq.read_table('/kaggle/input/asl-fingerspelling/train_landmarks/5414471.parquet')\ndf = pd.DataFrame.from_dict(table.to_pydict())\ndisplay(df)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:41:53.757216Z","iopub.execute_input":"2023-05-13T08:41:53.757573Z","iopub.status.idle":"2023-05-13T08:44:57.857724Z","shell.execute_reply.started":"2023-05-13T08:41:53.757539Z","shell.execute_reply":"2023-05-13T08:44:57.856112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pick up one sequence. This sequence is composed of 123 frames.","metadata":{}},{"cell_type":"code","source":"df2=df[df['sequence_id']==1816796431]\ndisplay(df2)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:57.860298Z","iopub.execute_input":"2023-05-13T08:44:57.861374Z","iopub.status.idle":"2023-05-13T08:44:57.899305Z","shell.execute_reply.started":"2023-05-13T08:44:57.861301Z","shell.execute_reply":"2023-05-13T08:44:57.898231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ony use x and y axis.","metadata":{}},{"cell_type":"code","source":"df3=df2.iloc[:,1:1087]\ndisplay(df3)\n\ncols=df3.columns.tolist()\ncols2=set()\nfor item in cols:\n    cols2.add(item.replace('x_','').replace('y_',''))\ncols2=sorted(list(cols2))\n#print(cols2)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:57.900784Z","iopub.execute_input":"2023-05-13T08:44:57.901799Z","iopub.status.idle":"2023-05-13T08:44:57.936692Z","shell.execute_reply.started":"2023-05-13T08:44:57.901752Z","shell.execute_reply":"2023-05-13T08:44:57.935042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfjs=list('a'*len(df3))\nfor j in range(len(df3)):\n    dfj=pd.DataFrame(columns=['col','type','x','y'],index=range(len(cols2)))\n    dfj['col']=cols2\n    for i in range(len(cols2)):\n        col=dfj.loc[i,'col']\n        dfj.loc[i,'type']=col.split('_')[-2]\n        dfj.loc[i,'x']=df3.loc[j,'x_'+col]\n        dfj.loc[i,'y']=-df3.loc[j,'y_'+col]\n    dfjs[j]=dfj\n    \ndisplay(dfjs[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:45:38.762491Z","iopub.execute_input":"2023-05-13T08:45:38.76318Z","iopub.status.idle":"2023-05-13T08:45:39.011277Z","shell.execute_reply.started":"2023-05-13T08:45:38.763117Z","shell.execute_reply":"2023-05-13T08:45:39.009648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xmin,xmax=-0.5,2.0\nymin,ymax=-2.5,0\n\ntypes = ['face','hand','pose']\ncolors = ['#FFB7D5', '#AEC6CF', '#FDDC99', '#B39EB5', '#FFDAB9', '#D4B996']","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:58.599688Z","iopub.status.idle":"2023-05-13T08:44:58.600519Z","shell.execute_reply.started":"2023-05-13T08:44:58.600273Z","shell.execute_reply":"2023-05-13T08:44:58.600301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize_2D","metadata":{}},{"cell_type":"code","source":"def Visualize_2D(j):\n    df=dfjs[j]\n    fig,ax = plt.subplots(figsize=(4,4))\n    ax.set_xlabel('x',fontsize=8)\n    ax.set_ylabel('y',fontsize=8)\n    ax.set_xlim([xmin, xmax])\n    ax.set_ylim([ymin, ymax]) \n    for i in range(len(df)):\n        c=types.index(df.loc[i,'type'])\n        ax.scatter(df.loc[i,'x'],df.loc[i,'y'],color=colors[c])\n    plt.savefig('2D/'+str(j).zfill(3)+'.png') \n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:58.603624Z","iopub.status.idle":"2023-05-13T08:44:58.604096Z","shell.execute_reply.started":"2023-05-13T08:44:58.603878Z","shell.execute_reply":"2023-05-13T08:44:58.603902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j in range(len(df3)):\n    Visualize_2D(j) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Slide Show","metadata":{}},{"cell_type":"code","source":"paths=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/2D'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths+=[os.path.join(dirname, filename)]\npaths=sorted(paths)\nprint(paths[0:3])\n\nimages=[]\nfor path in paths:\n    images+=[cv2.imread(path)]","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:58.607455Z","iopub.status.idle":"2023-05-13T08:44:58.608144Z","shell.execute_reply.started":"2023-05-13T08:44:58.607784Z","shell.execute_reply":"2023-05-13T08:44:58.607819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animation(ims):\n    fig=plt.figure(figsize=(6,6))\n    plt.axis('off')\n    im=plt.imshow(cv2.cvtColor(ims[0],cv2.COLOR_BGR2RGB))\n    plt.close()\n    \n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000/5)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:58.612045Z","iopub.status.idle":"2023-05-13T08:44:58.61291Z","shell.execute_reply.started":"2023-05-13T08:44:58.61251Z","shell.execute_reply":"2023-05-13T08:44:58.612551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-05-13T08:44:58.614769Z","iopub.status.idle":"2023-05-13T08:44:58.615278Z","shell.execute_reply.started":"2023-05-13T08:44:58.615037Z","shell.execute_reply":"2023-05-13T08:44:58.615063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}