{"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":"import pandas as pd\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:11:31.740448Z","iopub.execute_input":"2021-11-30T12:11:31.741120Z","iopub.status.idle":"2021-11-30T12:11:32.015935Z","shell.execute_reply.started":"2021-11-30T12:11:31.741006Z","shell.execute_reply":"2021-11-30T12:11:32.015028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_csv = \"../input/tensorflow-great-barrier-reef/train.csv\"\ntrain_df = pd.read_csv(input_csv)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:11:32.017633Z","iopub.execute_input":"2021-11-30T12:11:32.017948Z","iopub.status.idle":"2021-11-30T12:11:32.098859Z","shell.execute_reply.started":"2021-11-30T12:11:32.017906Z","shell.execute_reply":"2021-11-30T12:11:32.098005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pos_df = train_df[train_df[\"annotations\"]!=\"[]\"]  # select only the positive samples for training\nprint(f\"The no if positive examples :{len(train_pos_df)}\")\nprint(f\"The number of negative examples :{len(train_df) - len(train_pos_df)}\")","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:11:32.099938Z","iopub.execute_input":"2021-11-30T12:11:32.100156Z","iopub.status.idle":"2021-11-30T12:11:32.115644Z","shell.execute_reply.started":"2021-11-30T12:11:32.100129Z","shell.execute_reply":"2021-11-30T12:11:32.114599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../input/tensorflow-great-barrier-reef/train_images\"\ntrain_pos_df[\"annotations\"] = train_pos_df[\"annotations\"].map(eval)\nex_paths = [f\"{image_dir}/video_{int(id)}/{int(frame)}.jpg\" \n            for id, frame in zip(train_pos_df[\"video_id\"], train_pos_df[\"video_frame\"])]\ntrain_pos_df[\"img_paths\"] = ex_paths\ntrain_pos_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:11:32.117484Z","iopub.execute_input":"2021-11-30T12:11:32.117694Z","iopub.status.idle":"2021-11-30T12:11:32.305398Z","shell.execute_reply.started":"2021-11-30T12:11:32.117667Z","shell.execute_reply":"2021-11-30T12:11:32.304590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows, ncols = 2, 2\nfig, axs = plt.subplots(nrows=nrows, ncols=ncols, constrained_layout=True)\n\nshow_paths = np.reshape(list(train_pos_df[\"img_paths\"])[:4], (2, 2))\nfor i in range(nrows):\n    for j in range(ncols):\n        img = Image.open(show_paths[i, j]).convert(\"RGB\")\n        img_array = np.asarray(img)\n        annots = train_pos_df[train_pos_df[\"img_paths\"] == show_paths[i, j]][\"annotations\"]\n        if annots is not None:\n            for annot in list(annots)[0]:\n                x_min, y_min = annot[\"x\"], annot[\"y\"]\n                x_max, y_max = annot[\"x\"] + annot[\"width\"], annot[\"y\"] + annot[\"height\"]\n                img_array = cv2.rectangle(img_array, (x_min, y_min), (x_max, y_max), (255, 0, 0), 4)\n        axs[i, j].imshow(img_array)\n        print(f\"image size: {img_array.shape}\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:26:42.986615Z","iopub.execute_input":"2021-11-30T12:26:42.986946Z","iopub.status.idle":"2021-11-30T12:26:44.484343Z","shell.execute_reply.started":"2021-11-30T12:26:42.986912Z","shell.execute_reply":"2021-11-30T12:26:44.483621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pos_df[\"num_boxes\"] = train_pos_df[\"annotations\"].map(len)\ntrain_pos_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T12:27:44.166651Z","iopub.execute_input":"2021-11-30T12:27:44.167310Z","iopub.status.idle":"2021-11-30T12:27:44.187963Z","shell.execute_reply.started":"2021-11-30T12:27:44.167271Z","shell.execute_reply":"2021-11-30T12:27:44.187089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}