{"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":"## Exploration notebook\n\nSo far:\n- Visualize dataframe\n- Animate video frames and include bounding boxes of the starfish.\n\nNext steps:\n\n- Start with image segmentation approaches","metadata":{}},{"cell_type":"markdown","source":"#### Imports","metadata":{}},{"cell_type":"markdown","source":"First load the main imports","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:46.953628Z","iopub.execute_input":"2021-11-23T20:13:46.953970Z","iopub.status.idle":"2021-11-23T20:13:46.960568Z","shell.execute_reply.started":"2021-11-23T20:13:46.953930Z","shell.execute_reply":"2021-11-23T20:13:46.959480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataframes","metadata":{}},{"cell_type":"markdown","source":"Load the raw (training) dataframe","metadata":{}},{"cell_type":"code","source":"df_train_raw = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ndf_train_raw","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:47.013337Z","iopub.execute_input":"2021-11-23T20:13:47.013764Z","iopub.status.idle":"2021-11-23T20:13:47.092931Z","shell.execute_reply.started":"2021-11-23T20:13:47.013716Z","shell.execute_reply":"2021-11-23T20:13:47.091727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check whether there is duplicate data. In this case all data is unique!","metadata":{}},{"cell_type":"code","source":"df_train_raw.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:47.095536Z","iopub.execute_input":"2021-11-23T20:13:47.095895Z","iopub.status.idle":"2021-11-23T20:13:47.117285Z","shell.execute_reply.started":"2021-11-23T20:13:47.095847Z","shell.execute_reply":"2021-11-23T20:13:47.116312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check # of frames with bounding boxes","metadata":{}},{"cell_type":"code","source":"df_train_raw[df_train_raw.annotations.str.len() > 2]","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:47.118525Z","iopub.execute_input":"2021-11-23T20:13:47.118772Z","iopub.status.idle":"2021-11-23T20:13:47.154611Z","shell.execute_reply.started":"2021-11-23T20:13:47.118740Z","shell.execute_reply":"2021-11-23T20:13:47.153957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualize one of the bounding boxes (annotations)","metadata":{}},{"cell_type":"code","source":"import ast\nast.literal_eval(df_train_raw.iloc[16].annotations)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:47.156382Z","iopub.execute_input":"2021-11-23T20:13:47.156625Z","iopub.status.idle":"2021-11-23T20:13:47.164429Z","shell.execute_reply.started":"2021-11-23T20:13:47.156594Z","shell.execute_reply":"2021-11-23T20:13:47.163505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Images","metadata":{}},{"cell_type":"markdown","source":"Validate if there is corrupted data","metadata":{}},{"cell_type":"code","source":"from os import listdir\nfrom PIL import Image\n\ndef validate_images(video_id):\n    path = '/kaggle/input/tensorflow-great-barrier-reef/train_images/video_{}/'.format(video_id)\n    \n    print(\"Verifying that video {} frames are valid...\".format(video_id))\n    for filename in listdir(path):\n        if filename.endswith('.jpg'):\n            try:\n                img = Image.open(path+filename)\n                img.verify() # Verify it is in fact an image\n            except (IOError, SyntaxError) as e:\n                print('Bad file:', filename) # Print out the names of corrupt files\n    print(\"Verified! Video {} has all valid images\".format(video_id))\n\nfor video_id in range(3):\n    validate_images(video_id)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:13:47.165996Z","iopub.execute_input":"2021-11-23T20:13:47.166355Z","iopub.status.idle":"2021-11-23T20:14:54.691841Z","shell.execute_reply.started":"2021-11-23T20:13:47.166308Z","shell.execute_reply":"2021-11-23T20:14:54.690977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load sequence of images with annotations","metadata":{}},{"cell_type":"code","source":"from PIL import Image, ImageDraw\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)\n\nprint(\"Num images: \", len(images))\nplt.imshow(images[0], interpolation='nearest')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:14:54.693024Z","iopub.execute_input":"2021-11-23T20:14:54.693260Z","iopub.status.idle":"2021-11-23T20:14:57.173920Z","shell.execute_reply.started":"2021-11-23T20:14:54.693230Z","shell.execute_reply":"2021-11-23T20:14:57.172978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visualize list of images as animation","metadata":{}},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T20:14:57.175075Z","iopub.execute_input":"2021-11-23T20:14:57.175322Z","iopub.status.idle":"2021-11-23T20:15:08.188565Z","shell.execute_reply.started":"2021-11-23T20:14:57.175291Z","shell.execute_reply":"2021-11-23T20:15:08.187515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And that's it for now! Please leave a like and share your comments! 🙂","metadata":{}}]}