{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from math import ceil\nfrom pathlib import Path\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Import train image and bboxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')  \npath_train_image_dir = Path('/kaggle/input/global-wheat-detection/train/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['bbox'][0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(df_train['bbox'][0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### transform string ('[x_min, y_min, width, height]') to array","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"bboxes = np.stack(df_train['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### transform ['x_min', 'y_min', 'width', 'height'] to ['x_min', 'y_min', 'x_max', 'y_max']","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, column in enumerate(['x_min', 'y_min', 'width', 'height']):\n    df_train[column] = bboxes[:,i]\n    \ndf_train[\"x_max\"] = df_train.apply(lambda col: col.x_min + col.width, axis=1)\ndf_train[\"y_max\"] = df_train.apply(lambda col: col.y_min + col.height, axis = 1)\ndf_train.drop(columns=['bbox'], inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Check anormal range","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"display(df_train[df_train[\"x_max\"] > 1024])\ndisplay(df_train[df_train[\"y_max\"] > 1024])\ndisplay(df_train[df_train[\"x_min\"] < 0])\ndisplay(df_train[df_train[\"y_min\"] < 0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Check image size","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"list_image_filepath = list(path_train_image_dir.glob('*.jpg'))\nlen(list_image_filepath)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list_image_conf = list()\nfor idx, image_filepath in enumerate(list_image_filepath): \n    image = cv2.imread(str(image_filepath))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    width = image.shape[0]\n    height = image.shape[1]\n    area = width * height\n    list_image_conf.append(pd.Series(\n        [image_filepath.stem, width, height, area], \n        index=['filename', 'width', 'height', 'area']\n    ))\n    \ndf_image_conf = pd.concat(list_image_conf, axis=1).T\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_image_conf.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_image_conf['width'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_image_conf['height'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_image_conf['area'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"These sizes are consistent.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[['x_min', 'x_max', 'y_min', 'y_max']].max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.iloc[31785]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Width, height, x_min, y_min are to be rounded in advance.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv') \nbboxes = np.stack(df_train['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bboxes = np.round(bboxes)\ndisplay(bboxes[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, column in enumerate(['x_min', 'y_min', 'width', 'height']):\n    df_train[column] = bboxes[:,i]\n    \ndf_train[['x_min', 'y_min', 'width', 'height']] = df_train[['x_min', 'y_min', 'width', 'height']].astype(int)\ndf_train.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[\"x_max\"] = df_train.apply(lambda col: col.x_min + col.width, axis=1)\ndf_train[\"y_max\"] = df_train.apply(lambda col: col.y_min + col.height, axis = 1)\ndf_train.drop(columns=['bbox'], inplace=True)\n\ndisplay(df_train.head(5))\ndisplay(df_train.dtypes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display(df_train[df_train[\"x_max\"] > 1024])\ndisplay(df_train[df_train[\"y_max\"] > 1024])\ndisplay(df_train[df_train[\"x_min\"] < 0])\ndisplay(df_train[df_train[\"y_min\"] < 0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['class'] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['image_filename'] = df_train['image_id'].apply(lambda x: f'{x}.jpg')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Plot images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_random_images(image_folder_path, df_image_annotation, num = 12):\n    img_dict = {}\n    list_image_name = df_image_annotation['image_filename'].unique().tolist()\n\n    # randomly choose 12 image.\n    img_files_list = np.random.choice(list_image_name, num)\n\n    img_matrix_list = []\n    for img_file in img_files_list:\n        image_file_path = image_folder_path/img_file\n        img = cv2.imread(str(image_file_path))\n        img_matrix_list.append(img)\n\n    fig, axes = plt.subplots(ceil(num / 4), 4, figsize=(12, 9))\n    for idx, image in enumerate(img_matrix_list):\n        idx_row = idx // 4\n        idx_col = idx % 4\n        axes[idx_row][idx_col].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n        axes[idx_row][idx_col].set_title(img_files_list[idx])\n        \n    plt.tight_layout()\n    plt.show()\n    plt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_random_images(path_train_image_dir, df_train, num=12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_rect(image, arr_bboxes, color=(0, 0, 255)):\n    for idx, bbox in enumerate(arr_bboxes): \n        image = cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 3)\n        \n    return image\n\ndef plot_random_images_bbox(image_folder_path, df_image_annotation, num = 12):\n    img_dict = {}\n    for idx in range(len(df_image_annotation)):\n        image_name = df_image_annotation['image_filename'][idx]\n        x_min = df_image_annotation['x_min'][idx]\n        y_min = df_image_annotation['y_min'][idx]\n        x_max = df_image_annotation['x_max'][idx]\n        y_max = df_image_annotation['y_max'][idx]\n\n        if image_name not in img_dict:\n            img_dict[image_name] = list()\n        img_dict[image_name].append([x_min, y_min, x_max, y_max])\n\n    # randomly choose 12 image.\n    img_files_list = np.random.choice(list(img_dict.keys()), num)\n\n    bbox_list = []\n    img_matrix_list = []\n    \n    for img_file in img_files_list:\n        image_file_path = image_folder_path/img_file\n        img = cv2.imread(str(image_file_path))\n        bbox_list.append(img_dict[img_file])\n        img_matrix_list.append(img)\n\n    final_bbox_list = []\n    for bboxes, img in zip(bbox_list, img_matrix_list):\n        final_bbox_array = np.array([])\n        for bbox in bboxes:\n            bbox = np.array(bbox).reshape(1,4)\n            final_bbox_array = np.append(final_bbox_array, bbox)\n        final_bbox_array = final_bbox_array.reshape(-1,4).astype(int)\n        image_with_bboxes = draw_rect(img.copy(), final_bbox_array.copy(), color=(0, 0, 255))\n        final_bbox_list.append(image_with_bboxes)\n\n    fig, axes = plt.subplots(ceil(num / 4), 4, figsize=(12, 9))\n    for idx, image in enumerate(final_bbox_list):\n        idx_row = idx // 4\n        idx_col = idx % 4\n        axes[idx_row][idx_col].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n        axes[idx_row][idx_col].set_title(img_files_list[idx])\n        \n    plt.tight_layout()\n    plt.show()\n    plt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_random_images_bbox(path_train_image_dir, df_train, num=12)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}