{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Notebook for converting the bounding boxes as per YOLO format","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 seaborn as sns\nimport cv2\nfrom ast import literal_eval","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/global-wheat-detection/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convert(size, box):\n    dw = 1. / size[0]\n    dh = 1. / size[1]\n    x = (box[0] + box[1]) / 2.0\n    y = (box[2] + box[3]) / 2.0\n    w = box[1] - box[0]\n    h = box[3] - box[2]\n    x = x * dw\n    w = w * dw\n    y = y * dh\n    h = h * dh\n    return [x, y, w, h]\n\ndef convert_to_yolo_label(coco_format_box, w = 1024, h = 1024):\n    bbox = literal_eval(coco_format_box)\n    xmin = bbox[0]\n    xmax = bbox[0] + bbox[2]\n    ymin = bbox[1]\n    ymax = bbox[1] + bbox[3]\n    b = (float(xmin), float(xmax), float(ymin), float(ymax))\n    yolo_box = convert((w, h), b)\n    if np.max(yolo_box) > 1 or np.min(yolo_box) < 0: # Take this opportunity to check that conversion works\n        print(\"BOX HAS AN ISSUE\")\n    return yolo_box","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['yolo_box'] = train_df.bbox.apply(convert_to_yolo_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"unique_img_ids = train_df.image_id.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(unique_img_ids)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img_id in tqdm(unique_img_ids):\n    filt_df = train_df.query(\"image_id == @img_id\")\n    all_boxes = filt_df.yolo_box.values\n    file_name = img_id+\".txt\"\n\n    s = \"0 %s %s %s %s \\n\"\n    with open(file_name, 'a') as file:\n        for i in all_boxes:\n            new_line = (s % tuple(i))\n            file.write(new_line)  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nall_imgs = glob.glob(\"../input/global-wheat-detection/train/*.jpg\")\nall_imgs = [i.split(\"/\")[-1].replace(\".jpg\", \"\") for i in all_imgs]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"negative_images = set(all_imgs) - set(unique_img_ids)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in tqdm(list(negative_images)):\n    file_name = i+\".txt\"\n    with open(file_name, 'w') as fp: \n        pass","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}