{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-15T14:00:30.498550Z","iopub.execute_input":"2021-12-15T14:00:30.499115Z","iopub.status.idle":"2021-12-15T14:00:30.524106Z","shell.execute_reply.started":"2021-12-15T14:00:30.498963Z","shell.execute_reply":"2021-12-15T14:00:30.523401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_raw = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\ndf_train_raw","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:01:14.538412Z","iopub.execute_input":"2021-12-15T14:01:14.538673Z","iopub.status.idle":"2021-12-15T14:01:14.611988Z","shell.execute_reply.started":"2021-12-15T14:01:14.538645Z","shell.execute_reply":"2021-12-15T14:01:14.611069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset with annotations","metadata":{}},{"cell_type":"code","source":"df_train_raw[df_train_raw[\"annotations\"] != '[]']","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:02:21.856718Z","iopub.execute_input":"2021-12-15T14:02:21.857591Z","iopub.status.idle":"2021-12-15T14:02:21.883375Z","shell.execute_reply.started":"2021-12-15T14:02:21.857551Z","shell.execute_reply":"2021-12-15T14:02:21.882672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset without annotations","metadata":{}},{"cell_type":"code","source":"df_train_raw[df_train_raw[\"annotations\"] == '[]']","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:03:37.106505Z","iopub.execute_input":"2021-12-15T14:03:37.106778Z","iopub.status.idle":"2021-12-15T14:03:37.127074Z","shell.execute_reply.started":"2021-12-15T14:03:37.106749Z","shell.execute_reply":"2021-12-15T14:03:37.126427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the images","metadata":{}},{"cell_type":"code","source":"from PIL import Image, ImageDraw\nimport ast\n\ndef fetch_image_list(df_tmp, video_id, num_images, start_frame_idx):\n    def fetch_image(frame_id):\n        path_base = '/kaggle/input/tensorflow-great-barrier-reef/train_images/video_{}/{}.jpg'\n        raw_img = Image.open(path_base.format(video_id, frame_id))\n\n        row_frame = df_tmp[(df_tmp.video_id == video_id) & (df_tmp.video_frame == frame_id)].iloc[0]\n        bounding_boxes = ast.literal_eval(row_frame.annotations)\n\n        for box in bounding_boxes:\n            draw = ImageDraw.Draw(raw_img)\n            x0, y0, x1, y1 = (box['x'], box['y'], box['x']+box['width'], box['y']+box['height'])\n            draw.rectangle( (x0, y0, x1, y1), outline=180, width=3)\n        return raw_img\n\n    return [np.array(fetch_image(start_frame_idx + index)) for index in range(num_images)]\n\nimages = fetch_image_list(df_train_raw, video_id = 0, num_images = 80, start_frame_idx = 25)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:09:25.798177Z","iopub.execute_input":"2021-12-15T14:09:25.798475Z","iopub.status.idle":"2021-12-15T14:09:27.932953Z","shell.execute_reply.started":"2021-12-15T14:09:25.798444Z","shell.execute_reply":"2021-12-15T14:09:27.932210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num images: \", len(images))\nplt.imshow(images[10], interpolation='nearest')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:09:47.439756Z","iopub.execute_input":"2021-12-15T14:09:47.440219Z","iopub.status.idle":"2021-12-15T14:09:47.628891Z","shell.execute_reply.started":"2021-12-15T14:09:47.440188Z","shell.execute_reply":"2021-12-15T14:09:47.628313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(9, 9))\n    plt.axis('off')\n    im = plt.imshow(ims[0])\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//12)\n\ncreate_animation(images)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T14:10:58.285568Z","iopub.execute_input":"2021-12-15T14:10:58.286139Z","iopub.status.idle":"2021-12-15T14:11:08.963251Z","shell.execute_reply.started":"2021-12-15T14:10:58.286091Z","shell.execute_reply":"2021-12-15T14:11:08.962361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}