{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"<h2>Introduction</h2>","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom matplotlib.patches import Rectangle\nimport seaborn as sns\nfrom os import listdir\nfrom os.path import isfile, join","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/global-wheat-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2>I - Exploratory Data Analysis</h2>","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df['source'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['bbox'] = df['bbox'].map(lambda x: x.lstrip('[').rstrip(']'))\ndf['bbox'] = df['bbox'].str.split(',')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['source'].hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(df['image_id'].value_counts(), bins=30)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Sample image with patches**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,5))\nimg = mpimg.imread('../input/global-wheat-detection/train/0d2948e6d.jpg')\nimgplot = plt.imshow(img)\n\nfor rec_patch in df[df['image_id'] == '0d2948e6d']['bbox']:\n    plt.gca().add_patch(\n        Rectangle(\n            (float(rec_patch[0]), float(rec_patch[1])),\n            float(rec_patch[2]),\n            float(rec_patch[3]),\n            linewidth=2,\n            edgecolor='r',\n            facecolor='none'\n        ))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Random 16 images**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(4, 4, figsize=(15,15))\n\nfor row in range(0, 4):\n    for column in range(0, 4):\n        image_id = df['image_id'].sample().values[0]        \n        img = mpimg.imread('../input/global-wheat-detection/train/%s.jpg' % image_id)\n        imgplot = axes[row, column].imshow(img)\n          \n        for rec_patch in df[df['image_id'] == image_id]['bbox']:\n            axes[row, column].add_patch(\n                Rectangle(\n                    (float(rec_patch[0]), float(rec_patch[1])),\n                    float(rec_patch[2]),\n                    float(rec_patch[3]),\n                    linewidth=2,\n                    edgecolor='r',\n                    facecolor='none'\n                ))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_head_count = df['image_id'].value_counts().to_frame('head_count')\ndf_head_count = df_head_count.rename_axis('image_id').reset_index()\n\ndef get_image_source(image_id):\n    return df[df['image_id'] == image_id]['source'].unique()[0]\n\n# df_head_count['image_id'].apply(lambda image_id: df[df['image_id'] == image_id]['source'].unique()[0])\n\ndf_head_count['source'] = df_head_count['image_id'].map(get_image_source)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_head_count.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Images with 1 wheat head**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(3, 3, figsize=(15,15))\n\nfor row in range(0, 3):\n    for column in range(0, 3):\n        image_id = df_head_count[df_head_count['head_count'] == 1].sample()['image_id'].values[0]     \n        img = mpimg.imread('../input/global-wheat-detection/train/%s.jpg' % image_id)\n        imgplot = axes[row, column].imshow(img)\n          \n        for rec_patch in df[df['image_id'] == image_id]['bbox']:\n            axes[row, column].add_patch(\n                Rectangle(\n                    (float(rec_patch[0]), float(rec_patch[1])),\n                    float(rec_patch[2]),\n                    float(rec_patch[3]),\n                    linewidth=2,\n                    edgecolor='r',\n                    facecolor='none'\n                ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"**Images with less than 10 wheat heads**","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(3, 3, figsize=(15,15))\n\nfor row in range(0, 3):\n    for column in range(0, 3):\n        image_id = df_head_count[df_head_count['head_count'] <= 10].sample()['image_id'].values[0]     \n        img = mpimg.imread('../input/global-wheat-detection/train/%s.jpg' % image_id)\n        imgplot = axes[row, column].imshow(img)\n          \n        for rec_patch in df[df['image_id'] == image_id]['bbox']:\n            axes[row, column].add_patch(\n                Rectangle(\n                    (float(rec_patch[0]), float(rec_patch[1])),\n                    float(rec_patch[2]),\n                    float(rec_patch[3]),\n                    linewidth=2,\n                    edgecolor='r',\n                    facecolor='none'\n                ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plot of wheat heads per source**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='source', data=df_head_count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_head_count.groupby('source')['head_count'].mean().plot.bar()","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}