{"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)\nimport matplotlib.pyplot as plt\nimport scipy.io\nimport scipy.misc\nimport PIL\nimport tensorflow as tf\nimport seaborn as sns\nimport matplotlib.image as mpimg\nfrom collections import Counter\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_names = os.listdir(\"/kaggle/input/global-wheat-detection/train/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Looking at individual image\n# Change the value of i to see different images\ni = 100\n\na = os.path.join(\"/kaggle/input/global-wheat-detection/train/\" + train_names[i])\na = mpimg.imread(a)\nplt.imshow(a)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfo = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ndfo.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfo[\"x_min\"] = dfo[\"bbox\"].str[1:-1].apply(lambda x : x.split(\",\")[0]).astype(float)\ndfo[\"y_min\"] = dfo[\"bbox\"].str[1:-1].apply(lambda x : x.split(\",\")[1]).astype(float)\ndfo[\"width\"] =  dfo[\"bbox\"].str[1:-1].apply(lambda x : x.split(\",\")[2]).astype('float32')\ndfo[\"height\"] =  dfo[\"bbox\"].str[1:-1].apply(lambda x : x.split(\",\")[3]).astype('float32')\ndfo[\"x_max\"] = dfo[\"x_min\"] + dfo[\"width\"]\ndfo[\"y_max\"] = dfo[\"y_min\"] + dfo[\"height\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfo.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ex_type = []\nfor i in train_names:\n    ex_type.append(i.split(\".\")[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Counter(ex_type)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfo[\"image_id\"] =  dfo[\"image_id\"] + \".jpg\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_ids = dfo[\"image_id\"].unique()\nimage_ids = list(set(image_ids))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"count_imageid = Counter(dfo[\"image_id\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.patches as patches\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Draw BOundary BOxes\nimid = 120 # changes this no to see different image\n\nim = np.array(Image.open(os.path.join(\"/kaggle/input/global-wheat-detection/train/\" , train_names[imid])))\nfig, ax = plt.subplots(1)\n\nax.imshow(im)\nfor i in range(count_imageid[train_names[imid]]):\n    x_min = dfo[dfo[\"image_id\"] == train_names[imid]][\"x_min\"].iloc[i]\n    y_min = dfo[dfo[\"image_id\"] == train_names[imid]][\"y_min\"].iloc[i]\n    width = dfo[dfo[\"image_id\"] == train_names[imid]][\"width\"].iloc[i]\n    height = dfo[dfo[\"image_id\"] == train_names[imid]][\"height\"].iloc[i]\n    rect = patches.Rectangle((x_min, y_min), width, height, facecolor = \"none\", linewidth = 1, edgecolor = \"r\")\n    ax.add_patch(rect)\nplt.show()","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}