{"cells":[{"metadata":{},"cell_type":"markdown","source":"# convert csv to json","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Hi everyone! this notebook demonstrates how to convert train.csv file to json annotations, which you can modify the bounding box using labelme.","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import cv2\nimport os\nimport pandas as pd\nfrom tqdm import tqdm\nimport json\nimport base64","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we need a json pattern which can be modified.\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"img_pth = '../input/global-wheat-detection/train/'\npattern = '../input/hourse/hourse1.json'\ntrain_csv = pd.read_csv('../input/global-wheat-detection/train.csv');train_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv['bbox'] = train_csv['bbox'].apply(lambda x: x[1:-1].split(\",\"))\ntrain_csv['x'] = train_csv['bbox'].apply(lambda x: x[0]).astype('float32')\ntrain_csv['y'] = train_csv['bbox'].apply(lambda x: x[1]).astype('float32')\ntrain_csv['w'] = train_csv['bbox'].apply(lambda x: x[2]).astype('float32')\ntrain_csv['h'] = train_csv['bbox'].apply(lambda x: x[3]).astype('float32')\n\ntrain_csv['w'] = train_csv['x'] + train_csv['w']\ntrain_csv['h'] = train_csv['y'] + train_csv['h']\ntrain_csv = train_csv.rename(columns={'x': 'x1', 'y': 'y1', 'w': 'x2', 'h': 'y2'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def readJson(pth):\n    f = open(pth, encoding='utf-8')\n    file = json.load(f)\n    return file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img_name in tqdm(os.listdir(img_pth)):\n    json_pattern = readJson(pattern)\n    #print(img_name)  # 00333207f.jpg\n    img_id = img_name.split('.')[0]\n    annos = train_csv.loc[train_csv['image_id'] == img_id]\n    #break\n    json_pattern['imagePath'] = img_name\n    json_pattern['imageHeight'] = 1024\n    json_pattern['imageWidth'] = 1024\n    json_pattern['imageData'] = base64.b64encode(open(img_pth + img_name, \"rb\").read()).strip().decode()\n    \n    json_pattern['shapes'] = []\n    n = len(annos)\n    for i in range(n):\n        anno = annos.iloc[i]\n        item = {}\n        item['label'] = 'wheat'\n        item['points'] = [[anno['x1']*1., anno['y1']*1.], [anno['x2']*1., anno['y2']*1.]]\n        item['group_id'] = None\n        item['shape_type'] = 'rectangle'\n        item['flags'] = dict()\n        json_pattern['shapes'].append(item)\n        \n    with open(\"../working/\" + img_id + '.json', 'w') as f:\n        f.write(json.dumps(json_pattern))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"then you can open the json file using labelme and do whatever you want.","execution_count":null}],"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}