{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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\n# for 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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_data=pd.read_csv(\"../input/global-wheat-detection/train.csv\")\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data[\"area\"]=[float(eval(train_data[\"bbox\"][i])[2])* float(eval(train_data[\"bbox\"][i])[3]) for i in range(len(train_data))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sorted_df=train_data.sort_values(by=[\"area\"]).reset_index(drop=True)\nsorted_df[\"bbox\"]=[eval(i) for i in sorted_df[\"bbox\"]]\nsorted_df[\"bbox_xmin\"]=[float(sorted_df[\"bbox\"][i][0]) for i in range(len(sorted_df))]\nsorted_df[\"bbox_ymin\"]=[float(sorted_df[\"bbox\"][i][1]) for i in range(len(sorted_df))]\nsorted_df[\"bbox_width\"]=[float(sorted_df[\"bbox\"][i][2]) for i in range(len(sorted_df))]\nsorted_df[\"bbox_height\"]=[float(sorted_df[\"bbox\"][i][3]) for i in range(len(sorted_df))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nTRAIN_DIR=\"../input/global-wheat-detection/train/\"\n\n\ndef get_all_bboxes(df, image_id):\n    image_bboxes = df[df.image_id == image_id]\n    \n    bboxes = []\n    for _,row in image_bboxes.iterrows():\n        bboxes.append((row.bbox_xmin, row.bbox_ymin, row.bbox_width, row.bbox_height))\n        \n    return bboxes\n\ndef plot_image_examples(df, rows=3, cols=3, title='Image examples'):\n    fig, axs = plt.subplots(rows, cols, figsize=(50,50))\n    for row in range(rows):\n        for col in range(cols):\n            idx = np.random.randint(len(df), size=1)[0]\n            img_id = df.iloc[idx].image_id\n            \n            img = Image.open(TRAIN_DIR + img_id + '.jpg')\n            axs[row, col].imshow(img)\n            \n            bboxes = get_all_bboxes(df, img_id)\n            \n            for bbox in bboxes:\n                rect = patches.Rectangle((bbox[0],bbox[1]),bbox[2],bbox[3],linewidth=3,edgecolor='r',facecolor='none')\n                axs[row, col].add_patch(rect)\n            \n            axs[row, col].axis('off')\n            \n    plt.suptitle(title)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plot bboxes with area less than 5000 pixels**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_examples(sorted_df[sorted_df[\"area\"]<5000])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plot bboxes with area less than 2000 pixels**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_examples(sorted_df[sorted_df[\"area\"]<2000])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plot bboxes with area less than 1000 pixels**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplot_image_examples(sorted_df[sorted_df[\"area\"]<1000])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_examples(sorted_df[(sorted_df[\"area\"]>200000) & (sorted_df[\"area\"]<300000)])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"drop the rows with very area less than 2000 pixels and greater than 200000 pixels\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filtered_df=sorted_df.drop(sorted_df[(sorted_df[\"area\"]>200000) | (sorted_df[\"area\"]<2000)].index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filtered_df.reset_index(drop=True, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Check if bbox height or width is greater than image height width**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filtered_df[(filtered_df[\"bbox_height\"]>1024) | (filtered_df[\"bbox_height\"]>1024)]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Check if xmin and ymin are greater than image size**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"filtered_df[(filtered_df[\"bbox_xmin\"]>1024) | (filtered_df[\"bbox_ymin\"]>1024)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f\"total bboxes before cleaning {len(train_data)}\")\nprint(f\"total bboxes after cleaning {len(filtered_df)}\")\nprint(f\"total bboxes cleaned {len(train_data)-len(filtered_df)}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filtered_df.to_csv(\"cleaned_data.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}