{"cells":[{"metadata":{},"cell_type":"markdown","source":"* This notebook is for make yolo label txt files from given csv file.\n\n* **NOTE:** I removing very small and very large Bounding boxes from dataset in this notebook, becuase it might effect accuracy of model.\n\n* If you want all bounding boxes data check our [Github repo](https://github.com/DhruvMakwana/Global-Wheat-Detection).\n\n* I got removing very large and small idea from [aleksandradeis](https://www.kaggle.com/aleksandradeis) 's EDA [notebook](https://www.kaggle.com/aleksandradeis/globalwheatdetection-eda).","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfrom tqdm import tqdm\nimport ast\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir('labels')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_csv_path = '../input/global-wheat-detection/train.csv'\noutput_path = 'labels/'\nimg_path = '../input/global-wheat-detection/train/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Processing ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv = pd.read_csv(train_csv_path)\ntrain_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(train_csv['bbox'][0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here data type of **train_csv['bbox']** is string so now convert it into LIST.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv['bbox'] = train_csv['bbox'].apply(ast.literal_eval)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(train_csv['bbox'][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#groupby all images\ndf = train_csv.groupby('image_id')['bbox'].apply(list).reset_index()\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Converting into YOLO format and saving","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Loop through extracting bb from csv and saving it yolo format","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"cnt = 0\nremoved = pd.DataFrame(columns=['img_id','bb'])\nfor i,row in tqdm(df.iterrows(),total = len(df)):\n    image_name = row['image_id']\n    bboxes = row['bbox']\n    yolo_labels = []\n    for bb in bboxes:\n        xmin = bb[0]\n        ymin = bb[1]\n        w    = bb[2]\n        h    = bb[3]\n        \n        # Removing very small and very large bounding boxes \n        # This value is choose by trial and error method\n        if w*h>120000 or w*h <700.0:\n            cnt += 1\n            removed.loc[cnt,'img_id'] = image_name\n            removed.loc[cnt,'bb'] = bb\n            continue\n        \n        # converting into YOLO format\n        x_center = (xmin + w/2)/1024.0\n        y_center = (ymin + h/2)/1024.0\n        w = w/1024.0\n        h = h/1024.0\n        yolo_labels.append([0,x_center,y_center,w,h])\n    yolo_labels = np.array(yolo_labels)\n    \n    # Saving images.\n    np.savetxt(output_path+image_name+'.txt',yolo_labels,fmt=['%d', '%f', '%f', '%f', '%f'])\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Total Removed Bounding Boxes: {}'.format(len(removed)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing removed labels\n\nMost of small labels are near corner and edge of image so you have to see images carefully.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"removed = removed.groupby('img_id')['bb'].apply(list).reset_index()\nplt.figure(figsize=(24,24))\nfor i,img_id in enumerate(range(116,132)):\n    img = plt.imread(img_path + removed.loc[img_id,'img_id']+'.jpg')\n\n    for box in removed.loc[img_id,'bb']:\n        x,y,w,h = box\n\n        cv2.rectangle(img,(int(x),int(y)),(int(x+w),int(y+h)),(255,0,0),5)\n    plt.subplot(4,4,i+1)\n    plt.axis('off')\n    plt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Check some labels","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Total labels: {}'.format(len(os.listdir('labels/'))))\nprint('Total images: {}'.format(len(os.listdir(img_path))))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**NOTE** 49 images is without labels, so you have to remove that 49 images from your dataset.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('labels/'+os.listdir('labels/')[1],'r') as f:\n    temp_file = f.read()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp_file.split('\\n')","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}