{"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-02T01:34:56.796415Z","iopub.execute_input":"2021-12-02T01:34:56.796820Z","iopub.status.idle":"2021-12-02T01:35:10.491589Z","shell.execute_reply.started":"2021-12-02T01:34:56.796772Z","shell.execute_reply":"2021-12-02T01:35:10.490677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-02T01:35:10.493685Z","iopub.execute_input":"2021-12-02T01:35:10.494091Z","iopub.status.idle":"2021-12-02T01:35:10.498896Z","shell.execute_reply.started":"2021-12-02T01:35:10.494045Z","shell.execute_reply":"2021-12-02T01:35:10.498113Z"},"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-12-02T01:35:10.500107Z","iopub.execute_input":"2021-12-02T01:35:10.500676Z","iopub.status.idle":"2021-12-02T01:35:10.584027Z","shell.execute_reply.started":"2021-12-02T01:35:10.500635Z","shell.execute_reply":"2021-12-02T01:35:10.583123Z"},"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-12-02T01:35:10.585344Z","iopub.execute_input":"2021-12-02T01:35:10.585654Z","iopub.status.idle":"2021-12-02T01:35:10.602171Z","shell.execute_reply.started":"2021-12-02T01:35:10.585596Z","shell.execute_reply":"2021-12-02T01:35:10.601442Z"},"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-12-02T01:35:10.604030Z","iopub.execute_input":"2021-12-02T01:35:10.604274Z","iopub.status.idle":"2021-12-02T01:35:10.806956Z","shell.execute_reply.started":"2021-12-02T01:35:10.604244Z","shell.execute_reply":"2021-12-02T01:35:10.806050Z"},"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-12-02T01:35:10.808220Z","iopub.execute_input":"2021-12-02T01:35:10.808447Z","iopub.status.idle":"2021-12-02T01:35:12.358428Z","shell.execute_reply.started":"2021-12-02T01:35:10.808419Z","shell.execute_reply":"2021-12-02T01:35:12.357573Z"},"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-12-02T01:35:12.359613Z","iopub.execute_input":"2021-12-02T01:35:12.359897Z","iopub.status.idle":"2021-12-02T01:35:12.380462Z","shell.execute_reply.started":"2021-12-02T01:35:12.359867Z","shell.execute_reply":"2021-12-02T01:35:12.379645Z"},"trusted":true},"execution_count":null,"outputs":[]}]}