{"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 numpy as np\nfrom PIL import Image, ImageDraw\nimport pandas as pd\nimport json\nfrom os import path\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:02.509130Z","iopub.execute_input":"2021-12-08T13:45:02.509502Z","iopub.status.idle":"2021-12-08T13:45:02.534112Z","shell.execute_reply.started":"2021-12-08T13:45:02.509401Z","shell.execute_reply":"2021-12-08T13:45:02.533416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '../input/tensorflow-great-barrier-reef/train_images'\ntrain_csv_path = '../input/tensorflow-great-barrier-reef/train.csv'","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:02.535597Z","iopub.execute_input":"2021-12-08T13:45:02.536139Z","iopub.status.idle":"2021-12-08T13:45:02.540244Z","shell.execute_reply.started":"2021-12-08T13:45:02.536093Z","shell.execute_reply":"2021-12-08T13:45:02.539699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_csv_path)\ntrain_df['annotations'] = train_df['annotations'].str.replace(\"'\", '\"')\ntrain_df['annotations'] = train_df['annotations'].apply(json.loads)\ntrain_df['num_fishes'] = train_df['annotations'].apply(len)\ntrain_df_with_fishes = train_df[train_df['num_fishes']>0].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:02.541354Z","iopub.execute_input":"2021-12-08T13:45:02.541748Z","iopub.status.idle":"2021-12-08T13:45:02.786872Z","shell.execute_reply.started":"2021-12-08T13:45:02.541710Z","shell.execute_reply":"2021-12-08T13:45:02.785938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['num_fishes'].hist()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:02.788805Z","iopub.execute_input":"2021-12-08T13:45:02.789059Z","iopub.status.idle":"2021-12-08T13:45:03.064441Z","shell.execute_reply.started":"2021-12-08T13:45:02.789020Z","shell.execute_reply":"2021-12-08T13:45:03.063378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image(img_dir, video_id, video_frame):\n    img_sub_path = \"video_{}/{}.jpg\".format(video_id, video_frame)\n    img_path = path.join(img_dir, img_sub_path)\n    return Image.open(img_path)\n    \ndef draw_bounding_boxes(img, annotations):\n    for box in annotations:\n        shape = [box['x'], box['y'], box['x']+box['width'], box['y']+box['height']]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\")\n    return img\n\ndef get_random_image(df):\n    id = np.random.randint(df.shape[0])\n    img_row = df.iloc[id]\n    img = get_image(images_dir, img_row['video_id'], img_row['video_frame'])\n    return img, img_row\n\ndef showImage(df):\n    img, img_row = get_random_image(df)\n    display(draw_bounding_boxes(img, img_row['annotations'])) \n\ndef showImageDiff(df):\n    img, img_row = get_random_image(df)\n    prev_img = get_image(images_dir, img_row['video_id'], img_row['video_frame']-1)\n    diff = ImageChops.difference(prev_img, img)\n  \n    if diff.getbbox():\n        dsplay(draw_bounding_boxes(diff, img_row['annotations']))\n\ndef plotImageHistogram(df):\n    def plotImageHist(img_arr):\n        plt.hist(img_arr.ravel(), bins=256)\n        plt.show()\n    img, img_row = get_random_image(df)\n    img_arr = np.array(img)\n    display(draw_bounding_boxes(img, img_row['annotations']))\n    plotImageHist(img_arr)\n    for box in img_row['annotations']:\n        sub_img_arr = img_arr[box['y']:box['y']+box['height'], box['x']:box['x']+box['width']]\n        plotImageHist(sub_img_arr)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:47.741900Z","iopub.execute_input":"2021-12-08T13:45:47.742214Z","iopub.status.idle":"2021-12-08T13:45:47.757447Z","shell.execute_reply.started":"2021-12-08T13:45:47.742184Z","shell.execute_reply":"2021-12-08T13:45:47.756020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"showImage(train_df_with_fishes)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:03.079893Z","iopub.execute_input":"2021-12-08T13:45:03.080387Z","iopub.status.idle":"2021-12-08T13:45:03.313595Z","shell.execute_reply.started":"2021-12-08T13:45:03.080335Z","shell.execute_reply":"2021-12-08T13:45:03.312512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import greatbarrierreef\n# env = greatbarrierreef.make_env()   # initialize the environment\n# iter_test = env.iter_test()    # an iterator which loops over the test set and sample submission","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:03.315005Z","iopub.execute_input":"2021-12-08T13:45:03.315776Z","iopub.status.idle":"2021-12-08T13:45:03.319883Z","shell.execute_reply.started":"2021-12-08T13:45:03.315719Z","shell.execute_reply":"2021-12-08T13:45:03.318927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for (pixel_array, sample_prediction_df) in iter_test:\n#     plt.imshow(pixel_array)\n#     print(sample_prediction_df)\n#     break\n#     # sample_prediction_df['annotations'] = '0.5 0 0 100 100'  # make your predictions here\n#     # env.predict(sample_prediction_df)   # register your predictions","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:45:03.321114Z","iopub.execute_input":"2021-12-08T13:45:03.321348Z","iopub.status.idle":"2021-12-08T13:45:03.336115Z","shell.execute_reply.started":"2021-12-08T13:45:03.321318Z","shell.execute_reply":"2021-12-08T13:45:03.335443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImageHistogram(train_df_with_fishes)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T13:46:03.604081Z","iopub.execute_input":"2021-12-08T13:46:03.604379Z","iopub.status.idle":"2021-12-08T13:46:05.723449Z","shell.execute_reply.started":"2021-12-08T13:46:03.604337Z","shell.execute_reply":"2021-12-08T13:46:05.722910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}