把voc格式的标注文件.xml转为coco格式的.json文件

本文介绍了如何将使用labelimg标注的XML格式文件转换为COCO格式的JSON文件,通过GitHub上的脚本pascal_voc_xml2json.py实现。该脚本读取XML文件并生成instances.json,其中包含'images'、'annotations'和'categories'等信息,用于目标检测模型的训练。
摘要由CSDN通过智能技术生成

在训练目标检测模型的时候一般使用labelimg标注的图像生产.xml格式的标注文件。有时候需要用到coco格式的json标注文件,在github找到了一个xml转json的脚本。(https://github.com/CivilNet/Gemfield/blob/master/src/python/pascal_voc_xml2json/pascal_voc_xml2json.py)

执行该脚本会读取Annotations下的.xml文件并解析其中的类别及boundbox的坐标,最后生成instances.json的文件。

这里使用了4张图像的xml进行测试。图像名字为2007_000027.jpg 2007_000032.jpg 2007_000033.jpg 2007_000039.jpg。

如下图所示为instances.json文件内容。从下图可以看到,coco的json标注格式实际上是一个大字典{},里面包括了“images”,“annotations”,“type”,"categories"等信息(为了便于观察,图中画出的双箭头表示该属性从开始到结束的范围)。"images"存放每个图像的名字宽高及图像id,"annotations"存放对应相同图像id的图像box的四个坐标位置及该框的类别id,"categories"则表示每个类别id到该类真实名字的对应关系。

在这里插入图片描述

    #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 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值