#coding:utf-8
# pip install lxml
import os
import glob
import json
import shutil
import numpy as np
import xml.etree.ElementTree as ET
path2 = "."
START_BOUNDING_BOX_ID = 1
def get(root, name):
return root.findall(name)
def get_and_check(root, name, length):
vars = root.findall(name)
if len(vars) == 0:
raise NotImplementedError('Can not find %s in %s.'%(name, root.tag))
if length > 0 and len(vars) != length:
raise NotImplementedError('The size of %s is supposed to be %d, but is %d.'%(name, length, len(vars)))
if length == 1:
vars = vars[0]
return vars
def convert(xml_list, json_file):
json_dict = {"images": [], "type": "instances", "annotations": [], "categories": []}
categories = pre_define_categories.copy()
bnd_id = START_BOUNDING_BOX_ID
all_categories = {}
for index, line in enumerate(xml_list):
# print("Processing %s"%(line))
xml_f = line
tree = ET.parse(xml_f)
root = tree.getroot()
filename = os.path.basename(xml_f)[:-4] + ".jpg"
image_id = 1 + index
size = get_and_check(root, 'size', 1)
wi
VOC转coco数据集
最新推荐文章于 2024-05-16 22:29:32 发布
本文详细介绍了如何将流行的PASCAL VOC数据集转换为COCO格式,以便更好地适应现代目标检测和分割算法的需求。转换过程包括解析VOC的XML注释文件,将类别映射到COCO的类别ID,并生成COCO格式的JSON文件。了解这个过程对于在COCO平台上训练和评估模型至关重要。
摘要由CSDN通过智能技术生成