(xml_path)
| 89 | |
| 90 | |
| 91 | def parseXmlFiles(xml_path): |
| 92 | for f in os.listdir(xml_path): |
| 93 | if not f.endswith('.xml'): |
| 94 | continue |
| 95 | |
| 96 | bndbox = dict() |
| 97 | size = dict() |
| 98 | current_image_id = None |
| 99 | current_category_id = None |
| 100 | file_name = None |
| 101 | size['width'] = None |
| 102 | size['height'] = None |
| 103 | size['depth'] = None |
| 104 | |
| 105 | xml_file = os.path.join(xml_path, f) |
| 106 | |
| 107 | tree = ET.parse(xml_file) |
| 108 | root = tree.getroot() |
| 109 | if root.tag != 'annotation': |
| 110 | raise Exception('pascal voc xml root element should be annotation, rather than {}'.format(root.tag)) |
| 111 | |
| 112 | # elem is <folder>, <filename>, <size>, <object> |
| 113 | for elem in root: |
| 114 | current_parent = elem.tag |
| 115 | current_sub = None |
| 116 | object_name = None |
| 117 | |
| 118 | if elem.tag == 'folder': |
| 119 | continue |
| 120 | |
| 121 | if elem.tag == 'filename': |
| 122 | file_name = elem.text |
| 123 | if file_name in category_set: |
| 124 | raise Exception('file_name duplicated') |
| 125 | |
| 126 | # add img item only after parse <size> tag |
| 127 | elif current_image_id is None and file_name is not None and size['width'] is not None: |
| 128 | if file_name not in image_set: |
| 129 | current_image_id = addImgItem(file_name, size) |
| 130 | #print('add image with {} and {}'.format(file_name, size)) |
| 131 | else: |
| 132 | raise Exception('duplicated image: {}'.format(file_name)) |
| 133 | # subelem is <width>, <height>, <depth>, <name>, <bndbox> |
| 134 | for subelem in elem: |
| 135 | bndbox['xmin'] = None |
| 136 | bndbox['xmax'] = None |
| 137 | bndbox['ymin'] = None |
| 138 | bndbox['ymax'] = None |
| 139 | |
| 140 | current_sub = subelem.tag |
| 141 | if current_parent == 'object' and subelem.tag == 'name': |
| 142 | object_name = subelem.text |
| 143 | if object_name not in category_set: |
| 144 | current_category_id = addCatItem(object_name) |
| 145 | else: |
| 146 | current_category_id = category_set[object_name] |
| 147 | |
| 148 | elif current_parent == 'size': |
no test coverage detected