{"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-08T15:51:05.958511Z","iopub.execute_input":"2021-12-08T15:51:05.958852Z","iopub.status.idle":"2021-12-08T15:51:05.963770Z","shell.execute_reply.started":"2021-12-08T15:51:05.958818Z","shell.execute_reply":"2021-12-08T15:51:05.962923Z"},"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-08T15:51:11.331646Z","iopub.execute_input":"2021-12-08T15:51:11.332196Z","iopub.status.idle":"2021-12-08T15:51:11.335719Z","shell.execute_reply.started":"2021-12-08T15:51:11.332159Z","shell.execute_reply":"2021-12-08T15:51:11.334793Z"},"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-08T15:51:12.660138Z","iopub.execute_input":"2021-12-08T15:51:12.660534Z","iopub.status.idle":"2021-12-08T15:51:12.776090Z","shell.execute_reply.started":"2021-12-08T15:51:12.660506Z","shell.execute_reply":"2021-12-08T15:51:12.775239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['num_fishes'].hist()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T15:51:14.284034Z","iopub.execute_input":"2021-12-08T15:51:14.284312Z","iopub.status.idle":"2021-12-08T15:51:14.523133Z","shell.execute_reply.started":"2021-12-08T15:51:14.284281Z","shell.execute_reply":"2021-12-08T15:51:14.522150Z"},"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-08T15:51:17.823472Z","iopub.execute_input":"2021-12-08T15:51:17.824088Z","iopub.status.idle":"2021-12-08T15:51:17.835270Z","shell.execute_reply.started":"2021-12-08T15:51:17.824049Z","shell.execute_reply":"2021-12-08T15:51:17.834162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"showImage(train_df_with_fishes)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T15:51:26.590944Z","iopub.execute_input":"2021-12-08T15:51:26.591485Z","iopub.status.idle":"2021-12-08T15:51:27.017267Z","shell.execute_reply.started":"2021-12-08T15:51:26.591438Z","shell.execute_reply":"2021-12-08T15:51:27.016404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImageHistogram(train_df_with_fishes)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T15:52:00.489761Z","iopub.execute_input":"2021-12-08T15:52:00.490162Z","iopub.status.idle":"2021-12-08T15:52:02.239348Z","shell.execute_reply.started":"2021-12-08T15:52:00.490132Z","shell.execute_reply":"2021-12-08T15:52:02.238464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}