{"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":"**Packages**","metadata":{}},{"cell_type":"code","source":"pip install flake8 pycodestyle_magic","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:52:48.733787Z","iopub.execute_input":"2021-12-19T21:52:48.734077Z","iopub.status.idle":"2021-12-19T21:52:56.396451Z","shell.execute_reply.started":"2021-12-19T21:52:48.734049Z","shell.execute_reply":"2021-12-19T21:52:56.395268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext pycodestyle_magic","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:52:56.398853Z","iopub.execute_input":"2021-12-19T21:52:56.399238Z","iopub.status.idle":"2021-12-19T21:52:56.404472Z","shell.execute_reply.started":"2021-12-19T21:52:56.399189Z","shell.execute_reply":"2021-12-19T21:52:56.403714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Packages import\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nfrom PIL import Image, ImageDraw\nimport ast\n\n!pip install -q imagesize\nimport imagesize","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:52:56.406024Z","iopub.execute_input":"2021-12-19T21:52:56.406346Z","iopub.status.idle":"2021-12-19T21:53:03.897063Z","shell.execute_reply.started":"2021-12-19T21:52:56.406306Z","shell.execute_reply":"2021-12-19T21:53:03.896216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\ndf = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:03.899216Z","iopub.execute_input":"2021-12-19T21:53:03.899486Z","iopub.status.idle":"2021-12-19T21:53:03.954979Z","shell.execute_reply.started":"2021-12-19T21:53:03.899454Z","shell.execute_reply":"2021-12-19T21:53:03.954256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\nprint('df size :',len(df))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:03.956194Z","iopub.execute_input":"2021-12-19T21:53:03.956445Z","iopub.status.idle":"2021-12-19T21:53:03.960563Z","shell.execute_reply.started":"2021-12-19T21:53:03.956415Z","shell.execute_reply":"2021-12-19T21:53:03.960042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# Add the link to the image\ndf['img_path'] = os.path.join('../input/tensorflow-great-barrier-reef/train_images')+\"/video_\"+df.video_id.astype(str)+\"/\"+df.video_frame.astype(str)+\".jpg\"\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:03.961726Z","iopub.execute_input":"2021-12-19T21:53:03.962088Z","iopub.status.idle":"2021-12-19T21:53:04.035158Z","shell.execute_reply.started":"2021-12-19T21:53:03.962048Z","shell.execute_reply":"2021-12-19T21:53:04.034381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploration","metadata":{}},{"cell_type":"markdown","source":"Exploration code inspired by Kartik Khandelwal notebook 📊📈Data Analysis & Visualization for Beginners","metadata":{}},{"cell_type":"code","source":"%pycodestyle_on\n# How many image per video?\nplt.figure(figsize=(8,5))\nsns.countplot(df['video_id'], color='#49A9DB').set_title('Nb of image per video')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.036503Z","iopub.execute_input":"2021-12-19T21:53:04.037508Z","iopub.status.idle":"2021-12-19T21:53:04.252067Z","shell.execute_reply.started":"2021-12-19T21:53:04.037461Z","shell.execute_reply":"2021-12-19T21:53:04.250880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# How many image with starfish ?\nwith_annotation = len(df[df['annotations'] != '[]'])\nwithout_annotation = len(df[df['annotations'] == '[]'])\n\nlabels = ['Without Bounding Box', 'With Bounding Box']\n\nfig = go.Figure([go.Bar(x=labels, \n                        y=[without_annotation, with_annotation], width=0.6)])\nfig.update_layout(title=\"Image with Starfish\", autosize=False, width=500, height=350, margin=dict(l=60, r=60, b=50, t=50, pad=4))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.253249Z","iopub.execute_input":"2021-12-19T21:53:04.253529Z","iopub.status.idle":"2021-12-19T21:53:04.280747Z","shell.execute_reply.started":"2021-12-19T21:53:04.253500Z","shell.execute_reply":"2021-12-19T21:53:04.279956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# How many starfish detected per image ? \n\n# creating new column which contains the total number of bounding boxes\ndf['No_bbox'] = df['annotations'].apply(lambda x:x.count('{')) \ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.282714Z","iopub.execute_input":"2021-12-19T21:53:04.283329Z","iopub.status.idle":"2021-12-19T21:53:04.309427Z","shell.execute_reply.started":"2021-12-19T21:53:04.283282Z","shell.execute_reply":"2021-12-19T21:53:04.308887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\nfig = px.bar(df['No_bbox'].value_counts().drop(0), title='Count of Bounding Boxes per image')\nfig.update_layout(autosize=False, width=700, height=400, margin=dict(l=60, r=60, b=50, t=50, pad=4))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.311287Z","iopub.execute_input":"2021-12-19T21:53:04.311716Z","iopub.status.idle":"2021-12-19T21:53:04.388464Z","shell.execute_reply.started":"2021-12-19T21:53:04.311684Z","shell.execute_reply":"2021-12-19T21:53:04.387948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# change 'annotations' from string to list data type using ast\ndf['annotations'] = df['annotations'].apply(ast.literal_eval)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.389667Z","iopub.execute_input":"2021-12-19T21:53:04.390046Z","iopub.status.idle":"2021-12-19T21:53:04.693636Z","shell.execute_reply.started":"2021-12-19T21:53:04.390004Z","shell.execute_reply":"2021-12-19T21:53:04.692941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# Changement de format pour les BBoxes [x,y,width,height]\ndef get_bbox(annots):\n    bboxes = [annot.values() for annot in annots]\n    return bboxes\n\ndf['bboxes'] = df.annotations.apply(get_bbox)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.696480Z","iopub.execute_input":"2021-12-19T21:53:04.697071Z","iopub.status.idle":"2021-12-19T21:53:04.729373Z","shell.execute_reply.started":"2021-12-19T21:53:04.697030Z","shell.execute_reply":"2021-12-19T21:53:04.728431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\n# Vérification des tailles des images : toutes les images ont bien la même taille\ndef get_imgsize(row):\n    row['width'], row['height'] = imagesize.get(row['img_path'])\n    return row\n\ndf = df.apply(get_imgsize,axis=1)\ndisplay(df.width.unique(), df.height.unique())\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:53:04.730818Z","iopub.execute_input":"2021-12-19T21:53:04.731053Z","iopub.status.idle":"2021-12-19T21:55:18.473009Z","shell.execute_reply.started":"2021-12-19T21:53:04.731027Z","shell.execute_reply":"2021-12-19T21:55:18.472307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\ndef img_viz(df, id):\n    image = df['img_path'][id]\n    img = Image.open(image)\n    \n    for box in df['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-19T21:55:18.474384Z","iopub.execute_input":"2021-12-19T21:55:18.474668Z","iopub.status.idle":"2021-12-19T21:55:18.482615Z","shell.execute_reply.started":"2021-12-19T21:55:18.474615Z","shell.execute_reply":"2021-12-19T21:55:18.481322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pycodestyle_on\nimg_viz(df, 40)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T21:55:18.484770Z","iopub.execute_input":"2021-12-19T21:55:18.485118Z","iopub.status.idle":"2021-12-19T21:55:18.838223Z","shell.execute_reply.started":"2021-12-19T21:55:18.485063Z","shell.execute_reply":"2021-12-19T21:55:18.837598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}