{"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 Signs 2D/3D 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-04-19T03:42:33.863418Z","iopub.execute_input":"2023-04-19T03:42:33.864586Z","iopub.status.idle":"2023-04-19T03:42:33.872953Z","shell.execute_reply.started":"2023-04-19T03:42:33.864533Z","shell.execute_reply":"2023-04-19T03:42:33.871370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/asl-signs/train.csv')\ndisplay(train)\ndisplay(train.info())\ndisplay(train['sign'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:33.875954Z","iopub.execute_input":"2023-04-19T03:42:33.876828Z","iopub.status.idle":"2023-04-19T03:42:34.042952Z","shell.execute_reply.started":"2023-04-19T03:42:33.876768Z","shell.execute_reply":"2023-04-19T03:42:34.041527Z"},"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-04-19T03:42:34.044514Z","iopub.execute_input":"2023-04-19T03:42:34.044888Z","iopub.status.idle":"2023-04-19T03:42:34.071283Z","shell.execute_reply.started":"2023-04-19T03:42:34.044852Z","shell.execute_reply":"2023-04-19T03:42:34.069881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N=list(range(150))\nsigns=sorted(train['sign'].unique().tolist())\nprint(signs)\nprint(len(signs))","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:34.075260Z","iopub.execute_input":"2023-04-19T03:42:34.076154Z","iopub.status.idle":"2023-04-19T03:42:34.091010Z","shell.execute_reply.started":"2023-04-19T03:42:34.076067Z","shell.execute_reply":"2023-04-19T03:42:34.089915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_file_path = \"/kaggle/input/asl-signs/sign_to_prediction_index_map.json\"\nwith open(json_file_path) as f:\n    normal_mapping = json.load(f)\n#print(normal_mapping)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:34.092635Z","iopub.execute_input":"2023-04-19T03:42:34.092999Z","iopub.status.idle":"2023-04-19T03:42:34.101300Z","shell.execute_reply.started":"2023-04-19T03:42:34.092961Z","shell.execute_reply":"2023-04-19T03:42:34.100342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reverse_mapping = {v: k for k, v in normal_mapping.items()}\n#print(reverse_mapping)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:34.102888Z","iopub.execute_input":"2023-04-19T03:42:34.103268Z","iopub.status.idle":"2023-04-19T03:42:34.113838Z","shell.execute_reply.started":"2023-04-19T03:42:34.103232Z","shell.execute_reply":"2023-04-19T03:42:34.112556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize paraquet image","metadata":{}},{"cell_type":"code","source":"!rm -rf 2D\n!rm -rf 3D\n!mkdir 2D\n!mkdir 3D","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:34.115702Z","iopub.execute_input":"2023-04-19T03:42:34.116078Z","iopub.status.idle":"2023-04-19T03:42:38.258038Z","shell.execute_reply.started":"2023-04-19T03:42:34.116043Z","shell.execute_reply":"2023-04-19T03:42:38.256072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### One Sign composed of multiple frames","metadata":{}},{"cell_type":"code","source":"i=2\nsign=train.iloc[i,3]\nprint(sign)\ntable = pq.read_table('/kaggle/input/asl-signs/'+train.iloc[i,0])\n#print(table.to_pydict().keys())\ndf = pd.DataFrame.from_dict(table.to_pydict())\ndf['y']=-df['y']\ndf=df.dropna()\nxmin,xmax=df['x'].min(),df['x'].max()\nymin,ymax=df['y'].min(),df['y'].max()\nzmin,zmax=df['z'].min(),df['z'].max()\nframes=df['frame'].unique().tolist()\n#print(frames)\nprint(len(frames))\ndisplay(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:48:30.505783Z","iopub.execute_input":"2023-04-19T03:48:30.506329Z","iopub.status.idle":"2023-04-19T03:48:30.868661Z","shell.execute_reply.started":"2023-04-19T03:48:30.506279Z","shell.execute_reply":"2023-04-19T03:48:30.867262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This Parquet file contains 3D location information. Specifically, each record contains the coordinates of a point in three-dimensional space along three axes: x, y, and z.","metadata":{}},{"cell_type":"code","source":"#types=df['type'].unique().tolist()\ntypes=['face','right_hand','left_hand','pose']\nprint(types)\ncolors = ['#FFB7D5', '#AEC6CF', '#FDDC99', '#B39EB5', '#FFDAB9', '#D4B996']","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:38.865860Z","iopub.execute_input":"2023-04-19T03:42:38.866241Z","iopub.status.idle":"2023-04-19T03:42:38.874362Z","shell.execute_reply.started":"2023-04-19T03:42:38.866201Z","shell.execute_reply":"2023-04-19T03:42:38.872920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize_2D","metadata":{}},{"cell_type":"code","source":"def Visualize_2D(frame):\n    dfi=df[df['frame']==frame].dropna()\n    dfj=[]\n    for i,typei in enumerate(types):\n        dfj+=[dfi[dfi['type']==typei]]\n    fig,ax = plt.subplots(figsize=(4,4))\n    ax.set_title('frame '+str(frame),fontsize=14)\n    ax.set_xlim([xmin, xmax])\n    ax.set_ylim([ymin, ymax]) \n    ax.set_xlabel('x',fontsize=12)\n    ax.set_ylabel('y',fontsize=12)\n    #ax.axis('off')\n    for i,typei in enumerate(types):\n        dfji=dfj[i]\n        ax.scatter(dfji['x'],dfji['y'],color=colors[i])\n    plt.savefig('2D/frame'+str(frame)+'.png') \n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:38.878664Z","iopub.execute_input":"2023-04-19T03:42:38.879037Z","iopub.status.idle":"2023-04-19T03:42:38.888852Z","shell.execute_reply.started":"2023-04-19T03:42:38.879003Z","shell.execute_reply":"2023-04-19T03:42:38.887509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for frame in range(frames[0],frames[-1]+1):\n    Visualize_2D(frame)    ","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:38.890332Z","iopub.execute_input":"2023-04-19T03:42:38.891283Z","iopub.status.idle":"2023-04-19T03:42:57.451819Z","shell.execute_reply.started":"2023-04-19T03:42:38.891244Z","shell.execute_reply":"2023-04-19T03:42:57.450568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize_3D","metadata":{}},{"cell_type":"code","source":"def Visualize_3D(frame):\n    dfi=df[df['frame']==frame].dropna()\n    dfj=[]\n    for i,typei in enumerate(types):\n        dfj+=[dfi[dfi['type']==typei]]\n    fig,ax = plt.subplots(figsize=(4,4))\n    ax.axis('off')\n    ax = fig.add_subplot(111, projection='3d')\n    ax.set_title('frame '+str(frame),fontsize=14)\n    ax.set_xlim([xmin, xmax])\n    ax.set_ylim([ymin, ymax]) \n    ax.set_zlim([zmin, zmax])    \n    ax.set_xlabel('x',fontsize=12)\n    ax.set_ylabel('y',fontsize=12)\n    ax.set_zlabel('z',fontsize=12)\n    for i,typei in enumerate(types):\n        dfji=dfj[i]\n        ax.scatter(dfji['x'],dfji['y'],dfji['z'],color=colors[i])\n    plt.savefig('3D/frame'+str(frame)+'.png') \n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:57.453180Z","iopub.execute_input":"2023-04-19T03:42:57.453525Z","iopub.status.idle":"2023-04-19T03:42:57.463214Z","shell.execute_reply.started":"2023-04-19T03:42:57.453491Z","shell.execute_reply":"2023-04-19T03:42:57.462205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for frame in range(frames[0],frames[-1]+1):\n    Visualize_3D(frame)   ","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:42:57.464471Z","iopub.execute_input":"2023-04-19T03:42:57.465256Z","iopub.status.idle":"2023-04-19T03:43:23.713045Z","shell.execute_reply.started":"2023-04-19T03:42:57.465210Z","shell.execute_reply":"2023-04-19T03:43:23.711827Z"},"trusted":true},"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-04-19T03:43:23.714531Z","iopub.execute_input":"2023-04-19T03:43:23.714877Z","iopub.status.idle":"2023-04-19T03:43:24.031719Z","shell.execute_reply.started":"2023-04-19T03:43:23.714845Z","shell.execute_reply":"2023-04-19T03:43:24.030001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths3=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/3D'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths3+=[os.path.join(dirname, filename)]\npaths3=sorted(paths3)\nprint(paths3[0:3])\n\nimages3=[]\nfor path in paths3:\n    images3+=[cv2.imread(path)]","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:43:24.033768Z","iopub.execute_input":"2023-04-19T03:43:24.034504Z","iopub.status.idle":"2023-04-19T03:43:24.439251Z","shell.execute_reply.started":"2023-04-19T03:43:24.034464Z","shell.execute_reply":"2023-04-19T03:43:24.437908Z"},"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-04-19T03:43:24.441283Z","iopub.execute_input":"2023-04-19T03:43:24.441650Z","iopub.status.idle":"2023-04-19T03:43:24.449388Z","shell.execute_reply.started":"2023-04-19T03:43:24.441615Z","shell.execute_reply":"2023-04-19T03:43:24.448013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2D Slide Show","metadata":{}},{"cell_type":"code","source":"create_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:43:24.451050Z","iopub.execute_input":"2023-04-19T03:43:24.452326Z","iopub.status.idle":"2023-04-19T03:43:33.381632Z","shell.execute_reply.started":"2023-04-19T03:43:24.452277Z","shell.execute_reply":"2023-04-19T03:43:33.379049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3D Slide Show","metadata":{}},{"cell_type":"code","source":"create_animation(images3)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:43:33.383207Z","iopub.execute_input":"2023-04-19T03:43:33.383579Z","iopub.status.idle":"2023-04-19T03:43:43.376376Z","shell.execute_reply.started":"2023-04-19T03:43:33.383541Z","shell.execute_reply":"2023-04-19T03:43:43.375271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\nx = [1, 2, 3, 4, 5]\ny = [5, 4, 3, 2, 1]\nz = [0, 0, 0, 0, 0]\n\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\nax.scatter(x, y, z)\n\nax.set_xlabel('X Label')\nax.set_ylabel('Y Label')\nax.set_zlabel('Z Label')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:43:43.377562Z","iopub.execute_input":"2023-04-19T03:43:43.378566Z","iopub.status.idle":"2023-04-19T03:43:43.579155Z","shell.execute_reply.started":"2023-04-19T03:43:43.378519Z","shell.execute_reply":"2023-04-19T03:43:43.577954Z"},"trusted":true},"execution_count":null,"outputs":[]}]}