{"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":"## Animation tool","metadata":{}},{"cell_type":"markdown","source":"There is simple animation tool you can use in your work. Enjoy!","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport torch\nimport pandas as pd\nfrom torch import nn, Tensor","metadata":{"execution":{"iopub.status.busy":"2023-03-07T19:46:14.003845Z","iopub.execute_input":"2023-03-07T19:46:14.004378Z","iopub.status.idle":"2023-03-07T19:46:14.011911Z","shell.execute_reply.started":"2023-03-07T19:46:14.004326Z","shell.execute_reply":"2023-03-07T19:46:14.010490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation\nfrom IPython.display import Image\n\n\ndef islr_animation(x: Tensor, sign_name: str=None, participant_id:int=None, path:str=None):\n    fig, ax = plt.subplots(1, 1)\n    \n    x_transpose = -x\n    min_x = x_transpose[:, :, 0].nan_to_num(10000).min()\n    max_x = x_transpose[:, :, 0].nan_to_num(-10000).max()\n    min_y = x_transpose[:, :, 1].nan_to_num(10000).min()\n    max_y = x_transpose[:, :, 1].nan_to_num(-10000).max()\n    \n    def animate(i):\n        frame = x_transpose[i] \n        \n        ax.clear()\n        ax.axis(\"off\")\n        ax.set_xlim(min_x, max_x)\n        ax.set_ylim(min_y - 0.4, max_y)\n        \n        if sign_name is not None:\n            ax.text(min_x, min_y, \"Sign: \" + sign_name)\n        if participant_id is not None:\n            ax.text(min_x, min_y - 0.15, \"Participant: \" + str(participant_id))\n        if path is not None:\n            ax.text(min_x, min_y - 0.3, \"Path: \" + path)\n        \n        ax.scatter(frame[:468, 0], frame[:468, 1], color=\"yellow\", s=0.2) # head\n        ax.scatter(frame[468:488, 0], frame[468:488, 1], color=\"red\", s=0.2) # left hand\n        ax.scatter(frame[489:521, 0], frame[489:521, 1], color=\"gray\", s=0.2) # posture\n        ax.scatter(frame[522:, 0], frame[522:, 1], color=\"blue\", s=0.2) # right hand\n        \n        # We plot hands.\n        lines = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [5, 9], [9, 10], [10, 11], [11, 12],\n                [9, 13], [13, 14], [14, 15], [15, 16], [17, 18], [18, 19], [19, 20], [0, 17], [13, 17]]\n        for line_x, line_y in lines:\n            ax.plot([frame[line_x + 468, 0], frame[line_y + 468, 0]],\n                    [frame[line_x + 468, 1], frame[line_y + 468, 1]], color=\"red\", linewidth=0.5)\n            ax.plot([frame[line_x + 522, 0], frame[line_y + 522, 0]],\n                    [frame[line_x + 522, 1], frame[line_y + 522, 1]], color=\"blue\", linewidth=0.5)\n        \n        return ax\n    \n    anim = animation.FuncAnimation(fig, animate, frames=range(x.shape[0]), interval=300)\n    anim.save(\"islr.gif\", writer=\"imagemagick\")\n    plt.close()\n\ndef animate(idx:int):\n    ROWS_PER_FRAME = 543  # number of landmarks per frame\n    PATH = \"/kaggle/input/asl-signs/\"\n\n    train_csv = pd.read_csv(PATH + \"train.csv\")\n\n    path, participant_id, sign = train_csv.iloc[idx][\"path\"], train_csv.iloc[idx][\"participant_id\"], train_csv.iloc[idx][\"sign\"]\n\n    def 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    frames_np = load_relevant_data_subset(PATH + path)\n    islr_animation(Tensor(frames_np), sign_name=sign, participant_id=participant_id, path=path)\n    display(Image(\"./islr.gif\"))","metadata":{"execution":{"iopub.status.busy":"2023-03-07T19:46:14.014970Z","iopub.execute_input":"2023-03-07T19:46:14.015949Z","iopub.status.idle":"2023-03-07T19:46:14.043552Z","shell.execute_reply.started":"2023-03-07T19:46:14.015891Z","shell.execute_reply":"2023-03-07T19:46:14.042204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Examples","metadata":{}},{"cell_type":"code","source":"animate(716)","metadata":{"execution":{"iopub.status.busy":"2023-03-07T19:46:14.045791Z","iopub.execute_input":"2023-03-07T19:46:14.046590Z","iopub.status.idle":"2023-03-07T19:46:23.246886Z","shell.execute_reply.started":"2023-03-07T19:46:14.046545Z","shell.execute_reply":"2023-03-07T19:46:23.245433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](./islr.gif)","metadata":{}},{"cell_type":"markdown","source":"It is possible to use only islr_animation() function -- to print transformed animations.","metadata":{}}]}