{"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\n\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nfrom PIL import Image, ImageDraw\nimport tensorflow as tf\n\nimport os\nimport ast\nimport sys\nimport time\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport greatbarrierreef\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:35:47.042656Z","iopub.execute_input":"2021-12-20T17:35:47.043209Z","iopub.status.idle":"2021-12-20T17:35:54.239079Z","shell.execute_reply.started":"2021-12-20T17:35:47.043106Z","shell.execute_reply":"2021-12-20T17:35:54.238174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data imports\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef'\nimages_path = os.path.join(DATA_PATH,'train_images')\ndf_test = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"../input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"../input/tensorflow-great-barrier-reef/example_test.npy\")","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:31.279043Z","iopub.execute_input":"2021-12-20T17:44:31.279341Z","iopub.status.idle":"2021-12-20T17:44:31.328453Z","shell.execute_reply.started":"2021-12-20T17:44:31.279308Z","shell.execute_reply":"2021-12-20T17:44:31.327570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['img_path'] = os.path.join('../input/tensorflow-great-barrier-reef/train_images')+\"/video_\"+df_train.video_id.astype(str)+\"/\"+df_train.video_frame.astype(str)+\".jpg\"\ndf_train['annotations'] = df_train['annotations'].apply(lambda x: ast.literal_eval(x))\ndf_train['Number_bbox'] = df_train['annotations'].apply(lambda x:len(x)) ","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:32.553409Z","iopub.execute_input":"2021-12-20T17:44:32.553705Z","iopub.status.idle":"2021-12-20T17:44:33.028098Z","shell.execute_reply.started":"2021-12-20T17:44:32.553673Z","shell.execute_reply":"2021-12-20T17:44:33.027490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bbox_areas(annotations):\n    if not annotations:\n        return [0]\n    area_list = []\n    for annotation in annotations:\n        area_list.append(annotation['width']*annotation['height'])\n    return area_list\ndf_train[\"bbox_area\"] = df_train[\"annotations\"].apply(bbox_areas)\ndf_train[\"max_area\"] = df_train[\"bbox_area\"].apply(lambda x : max(x))\ndf_train[\"min_area\"] = df_train[\"bbox_area\"].apply(lambda x : min(x))\ndf_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:33.466253Z","iopub.execute_input":"2021-12-20T17:44:33.466530Z","iopub.status.idle":"2021-12-20T17:44:33.748400Z","shell.execute_reply.started":"2021-12-20T17:44:33.466498Z","shell.execute_reply":"2021-12-20T17:44:33.747579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_viz(df_train, id):\n    image = df_train['img_path'][id]\n    img = Image.open(image)\n    \n    for box in df_train['annotations'][id]:\n        shape = [box['x'], box['y'], box['x']+box['width'], box['y']+box['height']]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\", width=3)\n    display(img)\ndf_train.sort_values(\"max_area\", ascending=False).head()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:34.712219Z","iopub.execute_input":"2021-12-20T17:44:34.712490Z","iopub.status.idle":"2021-12-20T17:44:34.740331Z","shell.execute_reply.started":"2021-12-20T17:44:34.712464Z","shell.execute_reply":"2021-12-20T17:44:34.739775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_viz(df_train, 7336)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:35.632033Z","iopub.execute_input":"2021-12-20T17:44:35.632572Z","iopub.status.idle":"2021-12-20T17:44:35.953927Z","shell.execute_reply.started":"2021-12-20T17:44:35.632508Z","shell.execute_reply":"2021-12-20T17:44:35.952881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import collections\nimport tqdm\n\n\nall_boxes_area = []\n\nfor index, row in df_train.iterrows():\n    all_boxes_area.extend(row['bbox_area'])\n        \nax_h_w = (2,1)\n# ratio = 6\nfig1 = plt.figure(figsize=(24, 18))\nax1 = fig1.add_subplot(*ax_h_w, 1)\nax1.hist(x=all_boxes_area, bins=50)\nax2 = fig1.add_subplot(*ax_h_w, 2)\nax2.hist(x=all_boxes_area, bins=50, log=True)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:37.802264Z","iopub.execute_input":"2021-12-20T17:44:37.802550Z","iopub.status.idle":"2021-12-20T17:44:40.292584Z","shell.execute_reply.started":"2021-12-20T17:44:37.802518Z","shell.execute_reply":"2021-12-20T17:44:40.291706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['video_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:44:40.766267Z","iopub.execute_input":"2021-12-20T17:44:40.766541Z","iopub.status.idle":"2021-12-20T17:44:40.773930Z","shell.execute_reply.started":"2021-12-20T17:44:40.766513Z","shell.execute_reply":"2021-12-20T17:44:40.773315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ndef video_stats(path):\n    # Lookfor files within video folder\n    onlyfiles = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]\n    # Filter files by extension\n    onlyfiles = [f for f in onlyfiles if f.endswith(\".jpg\")]\n    im = Image.open(os.path.join(path,onlyfiles[0]))\n    width, height = im.size\n    print(f'Number of frames: {len(onlyfiles)}')\n    print(f'Frames with size (w,h): ({width},{height})')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:46:37.046997Z","iopub.execute_input":"2021-12-20T17:46:37.047305Z","iopub.status.idle":"2021-12-20T17:46:37.054195Z","shell.execute_reply.started":"2021-12-20T17:46:37.047271Z","shell.execute_reply":"2021-12-20T17:46:37.053320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Video 0 Stats:')\nvideo_stats(os.path.join(images_path,'video_0'))\n\nprint(\"\\n\",'Video 1 Stats:')\nvideo_stats(os.path.join(images_path,'video_1'))\n\nprint(\"\\n\",'Video 2 Stats:')\nvideo_stats(os.path.join(images_path,'video_2'))\n","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:46:37.973577Z","iopub.execute_input":"2021-12-20T17:46:37.974145Z","iopub.status.idle":"2021-12-20T17:46:52.225875Z","shell.execute_reply.started":"2021-12-20T17:46:37.974100Z","shell.execute_reply":"2021-12-20T17:46:52.225145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_annotations_count = df_train.groupby('Number_bbox')['annotations'].count()\ndf_annotations_count = df_annotations_count.drop([0]);\nfig = px.bar(df_annotations_count)\nfig.update_layout(xaxis=dict(type='category'), showlegend=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T17:49:03.158152Z","iopub.execute_input":"2021-12-20T17:49:03.158564Z","iopub.status.idle":"2021-12-20T17:49:04.164057Z","shell.execute_reply.started":"2021-12-20T17:49:03.158534Z","shell.execute_reply":"2021-12-20T17:49:04.163187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}