{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import xml.etree.ElementTree as ET\nimport pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get image ids\nmarking = pd.read_csv('../input/global-wheat-detection/train.csv')\n\nbboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))\nfor i, column in enumerate(['x', 'y', 'w', 'h']):\n    marking[column] = bboxs[:,i]\nmarking.drop(columns=['bbox'], inplace=True)\n\n\ndf_imgs = marking['image_id']\nimage_ids = df_imgs.unique()\n# for a check\nprint(image_ids)\nprint(len(image_ids))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"width, height, depth = 1024, 1024, 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef trans_to_XML():\n    for imindex in range(len(image_ids)): # for each image\n\n        img_mark = marking[marking['image_id'] == image_ids[imindex]]\n        annotation = ET.Element('annotation')\n        \n#         print (image_ids[imindex])\n#         print(img_mark)\n        for  row in img_mark.itertuples():\n#             print(row)\n            ET.SubElement(annotation, 'folder').text = 'train'\n            ET.SubElement(annotation, 'filename').text = image_ids[imindex] + '.jpg'\n            size = ET.SubElement(annotation, 'size')\n            ET.SubElement(size, 'width').text = str(width)\n            ET.SubElement(size, 'height').text = str(height)\n            ET.SubElement(size, 'depth').text = str(depth)\n            obj = ET.SubElement(annotation, 'object')\n            ET.SubElement(obj, 'name').text = 'wheat head'\n            ET.SubElement(obj, 'pose').text = 'Unspecified'\n            ET.SubElement(obj, 'truncated').text = '0'\n            ET.SubElement(obj, 'difficult').text = '0'\n            bbox = ET.SubElement(obj, 'bndbox')\n#             print(bbox.text)\n            ET.SubElement(bbox, 'xmin').text = str(row[5])\n            ET.SubElement(bbox, 'ymin').text = str(row[6])\n            ET.SubElement(bbox, 'xmax').text = str(row[5] + row[7])\n            ET.SubElement(bbox, 'ymax').text = str(row[6] + row[8])\n\n        tree = ET.ElementTree(annotation)\n        # here is the path you want to save, I used my train picture fold\n        outputfold = ''\n        tree.write(outputfold + image_ids[imindex] + '.xml', encoding='utf-8')\n        if imindex % 100 == 0:\n            print(imindex, image_ids[imindex] + ' finished', end='\\t')\n            if imindex % 300 == 0:\n                print()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trans_to_XML()","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}