{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec \nimport seaborn as sns\nfrom PIL import ImageDraw\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\n\nimport ast\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-04T10:13:40.348498Z","iopub.execute_input":"2021-12-04T10:13:40.349134Z","iopub.status.idle":"2021-12-04T10:13:41.219116Z","shell.execute_reply.started":"2021-12-04T10:13:40.349006Z","shell.execute_reply":"2021-12-04T10:13:41.218345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntest = pd.read_csv('../input/tensorflow-great-barrier-reef/test.csv')\nsample = pd.read_csv('../input/tensorflow-great-barrier-reef/example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:13:56.488423Z","iopub.execute_input":"2021-12-04T10:13:56.488674Z","iopub.status.idle":"2021-12-04T10:13:56.552972Z","shell.execute_reply.started":"2021-12-04T10:13:56.488647Z","shell.execute_reply":"2021-12-04T10:13:56.552253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:14:08.548225Z","iopub.execute_input":"2021-12-04T10:14:08.548482Z","iopub.status.idle":"2021-12-04T10:14:08.569812Z","shell.execute_reply.started":"2021-12-04T10:14:08.548455Z","shell.execute_reply":"2021-12-04T10:14:08.568967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.tail()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:14:21.308827Z","iopub.execute_input":"2021-12-04T10:14:21.309109Z","iopub.status.idle":"2021-12-04T10:14:21.319272Z","shell.execute_reply.started":"2021-12-04T10:14:21.309077Z","shell.execute_reply":"2021-12-04T10:14:21.318423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:14:43.758323Z","iopub.execute_input":"2021-12-04T10:14:43.758572Z","iopub.status.idle":"2021-12-04T10:14:43.785854Z","shell.execute_reply.started":"2021-12-04T10:14:43.758543Z","shell.execute_reply":"2021-12-04T10:14:43.785111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:14:54.068376Z","iopub.execute_input":"2021-12-04T10:14:54.068647Z","iopub.status.idle":"2021-12-04T10:14:54.088809Z","shell.execute_reply.started":"2021-12-04T10:14:54.068618Z","shell.execute_reply":"2021-12-04T10:14:54.088077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['video_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:15:01.887853Z","iopub.execute_input":"2021-12-04T10:15:01.888142Z","iopub.status.idle":"2021-12-04T10:15:01.894487Z","shell.execute_reply.started":"2021-12-04T10:15:01.888109Z","shell.execute_reply":"2021-12-04T10:15:01.893756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nax = sns.countplot(x='video_id', data=train)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:15:25.358411Z","iopub.execute_input":"2021-12-04T10:15:25.359226Z","iopub.status.idle":"2021-12-04T10:15:25.590580Z","shell.execute_reply.started":"2021-12-04T10:15:25.359177Z","shell.execute_reply":"2021-12-04T10:15:25.589884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(3):\n    print(\"Video \" + str(i))\n    print(\"Frames with Annotations : \" + str((train[train['video_id'] != i]['annotations'] != '[]').sum()) )\n    print(\"Frames without Annotations : \" + str((train[train['video_id'] == i]['annotations'] != '[]').sum()) )\n    print(\"---------\")","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:15:36.477994Z","iopub.execute_input":"2021-12-04T10:15:36.478546Z","iopub.status.idle":"2021-12-04T10:15:36.507500Z","shell.execute_reply.started":"2021-12-04T10:15:36.478508Z","shell.execute_reply":"2021-12-04T10:15:36.506701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.iloc[16].annotations","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:15:46.668505Z","iopub.execute_input":"2021-12-04T10:15:46.668779Z","iopub.status.idle":"2021-12-04T10:15:46.674774Z","shell.execute_reply.started":"2021-12-04T10:15:46.668731Z","shell.execute_reply":"2021-12-04T10:15:46.673953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['annotations'] = train['annotations'].apply(ast.literal_eval)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:15:56.278420Z","iopub.execute_input":"2021-12-04T10:15:56.278694Z","iopub.status.idle":"2021-12-04T10:15:56.602702Z","shell.execute_reply.started":"2021-12-04T10:15:56.278664Z","shell.execute_reply":"2021-12-04T10:15:56.601942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['num_bboxes'] = train['annotations'].apply(lambda x: len(x))","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:16:06.758370Z","iopub.execute_input":"2021-12-04T10:16:06.758635Z","iopub.status.idle":"2021-12-04T10:16:06.780415Z","shell.execute_reply.started":"2021-12-04T10:16:06.758605Z","shell.execute_reply":"2021-12-04T10:16:06.779705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Total rows without annotations : {}'.format(train[train['num_bboxes'] == 0]['num_bboxes'].count()))","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:16:27.297850Z","iopub.execute_input":"2021-12-04T10:16:27.298121Z","iopub.status.idle":"2021-12-04T10:16:27.308234Z","shell.execute_reply.started":"2021-12-04T10:16:27.298092Z","shell.execute_reply":"2021-12-04T10:16:27.307379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.countplot(x=train[train['num_bboxes'] > 0].num_bboxes,data=train)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:16:56.238725Z","iopub.execute_input":"2021-12-04T10:16:56.239281Z","iopub.status.idle":"2021-12-04T10:16:56.605468Z","shell.execute_reply.started":"2021-12-04T10:16:56.239244Z","shell.execute_reply":"2021-12-04T10:16:56.604801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['annotations'].str.len() > 2]","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:17:11.598242Z","iopub.execute_input":"2021-12-04T10:17:11.598513Z","iopub.status.idle":"2021-12-04T10:17:11.682560Z","shell.execute_reply.started":"2021-12-04T10:17:11.598483Z","shell.execute_reply":"2021-12-04T10:17:11.681831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-04T10:17:28.007847Z","iopub.execute_input":"2021-12-04T10:17:28.008593Z","iopub.status.idle":"2021-12-04T10:21:08.194250Z","shell.execute_reply.started":"2021-12-04T10:17:28.008548Z","shell.execute_reply":"2021-12-04T10:21:08.193518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fetch_image(df, video_id, frame_id):\n    # get frame\n    frame = df[(df['video_id'] == video_id) & (df['video_frame'] == frame_id)].iloc[0]\n    # get bounding_boxes\n    bounding_boxes = frame['annotations']\n    # open image\n    img = Image.open('/kaggle/input/tensorflow-great-barrier-reef/' + f'train_images/video_{video_id}/{frame_id}.jpg')\n\n    for box in bounding_boxes:\n        x0, y0, x1, y1 = (box['x'], box['y'], box['x']+box['width'], box['y']+box['height'])\n        draw = ImageDraw.Draw(img)\n        draw.rectangle( (x0, y0, x1, y1), outline=180, width=5)\n    return img\n\ndef fetch_image_list(df, video_id, num_images, start_frame_idx):\n    image_list = [np.array(fetch_image(df, video_id, start_frame_idx + index)) for index in range(num_images)]\n\n    return image_list","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:21:59.187956Z","iopub.execute_input":"2021-12-04T10:21:59.188534Z","iopub.status.idle":"2021-12-04T10:21:59.196235Z","shell.execute_reply.started":"2021-12-04T10:21:59.188500Z","shell.execute_reply":"2021-12-04T10:21:59.195292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = fetch_image_list(train, video_id=0, num_images=80, start_frame_idx=25)\n\nprint(f'Number of images: {len(images)}')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:22:23.949152Z","iopub.execute_input":"2021-12-04T10:22:23.949679Z","iopub.status.idle":"2021-12-04T10:22:28.120570Z","shell.execute_reply.started":"2021-12-04T10:22:23.949641Z","shell.execute_reply":"2021-12-04T10:22:28.119706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid = gridspec.GridSpec(4, 2) \nplt.figure(figsize=(18, 20))\n\nidx_list = [0, 5, 10, 15, 20, 25, 30, 35] \n\nfor i, idx in enumerate(idx_list): \n    ax = plt.subplot(grid[i])\n    plt.imshow(images[idx], interpolation='nearest')\n    ax.set_title(f'frame index {idx}')\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:22:34.188731Z","iopub.execute_input":"2021-12-04T10:22:34.189483Z","iopub.status.idle":"2021-12-04T10:22:35.912621Z","shell.execute_reply.started":"2021-12-04T10:22:34.189444Z","shell.execute_reply":"2021-12-04T10:22:35.911860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(imgs, frame_interval=130):\n    fig = plt.figure(figsize=(7, 4))\n    plt.axis('off')\n    img = plt.imshow(imgs[0])\n\n    def animate(i):\n        img.set_array(imgs[i])\n        return [img]\n\n    return animation.FuncAnimation(fig, animate, frames=len(imgs), interval=frame_interval)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:22:41.998601Z","iopub.execute_input":"2021-12-04T10:22:41.998850Z","iopub.status.idle":"2021-12-04T10:22:42.004207Z","shell.execute_reply.started":"2021-12-04T10:22:41.998821Z","shell.execute_reply":"2021-12-04T10:22:42.003257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frame_interval = 130 # set smaller number if you want to play fast, otherwise set bigger\n\ncreate_animation(images, frame_interval=frame_interval)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T10:22:45.505809Z","iopub.execute_input":"2021-12-04T10:22:45.506368Z","iopub.status.idle":"2021-12-04T10:22:57.593284Z","shell.execute_reply.started":"2021-12-04T10:22:45.506333Z","shell.execute_reply":"2021-12-04T10:22:57.592590Z"},"trusted":true},"execution_count":null,"outputs":[]}]}