{"cells":[{"metadata":{},"cell_type":"markdown","source":"Convert [Global Wheat Detection DS](https://www.kaggle.com/c/global-wheat-detection) the train.csv to per image [labelme json files](https://github.com/wkentaro/labelme/tree/master/examples/bbox_detection)","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom IPython.display import Image\nimport json\nimport os\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"d = pd.read_csv('../input/global-wheat-detection/train.csv')\nd['image_id'].unique()[0]\nfor image_id in tqdm(list(d['image_id'].unique())):\n    shapes = []\n    for _, row in d[d['image_id']==image_id].iterrows():\n        x, y, w, h = json.loads(row['bbox'])\n        shapes.append({\n          \"label\": \"wheat\",\n          \"points\": [[x, y], [x+w, y+h]],\n          \"group_id\": None,\n          \"shape_type\": \"rectangle\",\n          \"flags\": {}\n        })\n    img_filename = \"{}.jpg\".format(image_id)\n    labelme = json.dumps({\n      \"version\": \"4.0.0\",\n      \"flags\": {},\n      \"shapes\": shapes,\n      \"imagePath\": img_filename,\n      \"imageData\": None,\n      \"imageHeight\": 1024,\n      \"imageWidth\": 1024\n    }, indent=2)\n    annot_filename = \"labelme/{}.json\".format(image_id)\n    os.makedirs(os.path.dirname(annot_filename), exist_ok=True)\n    with open(annot_filename, \"w\") as f:\n        f.write(labelme)","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}