{"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":"# Starfish Detection","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:38:52.122165Z","iopub.execute_input":"2021-12-17T20:38:52.122558Z","iopub.status.idle":"2021-12-17T20:38:52.12726Z","shell.execute_reply.started":"2021-12-17T20:38:52.122516Z","shell.execute_reply":"2021-12-17T20:38:52.126443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get the Data","metadata":{}},{"cell_type":"code","source":"path = \"../input/tensorflow-great-barrier-reef/\"\ntrain = pd.read_csv(path+\"train.csv\")\ntest = pd.read_csv(path+\"test.csv\")\n\n# Add images path to data\n\ntrain[\"image_path\"] = \"../input/tensorflow-great-barrier-reef/train_images/video_\"+train[\"video_id\"].astype(str)+\"/\"+train[\"image_id\"].apply(lambda x: x.split(\"-\")[1])+\".jpg\"\n\n# Reorganise columns\n\ncols = train.columns[:-2].tolist()+[\"image_path\"]+[train.columns[-2]]\ntrain = train[cols]\n\n# eval annotations\n\ntrain[\"annotations\"] = train[\"annotations\"].apply(eval)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:38:56.789551Z","iopub.execute_input":"2021-12-17T20:38:56.789807Z","iopub.status.idle":"2021-12-17T20:38:57.284027Z","shell.execute_reply.started":"2021-12-17T20:38:56.789778Z","shell.execute_reply":"2021-12-17T20:38:57.283253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualise Random Images from the Training Set And the Spotted Starfishes","metadata":{}},{"cell_type":"code","source":"# Pick a frame in the training data\n\nframe = pd.DataFrame.sample(train, n=1)\nn_starfish = frame[\"annotations\"].apply(lambda x: len(x)).tolist()[0]\nprint(\"There is {} starfish(es) in this frame.\".format(n_starfish)) if n_starfish > 0 else print(\"There is no starfish in this frame.\")\n\nplt.figure(figsize=(15, 10))\nimg_path = frame[\"image_path\"].tolist()[0]\nimg = plt.imread(img_path)\nax = plt.gca()\n\n# Plot bounding boxes\n\nann_mask = train[\"image_path\"] == img_path\nannotations = train[ann_mask][\"annotations\"].tolist()[0]\nfor bbox in annotations:\n    x, y, w, h = bbox[\"x\"], bbox[\"y\"], bbox[\"width\"], bbox[\"height\"]\n    rect = patches.Rectangle((x, y), w, h, linewidth=1, edgecolor=\"darkorange\", facecolor=\"orange\", alpha=.5)\n    ax.add_patch(rect)\nplt.imshow(img);\nplt.axis(\"off\");","metadata":{"execution":{"iopub.status.busy":"2021-12-17T20:39:09.121447Z","iopub.execute_input":"2021-12-17T20:39:09.121874Z","iopub.status.idle":"2021-12-17T20:39:09.818855Z","shell.execute_reply.started":"2021-12-17T20:39:09.121843Z","shell.execute_reply":"2021-12-17T20:39:09.817922Z"},"trusted":true},"execution_count":null,"outputs":[]}]}