defaddImgItem(file_name, size): global image_id if file_name isNone: raise Exception('Could not find filename tag in xml file.') if size['width'] isNone: raise Exception('Could not find width tag in xml file.') if size['height'] isNone: raise Exception('Could not find height tag in xml file.') image_id += 1 image_item = dict() image_item['id'] = image_id image_item['file_name'] = file_name image_item['width'] = size['width'] image_item['height'] = size['height'] coco['images'].append(image_item) image_set.add(file_name) return image_id
tree = ET.parse(xml_file) root = tree.getroot() if root.tag != 'annotation': raise Exception( 'pascal voc xml root element should be annotation, rather than {}' .format(root.tag))
#elem is <folder>, <filename>, <size>, <object> for elem in root: current_parent = elem.tag current_sub = None object_name = None
if elem.tag == 'folder': continue
if elem.tag == 'filename': file_name = real_file_name #elem.text if file_name in category_set: raise Exception('file_name duplicated')
#add img item only after parse <size> tag elif current_image_id isNoneand file_name isnotNoneand size[ 'width'] isnotNone: # print(file_name, "===", image_set) if file_name notin image_set: current_image_id = addImgItem(file_name, size) print('add image with {} and {}'.format(file_name, size)) else: pass # raise Exception('duplicated image: {}'.format(file_name)) #subelem is <width>, <height>, <depth>, <name>, <bndbox> for subelem in elem: bndbox['xmin'] = None bndbox['xmax'] = None bndbox['ymin'] = None bndbox['ymax'] = None
elif current_parent == 'size': if size[subelem.tag] isnotNone: raise Exception('xml structure broken at size tag.') size[subelem.tag] = int(subelem.text)
#option is <xmin>, <ymin>, <xmax>, <ymax>, when subelem is <bndbox> for option in subelem: if current_sub == 'bndbox': if bndbox[option.tag] isnotNone: raise Exception( 'xml structure corrupted at bndbox tag.') bndbox[option.tag] = int(option.text)
#only after parse the <object> tag if bndbox['xmin'] isnotNone: if object_name isNone: raise Exception('xml structure broken at bndbox tag') if current_image_id isNone: raise Exception('xml structure broken at bndbox tag') if current_category_id isNone: raise Exception('xml structure broken at bndbox tag') bbox = [] #x bbox.append(bndbox['xmin']) #y bbox.append(bndbox['ymin']) #w bbox.append(bndbox['xmax'] - bndbox['xmin']) #h bbox.append(bndbox['ymax'] - bndbox['ymin']) print('add annotation with {},{},{},{}'.format( object_name, current_image_id, current_category_id, bbox)) addAnnoItem(object_name, current_image_id, current_category_id, bbox)
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。