{"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  ## Change str -> list.\nimport sys\nimport time\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport greatbarrierreef","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:20:32.704212Z","iopub.execute_input":"2021-12-17T11:20:32.704482Z","iopub.status.idle":"2021-12-17T11:20:39.726819Z","shell.execute_reply.started":"2021-12-17T11:20:32.704454Z","shell.execute_reply":"2021-12-17T11:20:39.726158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ndf_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.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-17T11:53:30.634905Z","iopub.execute_input":"2021-12-17T11:53:30.635328Z","iopub.status.idle":"2021-12-17T11:53:30.741086Z","shell.execute_reply.started":"2021-12-17T11:53:30.635289Z","shell.execute_reply":"2021-12-17T11:53:30.739527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['annotations'] = df_train['annotations'].apply(lambda x: ast.literal_eval(x))\ndf_train['Number_bbox'] = df_train['annotations'].apply(lambda x:len(x)) \ndf_train = df_train.query(\"Number_bbox>0\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:54:44.554949Z","iopub.execute_input":"2021-12-17T11:54:44.555301Z","iopub.status.idle":"2021-12-17T11:54:44.909164Z","shell.execute_reply.started":"2021-12-17T11:54:44.555267Z","shell.execute_reply":"2021-12-17T11:54:44.908496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bbox_area(anotations):\n    area_list = []\n    for anno in anotations:\n#         print(anno)\n        area = anno[\"width\"] * anno[\"height\"]\n        area_list.append(area)\n    return area_list\n\ndf_train[\"bbox_area\"] = df_train[\"annotations\"].apply(bbox_area)\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\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:56:18.978666Z","iopub.execute_input":"2021-12-17T11:56:18.979021Z","iopub.status.idle":"2021-12-17T11:56:19.020277Z","shell.execute_reply.started":"2021-12-17T11:56:18.978977Z","shell.execute_reply":"2021-12-17T11:56:19.01919Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:59:14.575131Z","iopub.execute_input":"2021-12-17T11:59:14.575604Z","iopub.status.idle":"2021-12-17T11:59:14.580975Z","shell.execute_reply.started":"2021-12-17T11:59:14.575558Z","shell.execute_reply":"2021-12-17T11:59:14.580068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.sort_values(\"max_area\", ascending=False).head()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:58:09.979645Z","iopub.execute_input":"2021-12-17T11:58:09.979997Z","iopub.status.idle":"2021-12-17T11:58:10.006022Z","shell.execute_reply.started":"2021-12-17T11:58:09.979938Z","shell.execute_reply":"2021-12-17T11:58:10.005063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_viz(df_train, 7336)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:59:19.164318Z","iopub.execute_input":"2021-12-17T11:59:19.164761Z","iopub.status.idle":"2021-12-17T11:59:19.554664Z","shell.execute_reply.started":"2021-12-17T11:59:19.164721Z","shell.execute_reply":"2021-12-17T11:59:19.551051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.sort_values(\"min_area\").head()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T11:59:49.007945Z","iopub.execute_input":"2021-12-17T11:59:49.008268Z","iopub.status.idle":"2021-12-17T11:59:49.027527Z","shell.execute_reply.started":"2021-12-17T11:59:49.008238Z","shell.execute_reply":"2021-12-17T11:59:49.026892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_viz(df_train, 9233)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T12:00:03.320078Z","iopub.execute_input":"2021-12-17T12:00:03.320363Z","iopub.status.idle":"2021-12-17T12:00:03.736555Z","shell.execute_reply.started":"2021-12-17T12:00:03.320335Z","shell.execute_reply":"2021-12-17T12:00:03.734171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_viz(df_train, 5433)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T12:00:35.506833Z","iopub.execute_input":"2021-12-17T12:00:35.507157Z","iopub.status.idle":"2021-12-17T12:00:35.949018Z","shell.execute_reply.started":"2021-12-17T12:00:35.507118Z","shell.execute_reply":"2021-12-17T12:00:35.948286Z"},"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        \n\n# c = collections.Counter(all_boxes_area)\n# fig = plt.figure(figsize=(24, 8))\n# plt.hist(x=all_boxes_area, log=True)    \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()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T12:34:06.720741Z","iopub.execute_input":"2021-12-17T12:34:06.72107Z","iopub.status.idle":"2021-12-17T12:34:07.854981Z","shell.execute_reply.started":"2021-12-17T12:34:06.72102Z","shell.execute_reply":"2021-12-17T12:34:07.854172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(pd.Series(np.array(all_boxes_area).ravel()).describe())","metadata":{"execution":{"iopub.status.busy":"2021-12-17T12:47:54.441851Z","iopub.execute_input":"2021-12-17T12:47:54.442186Z","iopub.status.idle":"2021-12-17T12:47:54.455077Z","shell.execute_reply.started":"2021-12-17T12:47:54.442142Z","shell.execute_reply":"2021-12-17T12:47:54.454234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}