{"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":"!git clone https://github.com/facebookresearch/detectron2.git\n%cd detectron2\n!python -m pip install -e ./","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initialize Templates","metadata":{}},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom PIL import ImageDraw\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np\nimport json\nimport copy\nimport os\nimport cv2\nimport ast\n\n# functions\ndef get_templates():\n    coco_json_template = {\n        \"info\":{},\n        \"images\":[],\n        \"licenses\":[],\n        \"annotations\":[],\n        \"categories\":[]\n    }\n    image_template = {\n        'license': 0, \n        'file_name': '',\n        'coco_url': None,\n        'height': None,\n        'width': None,\n        'date_captured': None,\n        'flickr_url': None,\n        'id': None\n    }\n    annotation_template = {\n        'segmentation':[[]],\n        'area':None,\n        'iscrowd':0,\n        'image_id':None,\n        'bbox':[],\n        'category_id':None,\n        'id':None\n    }\n    category_template = {\n        'supercategory': 'Coral_creatures', \n        'id': None, \n        'name': ''\n    }\n    return coco_json_template,image_template, annotation_template, category_template","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-12T10:01:09.564218Z","iopub.execute_input":"2021-12-12T10:01:09.56451Z","iopub.status.idle":"2021-12-12T10:01:09.651554Z","shell.execute_reply.started":"2021-12-12T10:01:09.56442Z","shell.execute_reply":"2021-12-12T10:01:09.65086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_json(df, base_dir, draw=False):\n    coco_json, image_template, annotation_template, category_template = get_templates()\n\n    category_ = copy.deepcopy(category_template)\n    category_[\"id\"] = 1\n    category_[\"name\"] =\"starfish\"\n    coco_json['categories'].append(category_)\n\n    annot_id = 1\n    image_id = 1\n    with tqdm(total=len(df)) as pbar:\n        for i, row in df.iterrows():\n            # get instances of annotations\n            image_prop = copy.deepcopy(image_template)\n\n            # get image properties\n            image_name = os.path.join(f\"video_{row['video_id']}\", f\"{row['video_frame']}.jpg\")\n\n            img_ = Image.open(os.path.join(base_dir, image_name))\n            img_width, img_height = img_.size\n\n            image_prop['file_name'] = image_name\n            image_prop['height'] = img_height\n            image_prop['width'] = img_width\n            image_prop['id'] = image_id\n            image_id+=1\n\n            # append the image\n            coco_json['images'].append(image_prop)\n\n            annotations = eval(row[\"annotations\"])\n            for annotation in annotations:\n                image_annot = copy.deepcopy(annotation_template)\n                bbox = [\n                    annotation[\"x\"],\n                    annotation[\"y\"],\n                    annotation[\"width\"],\n                    annotation[\"height\"]\n                ]\n\n                if draw:\n                    draw_handle = ImageDraw.Draw(img_)\n                    draw_handle.rectangle([(int(bbox[0]),int(bbox[1])),(int(bbox[2])+int(bbox[0]),int(bbox[3])+int(bbox[1]))],\n                                         width = 5)\n                    if not os.path.exists(\"output\"):\n                        os.mkdir(\"output\")\n\n                # populate the template\n                image_annot['segmentation'] = []\n                image_annot[\"area\"] = bbox[2]*bbox[3]\n                image_annot['image_id'] = image_id\n                image_annot['bbox'] = bbox\n                image_annot['category_id'] = 1\n                image_annot['id'] = annot_id\n                annot_id+=1\n\n                # append the annotations\n                coco_json['annotations'].append(image_annot)\n\n            pbar.update(1)\n            \n    return coco_json","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:01:09.652776Z","iopub.execute_input":"2021-12-12T10:01:09.653448Z","iopub.status.idle":"2021-12-12T10:01:09.669412Z","shell.execute_reply.started":"2021-12-12T10:01:09.653414Z","shell.execute_reply":"2021-12-12T10:01:09.668444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split the data and convert to json","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndf = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ntrain_df, test_df = train_test_split(df, test_size=0.2, random_state=43)\n\nprint(f\"total images in test set :- {len(train_df)}\")\nprint(f\"total images in test set :- {len(test_df)}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:01:09.670873Z","iopub.execute_input":"2021-12-12T10:01:09.671111Z","iopub.status.idle":"2021-12-12T10:01:10.157205Z","shell.execute_reply.started":"2021-12-12T10:01:09.67108Z","shell.execute_reply":"2021-12-12T10:01:10.15596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_BASE_DIR = \"/kaggle/input/tensorflow-great-barrier-reef/train_images\"\n\ntrain_json = to_json(train_df, IMG_BASE_DIR)\nwith open(\"train.json\", \"w\") as file:\n    json.dump(train_json, file)\n    \ntest_json = to_json(test_df, IMG_BASE_DIR)\nwith open(\"test.json\", \"w\") as file:\n    json.dump(test_json, file)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:01:10.159454Z","iopub.execute_input":"2021-12-12T10:01:10.159705Z","iopub.status.idle":"2021-12-12T10:01:35.356867Z","shell.execute_reply.started":"2021-12-12T10:01:10.159665Z","shell.execute_reply":"2021-12-12T10:01:35.355983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# register datasets","metadata":{}},{"cell_type":"code","source":"from detectron2.data.datasets import register_coco_instances\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom detectron2.data import detection_utils as utils\nfrom detectron2.utils.visualizer import Visualizer\nimport matplotlib.pyplot as plt\nimport random\n\ntry:\n    register_coco_instances(\"Coral_starfish_train\", {}, \"train.json\", IMG_BASE_DIR)\n    register_coco_instances(\"Coral_starfish_test\", {}, \"test.json\", IMG_BASE_DIR)\nexcept AssertionError:\n    print(\"dataset already created\")","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:02:12.58269Z","iopub.execute_input":"2021-12-12T10:02:12.583001Z","iopub.status.idle":"2021-12-12T10:02:13.348446Z","shell.execute_reply.started":"2021-12-12T10:02:12.58297Z","shell.execute_reply":"2021-12-12T10:02:13.347448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualize train Set","metadata":{}},{"cell_type":"code","source":"n = 5\ndamage_metadata = MetadataCatalog.get(\"Coral_starfish_train\")\ndataset_dicts = DatasetCatalog.get(\"Coral_starfish_train\")\nimages_with_annot = [d for d in dataset_dicts if len(d[\"annotations\"])!=0]\nprint(f\"images with atleast one annotation in train Set :- {len(images_with_annot)}\")\nfor d in random.sample(images_with_annot, n):\n    print(d)\n    # Draw ground Truths\n    img = cv2.imread(d[\"file_name\"])\n    visualizer = Visualizer(img[:, :, ::-1], metadata=damage_metadata, scale=1)\n    vis = visualizer.draw_dataset_dict(d)\n    gt_image = vis.get_image()\n\n    plt.figure(figsize=(16,9))\n    plt.imshow(gt_image)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:02:25.241172Z","iopub.execute_input":"2021-12-12T10:02:25.241517Z","iopub.status.idle":"2021-12-12T10:02:29.837173Z","shell.execute_reply.started":"2021-12-12T10:02:25.241462Z","shell.execute_reply":"2021-12-12T10:02:29.836188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualize test Set","metadata":{}},{"cell_type":"code","source":"n = 5\ndamage_metadata = MetadataCatalog.get(\"Coral_starfish_test\")\ndataset_dicts = DatasetCatalog.get(\"Coral_starfish_test\")\nimages_with_annot = [d for d in dataset_dicts if len(d[\"annotations\"])!=0]\nprint(f\"images with atleast one annotation in test Set :- {len(images_with_annot)}\")\nfor d in random.sample(images_with_annot, n):\n    print(d)\n    # Draw ground Truths\n    img = cv2.imread(d[\"file_name\"])\n    visualizer = Visualizer(img[:, :, ::-1], metadata=damage_metadata, scale=1)\n    vis = visualizer.draw_dataset_dict(d)\n    gt_image = vis.get_image()\n\n    plt.figure(figsize=(16,9))\n    plt.imshow(gt_image)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T10:02:39.300587Z","iopub.execute_input":"2021-12-12T10:02:39.30136Z","iopub.status.idle":"2021-12-12T10:02:43.253077Z","shell.execute_reply.started":"2021-12-12T10:02:39.301311Z","shell.execute_reply":"2021-12-12T10:02:43.252118Z"},"trusted":true},"execution_count":null,"outputs":[]}]}