{"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":"What are Crown-of-thorns starfish? https://en.wikipedia.org/wiki/Crown-of-thorns_starfish","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nimport ast\nfrom PIL.ImageDraw import Draw","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-22T20:41:53.654389Z","iopub.execute_input":"2021-11-22T20:41:53.654836Z","iopub.status.idle":"2021-11-22T20:41:53.659067Z","shell.execute_reply.started":"2021-11-22T20:41:53.654805Z","shell.execute_reply":"2021-11-22T20:41:53.658445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = Path('/kaggle/input/tensorflow-great-barrier-reef')\ninput_path.ls()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:20:09.470218Z","iopub.execute_input":"2021-11-22T20:20:09.471068Z","iopub.status.idle":"2021-11-22T20:20:09.480787Z","shell.execute_reply.started":"2021-11-22T20:20:09.471030Z","shell.execute_reply":"2021-11-22T20:20:09.479019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(input_path/'train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:20:11.146156Z","iopub.execute_input":"2021-11-22T20:20:11.146555Z","iopub.status.idle":"2021-11-22T20:20:11.198070Z","shell.execute_reply.started":"2021-11-22T20:20:11.146519Z","shell.execute_reply":"2021-11-22T20:20:11.197230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.video_id.unique()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:20:30.445431Z","iopub.execute_input":"2021-11-22T20:20:30.445985Z","iopub.status.idle":"2021-11-22T20:20:30.457013Z","shell.execute_reply.started":"2021-11-22T20:20:30.445948Z","shell.execute_reply":"2021-11-22T20:20:30.455855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are only three videos in our dataset -- each video has a certaain number of frames (images)","metadata":{}},{"cell_type":"code","source":"train_df.annotations.map(len).value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:20:11.368312Z","iopub.execute_input":"2021-11-22T20:20:11.368896Z","iopub.status.idle":"2021-11-22T20:20:11.388821Z","shell.execute_reply.started":"2021-11-22T20:20:11.368855Z","shell.execute_reply":"2021-11-22T20:20:11.387851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"18582/train_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:20:13.197068Z","iopub.execute_input":"2021-11-22T20:20:13.198036Z","iopub.status.idle":"2021-11-22T20:20:13.204294Z","shell.execute_reply.started":"2021-11-22T20:20:13.197986Z","shell.execute_reply":"2021-11-22T20:20:13.203555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"80% of the images do not have a starfish!","metadata":{}},{"cell_type":"code","source":"train_df[train_df.annotations.map(len) > 100].iloc[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:23:41.480881Z","iopub.execute_input":"2021-11-22T20:23:41.481228Z","iopub.status.idle":"2021-11-22T20:23:41.501255Z","shell.execute_reply.started":"2021-11-22T20:23:41.481197Z","shell.execute_reply":"2021-11-22T20:23:41.497807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open((input_path/'train_images/video_0/57.jpg'))\nimg","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:45:22.761602Z","iopub.execute_input":"2021-11-22T20:45:22.762306Z","iopub.status.idle":"2021-11-22T20:45:23.151513Z","shell.execute_reply.started":"2021-11-22T20:45:22.762225Z","shell.execute_reply":"2021-11-22T20:45:23.150595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are two starfish in this frame, let's add the bounding boxes","metadata":{}},{"cell_type":"code","source":"annotations = train_df[train_df.annotations.map(len) > 100].iloc[0].annotations\nannotations","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:40:46.970476Z","iopub.execute_input":"2021-11-22T20:40:46.970769Z","iopub.status.idle":"2021-11-22T20:40:46.991454Z","shell.execute_reply.started":"2021-11-22T20:40:46.970740Z","shell.execute_reply":"2021-11-22T20:40:46.990544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bboxes(annotations):\n    annots = ast.literal_eval(annotations)\n    bboxes = []\n    for annotation in annots:\n        x, y = annotation['x'], annotation['y']\n        w, h = annotation['width'], annotation['height']\n        bboxes.append([x,y, x+w, y+h])\n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:40:47.691135Z","iopub.execute_input":"2021-11-22T20:40:47.691951Z","iopub.status.idle":"2021-11-22T20:40:47.697865Z","shell.execute_reply.started":"2021-11-22T20:40:47.691901Z","shell.execute_reply":"2021-11-22T20:40:47.697007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxes = get_bboxes(annotations)\nbboxes","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:40:54.867762Z","iopub.execute_input":"2021-11-22T20:40:54.868041Z","iopub.status.idle":"2021-11-22T20:40:54.873761Z","shell.execute_reply.started":"2021-11-22T20:40:54.868011Z","shell.execute_reply":"2021-11-22T20:40:54.872851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_bboxes(img, bboxes):\n    draw = Draw(img)\n    for bbox in bboxes:\n        draw.rectangle(bbox, outline='red')\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:45:44.974795Z","iopub.execute_input":"2021-11-22T20:45:44.975095Z","iopub.status.idle":"2021-11-22T20:45:44.980602Z","shell.execute_reply.started":"2021-11-22T20:45:44.975065Z","shell.execute_reply":"2021-11-22T20:45:44.979653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_bboxes(img, bboxes)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T20:45:45.996420Z","iopub.execute_input":"2021-11-22T20:45:45.996732Z","iopub.status.idle":"2021-11-22T20:45:46.344235Z","shell.execute_reply.started":"2021-11-22T20:45:45.996700Z","shell.execute_reply":"2021-11-22T20:45:46.342321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Both starfish appropriately drawn in the image!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}