{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Libraries","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# for file operations\nimport os\n\n# opencv for computer vision\nimport cv2\n# basic image loading and display operations\nimport matplotlib.image as mpimg\n# ploting\nimport matplotlib.pyplot as plt\n\n\n# linear algebra\nimport numpy as np \n# data processing, CSV file I/O\nimport pandas as pd ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"# Data","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## What is available ?","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"> * The data is images of wheat fields, with bounding boxes for each identified wheat head. \n> * Not all images include wheat heads / bounding boxes. \n> * The images were recorded in many locations around the world.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## What we need to predict ?","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"> * Bounding boxes around each wheat head in images that have them. \n> * If there are no wheat heads, you must predict no bounding boxes.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# list of available files\n! ls ../input/global-wheat-detection","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# no. of images in train and test folder\nprint('No. of files in train folder :', len(os.listdir('../input/global-wheat-detection/train')))\nprint('No. of files in test folder :', len(os.listdir('../input/global-wheat-detection/test')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train features and lable\ndf = pd.read_csv('../input/global-wheat-detection/train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# no. of rows and columns in the dataframe\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* looks like one image has multiple bounding box","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# images with most bounding boxes\ndf['image_id'].value_counts()[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Is there any other image size than 1024 ?\nprint('No. of images with width not equal to 1024 :', df[df['width']!=1024].shape[0])\nprint('No. of images with height not equal to 1024 :', df[df['height']!=1024].shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# how many images from each source\ndf['source'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# df[df['bbox'] == '[, , , ]']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"group_by_img_id = df['image_id'].value_counts()\nimg_wo_bbox = group_by_img_id[group_by_img_id == 1].index\ndf[df['image_id'].isin(img_wo_bbox)]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Utility functions","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot single image\n# =================\n\ndef plot_image(img_id, fig_size=(4, 4)):\n    \"\"\"Plot the image corresponds to the image_id\n    \n    Args:\n        img_src(str) : image id\n        fig_size(tuple optional) : figure size     \n    \"\"\"\n    \n    # complete image path\n    img_src = '../input/global-wheat-detection/train/' + img_id + '.jpg'\n    # read image\n    image = mpimg.imread(img_src)\n    # plot image\n    plt.figure(figsize=fig_size)\n    plt.imshow(image)\n    plt.axis('off')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot single image with bounding boxes\n# =====================================\n\ndef plot_with_bboxes(img_id, fig_size=(4, 4)):\n    \"\"\"Plot the image with bounding box corresponds to the image_id\n    \n    Args:\n        img_src(str) : image id\n        fig_size(tuple optional) : figure size     \n    \"\"\"\n    \n    # complete image path\n    img_src = '../input/global-wheat-detection/train/' + img_id + '.jpg'\n    # read image\n    img = mpimg.imread(img_src)\n    # convert image to np array\n    img = np.array(img)\n    \n    # extract bboxes\n    bboxes = [eval(i) for i in df[df['image_id'] == '7ad46c7f4']['bbox']]\n\n    # plot\n    fig, ax = plt.subplots(1, 1, figsize=fig_size)\n\n    for box in bboxes:\n\n        x = int(box[0])\n        y = int(box[1])\n        w = int(box[2])\n        h = int(box[3])\n\n        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 3)\n\n    ax.set_axis_off()\n    ax.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot images in subplots\n# =======================\n\ndef subplot_images(img_id_array, fig_size=(20, 4)):\n    \"\"\"Plot the images in a subplot\n    \n    Args:\n        img_id_array(list) : list of image ids\n        fig_size(tuple optional) : figure size     \n    \"\"\"\n    \n    # path to the dir\n    src = '../input/global-wheat-detection/train/'\n    # list of image srcs\n    img_src = [src + img_id + '.jpg' for img_id in img_id_array]\n    \n    # plot in subplots\n    fig, axes = plt.subplots(1, 5, figsize=(20, 4))\n    axes = axes.flatten()\n\n    for ind, src in enumerate(img_src[:5]):\n        \n        # read image\n        img = mpimg.imread(img_src[ind])\n        # convert image to numpy array\n        img = np.array(img)\n\n        axes[ind].set_title(img_ids[ind])\n        axes[ind].set_axis_off()\n        axes[ind].imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image('7ad46c7f4')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_with_bboxes('7ad46c7f4')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"subplot_images(img_ids[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_dir = '../input/global-wheat-detection/train/'\nimg_ids = [img.replace('.jpg', '') for img in os.listdir(img_dir)]\nimg_src = [img_dir + img for img in os.listdir(img_dir)]\nbboxes = [df[df['image_id'] == img_id]['bbox'] for img_id in img_ids]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 5, figsize=(20, 4))\naxes = axes.flatten()\n\nfor ind, src in enumerate(img_src[:5]):\n    \n    img = mpimg.imread(img_src[ind])\n    img = np.array(img)\n\n    axes[ind].imshow(img)\n    axes[ind].set_title(img_ids[ind])\n    axes[ind].set_axis_off()\n    axes[ind].imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(5, 5, figsize=(20, 20))\naxes = axes.flatten()\n\nfor ind, src in enumerate(img_src[:25]):\n    \n    img = mpimg.imread(img_src[ind])\n    img = np.array(img)\n\n    axes[ind].imshow(img)\n    axes[ind].set_title(img_ids[ind])\n    axes[ind].set_axis_off()\n    axes[ind].imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = mpimg.imread(img_src[2])\nimg = np.array(img)\n\nfig, ax = plt.subplots(1, 1, figsize=(5, 5))\n\nfor box in bboxes[2]:\n    \n    box = eval(box)\n    x = int(box[0])\n    y = int(box[1])\n    w = int(box[2])\n    h = int(box[3])\n\n    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 3)\n    \nax.set_axis_off()\nax.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(5, 5, figsize=(20, 20))\naxes = axes.flatten()\n\nfor ind, src in enumerate(img_src[:25]):\n    \n    img = mpimg.imread(img_src[ind])\n    img = np.array(img)\n\n    for box in bboxes[ind]:\n\n        box = eval(box)\n        x = int(box[0])\n        y = int(box[1])\n        w = int(box[2])\n        h = int(box[3])\n\n        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 3)\n\n    axes[ind].set_title(img_ids[ind])\n    axes[ind].set_axis_off()\n    axes[ind].imshow(img)","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}