{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-09T14:55:15.653876Z","iopub.execute_input":"2021-12-09T14:55:15.654165Z","iopub.status.idle":"2021-12-09T14:55:24.533160Z","shell.execute_reply.started":"2021-12-09T14:55:15.654132Z","shell.execute_reply":"2021-12-09T14:55:24.524210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nfrom PIL import Image\n\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:12.542133Z","iopub.execute_input":"2021-12-09T14:18:12.542386Z","iopub.status.idle":"2021-12-09T14:18:12.811247Z","shell.execute_reply.started":"2021-12-09T14:18:12.542355Z","shell.execute_reply":"2021-12-09T14:18:12.810405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\n\ntry:\n    from kaggle_secrets import UserSecretsClient\n    user_secrets = UserSecretsClient()\n    api_key = user_secrets.get_secret(\"wandb_api\")\n    wandb.login(key=api_key)\n    anony = None\nexcept:\n    anony = \"must\"\n    print('If you want to use your W&B account, go to Add-ons -> Secrets and provide your W&B access token. Use the Label name as wandb_api. \\nGet your W&B access token from here: https://wandb.ai/authorize')","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:12.812433Z","iopub.execute_input":"2021-12-09T14:18:12.812684Z","iopub.status.idle":"2021-12-09T14:18:14.474725Z","shell.execute_reply.started":"2021-12-09T14:18:12.812654Z","shell.execute_reply":"2021-12-09T14:18:14.473653Z"},"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":"2021-12-09T14:18:14.476619Z","iopub.execute_input":"2021-12-09T14:18:14.477050Z","iopub.status.idle":"2021-12-09T14:18:14.556423Z","shell.execute_reply.started":"2021-12-09T14:18:14.476972Z","shell.execute_reply":"2021-12-09T14:18:14.555599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert String to List\ndf['annotations'] = df['annotations'].apply(eval)\n\n# Get the number of bounding boxes for each image\ndf['num_bboxes'] = df['annotations'].apply(lambda x: len(x))","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:14.558492Z","iopub.execute_input":"2021-12-09T14:18:14.558822Z","iopub.status.idle":"2021-12-09T14:18:14.826886Z","shell.execute_reply.started":"2021-12-09T14:18:14.558788Z","shell.execute_reply":"2021-12-09T14:18:14.825967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('video_id').count()","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:14.828200Z","iopub.execute_input":"2021-12-09T14:18:14.828577Z","iopub.status.idle":"2021-12-09T14:18:14.857797Z","shell.execute_reply.started":"2021-12-09T14:18:14.828468Z","shell.execute_reply":"2021-12-09T14:18:14.856840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_with_boxes = df[df.num_bboxes != 0].reset_index(drop=True)\ndf_with_boxes.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:14.858879Z","iopub.execute_input":"2021-12-09T14:18:14.859573Z","iopub.status.idle":"2021-12-09T14:18:14.867692Z","shell.execute_reply.started":"2021-12-09T14:18:14.859537Z","shell.execute_reply":"2021-12-09T14:18:14.866993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run = wandb.init(project='GreatBarrier',\n                 job_type='Visualization',\n                 name='Image Visualization',\n                 anonymous='must')","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:14.868902Z","iopub.execute_input":"2021-12-09T14:18:14.869253Z","iopub.status.idle":"2021-12-09T14:18:23.099878Z","shell.execute_reply.started":"2021-12-09T14:18:14.869213Z","shell.execute_reply":"2021-12-09T14:18:23.099041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preview_table = wandb.Table(columns=['Video Id', 'Image', 'Video Frame', 'Num Boxes'])\n\nfor i in tqdm(range(len(df_with_boxes))):\n    row = df_with_boxes.loc[i]\n    img = Image.open(f'../input/tensorflow-great-barrier-reef/train_images/video_{row.video_id}/{row.video_frame}.jpg')\n    bboxes_list = []\n    for annot in row.annotations:\n        bbox = {\n            \"position\": {\n                \"minX\": annot['x'],\n                \"maxX\": annot['x'] + annot['width'],\n                \"minY\": annot['y'],\n                 \"maxY\": annot['y'] + annot['height']\n            },\n            \"class_id\": 1,\n            \"box_caption\": \"starfish\",\n            \"domain\": \"pixel\"\n        }\n        bboxes_list.append(bbox)\n        \n    image = wandb.Image(img,\n                        boxes = {\n                            \"ground_truth\": {\n                                \"box_data\": bboxes_list,\n                                \"class_labels\" : {1: 'starfish'}\n                                 }\n                        },\n                        # Add extra dummy class to get red bounding boxes\n                        classes = [{\"id\": 0, \"name\": \"none\"}, {\"id\": 1, \"name\": \"starfish\"}]\n                    )\n    preview_table.add_data(row.video_id,\n                           image,\n                           row.video_frame,\n                           len(bboxes_list))\n\nwandb.log({'Visualization': preview_table})\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2021-12-09T14:18:23.102194Z","iopub.execute_input":"2021-12-09T14:18:23.102478Z","iopub.status.idle":"2021-12-09T14:55:15.650437Z","shell.execute_reply.started":"2021-12-09T14:18:23.102443Z","shell.execute_reply":"2021-12-09T14:55:15.648408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}