{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Wheat head detection\nit is an essential focus of plant breeders for wheat variety selection and improved crop productivity.<a href=\"https://sourcerer.io/mmaithani\"><img src=\"https://img.shields.io/badge/Python-124%20commits-orange.svg\" alt=\"\"></a>","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"![output](https://i.imgur.com/qgN9awQ.png)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# importing Modules","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"import re\nimport os\nimport cv2\nimport random\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom PIL  import  Image\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"scrolled":false},"cell_type":"code","source":"train=pd.read_csv('../input/global-wheat-detection/train.csv')\nsample_output = pd.read_csv('../input/global-wheat-detection/sample_submission.csv')\ntrain_dir = '../input/global-wheat-detection/train/'\ntest_dir ='../input/global-wheat-detection/test/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"train.tail()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data exploration","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"- for now i dont know is there any insights are hidden from different sources","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"print(train['source'].unique())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"- width and height are of no use","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"train.drop(['width','height'],axis=1, inplace= True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"- submission sample output :","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"sample_output.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# How many [bounding-box](https://www.google.com/search?tbm=isch&q=machine%20learning%20bounding%20box) can one image contain ?\n* image id = 35b935b6c\tcontains 116 bounding box\n* on average one image contain 20-60 wheat heads","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"train1=train\ntrain1.drop(['source'],axis=1,inplace=True)\ndf = train1.drop_duplicates().groupby('image_id').count()\ndf.sort_values(by=['bbox'],ascending= False)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Average wheat heads in a image : 20-60","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"sns.distplot(df['bbox'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Some of the images are not annotated","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"path, dirs, files = next(os.walk(\"../input/global-wheat-detection/train\"))\nfile_count = len(files)\nprint(\"Total No. of images in  train directory :\", file_count)\nprint('Total number of annotated images in train data :',len(train['image_id'].unique()),'\\n')\nprint('--- > 49 images are unlabelled ')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Extracting names of Unlabeled images","execution_count":null},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"# train directory images name\nsep = '.'\nimages_dir=[]\nfor image in files:\n    rest = image.split(sep, 1)[0]\n    images_dir.append(rest)\n    \n# train data labeled images name\nimage_labelled =[]\nfor i in train['image_id']:\n    image_labelled.append(i)\n\n# extracting unlabeled images by comparing train dir. images and train.csv dataset      \nunlabelled_images= [x for x in images_dir if x not in image_labelled]\n\nprint(len(unlabelled_images),'unlabelled images are :')\nunlabelled=[]\nfor h in unlabelled_images:\n    unlabelled.append(h+'.jpg')\n# print(unlabelled)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"\n# Can we Label the those images ?\n## Let's see","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"rows=7\ncols=7\nlis =[]\nfig, axs = plt.subplots(rows, cols, figsize=(30,30))\nfor row in range(rows):\n    for col in range(cols): \n        imgss= random.choice(unlabelled)\n        img = Image.open(train_dir +imgss)\n        axs[row, col].imshow(img)\n        axs[row, col].axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":">  no wheat heads in images :(  just grass","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Functions for plotting multiple bounding-box images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# single box plotting\ndef plot():\n    color = (0,0,255) \n    thickness = 2\n    xmax =xmin+width\n    ymax =ymin+height\n    \n    img = cv2.imread(train_dir+image_id+'.jpg')\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img= cv2.rectangle(img,(xmin,ymin), (xmax,ymax),color,thickness)\n\n    plt.figure(figsize=(20, 20))\n    plt.subplot(1, 2, 1)\n    plt.imshow(img)\n    \ndef get_bbox(image_id):\n    df=train.where(train['image_id']== image_id)\n    df =df.dropna(axis='rows')\n    arr = df[\"bbox\"].to_numpy()\n    return(arr)\n\ndef show_box(image_id):\n    df=train.where(train['image_id']== image_id)\n    df =df.dropna(axis='rows')\n    arr = df[\"bbox\"].to_numpy()\n\n    img = cv2.imread(train_dir+image_id+'.jpg')\n    # opencv always read image as BGR - conversion is must\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    for box in arr:\n        box=box[1:-1]\n        xmin,ymin,width,height= box.split(\",\")\n        # converting string to int\n        xmin= int(float(xmin)); ymin= int(float(ymin)) \n        width= int(float(width)) ; height= int(float(height))\n\n        xmax =xmin+width\n        ymax =ymin+height\n\n        img= cv2.rectangle(img,(xmin,ymin), (xmax,ymax),(255,0,0),2)\n        \n        img1 = Image.fromarray(img) # cv2 to PIL\n    return img1\n#     plt.figure(figsize=(20, 20))\n#     plt.subplot(1, 2, 1)\n#     plt.imshow(img1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_box('134d4a01c')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# let's plot most and least bounding box images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"listt = ['ce4124be0','134d4a01c','a5cb30c38','35b935b6c','f1794c924','b70c20580','f79336a8e','22f341965','86296fc32']\nrows=3\ncols=3\ni=0\nfig, axs = plt.subplots(rows, cols, figsize=(20,20))\nfor row in range(rows):\n    for col in range(cols): \n        img1=show_box(listt[i])\n        axs[row, col].imshow(img1)    \n        axs[row, col].axis('off')\n        i=i+1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Images containing Maximum wheat heads\n> Note: some bbox are totally incorrect in future for better accuracy data needs to be cleaned","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"listt=['35b935b6c','f1794c924','f79336a8e','134d4a01c','b70c20580','4cf91995d','026b6f389','c0e168cd3','690c7d8f0','a119a31fb',\n       'a06241eb3','46e480374','b79a35c8d','bf2c0adde','3014e5cad','ab026459d','2cb361be8','c742fa9ae']\nrows=3\ncols=6\ni=0\nfig, axs = plt.subplots(rows, cols, figsize=(35,18))\nfor row in range(rows):\n    for col in range(cols): \n        img1=show_box(listt[i])\n        axs[row, col].imshow(img1)    \n#         axs[row, col].axis('on')\n        i=i+1","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}