数据集转换

最近用到json格式数据集转成voc歌手,记录一下。
coco转voc可在下面代码改动即可

import os, sys
import glob
from PIL import Image
import json
import cv2
import random

out_xml_dir = 'xml/Annotations/'  ###指向voc数据集的Annotations文件夹

import pycocotools.coco as coco
# (x1,y1,w,h)
json_file = '../aluminum/annotations/train.json'
img_root ='../aluminum/images'
coco = coco.COCO(json_file)  # 读取json信息
images = coco.getImgIds()  # 获取所有图片id
for img_id in images:
    file_name = coco.loadImgs(ids=[img_id])[0]['file_name']  # 获取一张图片文件名
    img_path = os.path.join(img_root, file_name)  # 图片绝对路径
    ann_ids = coco.getAnnIds(imgIds=[img_id])  # 获取这张图片下所有box的id
    anns = coco.loadAnns(ids=ann_ids)  # 获取这张图片下所有box信息

    p1=file_name.find('images/')
    p2=file_name.find('.jpg')
    img_name=file_name[p1+7:p2]
    con = coco.loadImgs(img_id)
    w, h = con[0]['width'], con[0]['height']

    # write in xml file
    xml_file = open((out_xml_dir + '/' + img_name + '.xml'), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + str(img_name) + '.jpg' + '</filename>\n')
    xml_file.write('    <path>' + str(img_path) + '.jpg' + '</path>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(w) + '</width>\n')
    xml_file.write('        <height>' + str(h) + '</height>\n')
    xml_file.write('        <depth>1</depth>\n')
    xml_file.write('    </size>\n')
    xml_file.write('    <segmented>0</segmented>\n')
    for i in range(len(anns)):
        bbox = anns[i]['bbox']
        a1 = (bbox[0], bbox[1])
        bbox[2], bbox[3] = bbox[0] + bbox[2], bbox[1] + bbox[3]
        b1 = (bbox[2], bbox[3])
        xmin, ymin, xmax, ymax = a1[0], a1[1], b1[0], b1[1]
        gt_label=anns[i]['category_id']
        print(gt_label)
        xmin, ymin, xmax, ymax = int(xmin), int(ymin), int(xmax), int(ymax)

        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str(gt_label) + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(xmin) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(ymin) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(xmax) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(ymax) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')
    xml_file.write('</annotation>')
    xml_file.close()
#
#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值