根据COCO的Json格式转VOC的代码,定义自己的类别

import json
import os
from lxml import etree
from collections import defaultdict

def create_voc_annotation(annotation_info, output_path):
    annotation = etree.Element("annotation")

    folder = etree.Element("folder")
    folder.text = "VOC"
    annotation.append(folder)

    filename = etree.Element("filename")
    filename.text = annotation_info["filename"]
    annotation.append(filename)

    size = etree.Element("size")
    annotation.append(size)

    width = etree.Element("width")
    width.text = str(annotation_info["width"])
    size.append(width)

    height = etree.Element("height")
    height.text = str(annotation_info["height"])
    size.append(height)

    depth = etree.Element("depth")
    depth.text = "3"
    size.append(depth)

    for obj in annotation_info["objects"]:
        object_item = etree.Element("object")
        annotation.append(object_item)

        name = etree.Element("name")
        name.text = obj["name"]
        object_item.append(name)

        bndbox = etree.Element("bndbox")
        object_item.append(bndbox)

        xmin = etree.Element("xmin")
        xmin.text = str(obj["bbox"][0])
        bndbox.append(xmin)

        ymin = etree.Element("ymin")
        ymin.text = str(obj["bbox"][1])
        bndbox.append(ymin)

        xmax = etree.Element("xmax")
        xmax.text = str(obj["bbox"][2])
        bndbox.append(xmax)

        ymax = etree.Element("ymax")
        ymax.text = str(obj["bbox"][3])
        bndbox.append(ymax)

    with open(output_path, "wb") as f:
        f.write(etree.tostring(annotation, pretty_print=True))

def coco_to_voc(coco_json_path, output_dir, categories):
    with open(coco_json_path) as f:
        coco_data = json.load(f)

    id_to_category = {c["id"]: c["name"] for c in coco_data["categories"]}

    annotations = defaultdict(list)

    for ann in coco_data["annotations"]:
        if ann["category_id"] in id_to_category:
            image_id = ann["image_id"]
            category_name = id_to_category[ann["category_id"]]
            bbox = [
                int(ann["bbox"][0]),
                int(ann["bbox"][1]),
                int(ann["bbox"][0] + ann["bbox"][2]),
                int(ann["bbox"][1] + ann["bbox"][3]),
            ]

            annotations[image_id].append({"name": category_name, "bbox": bbox})

    id_to_filename = {img["id"]: img["file_name"] for img in coco_data["images"]}

    for img_id, objects in annotations.items():
        filename = id_to_filename[img_id]

        image_info = {
            "filename": filename,
            "width": coco_data["images"][0]["width"],
            "height": coco_data["images"][0]["height"],
            "objects": objects,
        }

        output_path = os.path.join(output_dir, os.path.splitext(filename)[0] + ".xml")
        create_voc_annotation(image_info, output_path)

def main():
    coco_json_path = "E:/data/merge_test.json"
    output_dir = "E:/data/train/Annotations"
    categories = [f"class{i}" for i in range(1, 20)]

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    coco_to_voc(coco_json_path, output_dir, categories)

if __name__ == "__main__":
    main()
    # json_path = r"E:/data/train/merge_train.json"
    # img_path = r"E:/data/train/train/"
    # Annotations_save_path = r'E:/data/train/Annotations'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: COCO格式JSON换为VOC格式的XML需要经过以下几个步骤: 第一步,读取COCO格式JSON文件,解析其中的对象标注数据。一般来说,COCO格式JSON中每个对象标注都包含类别、边界框位置、标注区域等信息。 第二步,根据解析得到的标注信息,生成VOC格式的XML文件。在生成XML文件时,需要按照VOC格式的要求,设置好文件头和对象标注信息。每个对象标注都需要有其类别、边界框位置、标注区域等信息。 第三步,将生成的VOC格式的XML文件保存到指定路径下。 其中,关于换的实现细节需要注意以下几点: 首先,在解析COCO格式JSON文件时,需要根据JSON结构中不同的字段和嵌套关系,逐层解析并提取出标注信息。其中,需要注意一些数据格式换,如COCO格式中的标注区域信息通常是多边形或RLE格式,需要根据VOC格式要求化为矩形。 其次,在生成VOC格式的XML文件时,需要注意文件头的设置,并遵守XML文档的一些规范。例如,每个XML文件都需要有一个根节点,对象标注的信息需要封装在“object”标签中,且标签中的文本内容需要进行编码和义。 最后,在保存XML文件时,需要确保文件目录存在及权限设置正确。此外,还可以为XML文件设置其它元信息,如创建时间、文件格式等。 综上所述,将COCO格式JSON文件换为VOC格式的XML需要按照一定的规则解析和生成文件,实现上需要注意一些细节。 ### 回答2: 要将COCO格式JSON文件换为VOC格式的XML文件,需要进行以下步骤: 1.准备好COCO格式JSON文件和VOC格式的模板XML文件。 2.读取COCO格式JSON文件,可以使用Python中的json模块来实现。 3.遍历JSON文件中的所有目标,提取出相应的信息,例如目标的类别、位置等。 4.将提取出的信息填写到VOC格式的XML模板中,并保存成XML文件。 5.可以使用Python中的xml.etree.ElementTree模块来实现XML文件的创建和编辑。 6.将换后的XML文件导入到目标检测框架中进行训练和测试。 需要注意的是,COCO格式VOC格式有很大的差异,因此在换过程中需要特别小心。同时,也需要根据具体的数据集和目标检测框架的要求进行相应的修改和调整。 ### 回答3: COCO (Common Objects in Context)格式是一种常用的目标检测数据集格式,而VOC (Visual Object Classes)格式是另一种经常用于目标检测任务的格式。在实际应用中,有时需要将COCO格式的数据换为VOC格式,以便在某些特定场景中使用。 要将COCO格式JSON换为VOC格式XML,需要进行以下几个步骤: 1. 解析COCO格式JSON数据,获得图片路径、图片大小以及目标检测框的坐标、类别等信息。 2. 根据得到的类别信息,确定VOC格式XML中用于表示目标类别的ID号。 3. 将解析得到的图片大小以及目标框坐标换为VOC格式需要的左上角坐标、右下角坐标等信息。 4. 根据得到的信息,生成VOC格式XML文件。其中,每个目标检测框对应一个对象节点,包含对象的类别、位置等信息。 需要注意的是,COCO格式VOC格式的差异比较大,对于某些特定的键值对,需要进行相应的换或忽略。此外,在进行数据换时,应注意保留足够的信息,以便后续任务能够顺利进行。 总的来说,将COCO格式JSON换为VOC格式XML需要较为复杂的代码实现,对于没有相关经验的人来说难度较大,建议寻求专业人士的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值