{"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":"## EDA  🚡","metadata":{}},{"cell_type":"code","source":"# Importing libraries \nimport os\nimport cv2\nimport ast\nimport json\nimport subprocess\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nfrom pprint import pprint\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom IPython.display import Video","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:20.897197Z","iopub.execute_input":"2022-02-28T17:13:20.897881Z","iopub.status.idle":"2022-02-28T17:13:21.301615Z","shell.execute_reply.started":"2022-02-28T17:13:20.897740Z","shell.execute_reply":"2022-02-28T17:13:21.300764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:21.303183Z","iopub.execute_input":"2022-02-28T17:13:21.303411Z","iopub.status.idle":"2022-02-28T17:13:21.373561Z","shell.execute_reply.started":"2022-02-28T17:13:21.303384Z","shell.execute_reply":"2022-02-28T17:13:21.372660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:21.374788Z","iopub.execute_input":"2022-02-28T17:13:21.375018Z","iopub.status.idle":"2022-02-28T17:13:21.405827Z","shell.execute_reply.started":"2022-02-28T17:13:21.374988Z","shell.execute_reply":"2022-02-28T17:13:21.404743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking for duplicacy \nduplicate = df.duplicated().sum()\nprint('No. of duplicates :', duplicate)\nprint(\"_______________________________\")\n# Checking for no.of annotations per file\nfor video_id in df['video_id'].unique():\n    print(f'video_id: {video_id}')\n    print(f'with (w) annotations: {sum(df[df[\"video_id\"]==video_id][\"annotations\"]==\"[]\")}')\n    print(f'without (w/o) annotations: {sum(df[df[\"video_id\"]==video_id][\"annotations\"]!=\"[]\")}\\n')","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:21.408173Z","iopub.execute_input":"2022-02-28T17:13:21.410545Z","iopub.status.idle":"2022-02-28T17:13:21.445764Z","shell.execute_reply.started":"2022-02-28T17:13:21.410504Z","shell.execute_reply":"2022-02-28T17:13:21.444804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bounding box\n\ndf[df.annotations.str.len()>2]","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:21.447283Z","iopub.execute_input":"2022-02-28T17:13:21.447823Z","iopub.status.idle":"2022-02-28T17:13:21.474489Z","shell.execute_reply.started":"2022-02-28T17:13:21.447776Z","shell.execute_reply":"2022-02-28T17:13:21.473536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing the bbox on images ⭐⭐","metadata":{}},{"cell_type":"code","source":"## Visualizing the bbox on images \n# changing the type of annotations from str to list\ndf['annotations'] = df['annotations'].apply(ast.literal_eval)\n# add path of images, number of boxes , differences\ninput_path = '../input/tensorflow-great-barrier-reef/train_images/'\ndf['image_path'] = input_path + 'video_' + df['video_id'].astype(str) + '/' + df['video_frame'].astype(str) +'.jpg'\ndf['num_bboxes'] = df['annotations'].apply(lambda x: len(x))\ndf['diff_num_bboxes'] = df['num_bboxes'].diff().fillna(0).astype(int)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:13:27.746966Z","iopub.execute_input":"2022-02-28T17:13:27.747284Z","iopub.status.idle":"2022-02-28T17:13:28.239755Z","shell.execute_reply.started":"2022-02-28T17:13:27.747253Z","shell.execute_reply":"2022-02-28T17:13:28.239120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plotting the boxes on image**","metadata":{}},{"cell_type":"code","source":"\"\"\"Getting bounding box annotations\"\"\"\ndef get_bboxes(annotations):\n    \"\"\"\n    annotations: list of annotations\n    return: bboxes as [x_min, y_min, x_max, y_max]\n    \"\"\"\n    if len(annotations) == 0:\n        return []\n    bboxes = pd.DataFrame(annotations, columns=['x', 'y', 'width', 'height']).astype(np.int32).values\n    # [x_min, y_min , w, h] ->> [x_min, y_min , x_max, y_max]\n    bboxes[:, 2] = bboxes[:, 0] + bboxes[:, 2]\n    bboxes[:, 3] = bboxes[:, 1] + bboxes[:, 3]\n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:21:26.448021Z","iopub.execute_input":"2022-02-28T17:21:26.448498Z","iopub.status.idle":"2022-02-28T17:21:26.454722Z","shell.execute_reply.started":"2022-02-28T17:21:26.448465Z","shell.execute_reply":"2022-02-28T17:21:26.453739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"plotting the image\"\"\"\ndef plot_img_with_bbox(img_path, annotations):\n    img = cv2.imread(img_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    fig , ax = plt.subplots(1,1, figsize=(16,10))\n    if len(annotations) > 0:\n        bboxes = get_bboxes(annotations)\n        for i, box in enumerate(bboxes):\n            cv2.rectangle(img,(box[0], box[1]),(box[2], box[3]), color = (255, 0, 0), thickness= 2)\n            # numbering\n            ax.text(box[0], box[1]-5, i+1, color='red')\n    ax.set_axis_off()\n    ax.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:27:28.953644Z","iopub.execute_input":"2022-02-28T17:27:28.954411Z","iopub.status.idle":"2022-02-28T17:27:28.961879Z","shell.execute_reply.started":"2022-02-28T17:27:28.954358Z","shell.execute_reply":"2022-02-28T17:27:28.960910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_idx = 18\nsample = df.iloc[sample_idx]\nprint(sample)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:28:52.226887Z","iopub.execute_input":"2022-02-28T17:28:52.227261Z","iopub.status.idle":"2022-02-28T17:28:52.235372Z","shell.execute_reply.started":"2022-02-28T17:28:52.227221Z","shell.execute_reply":"2022-02-28T17:28:52.234384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path    = sample['image_path']\nannotations = sample['annotations']\nprint('image_id:', sample['image_id'])\n# plot image with bboxes\nplot_img_with_bbox(img_path, annotations)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T17:28:54.861851Z","iopub.execute_input":"2022-02-28T17:28:54.862523Z","iopub.status.idle":"2022-02-28T17:28:55.515686Z","shell.execute_reply.started":"2022-02-28T17:28:54.862484Z","shell.execute_reply":"2022-02-28T17:28:55.514798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Work in progress !!!!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}