首页 > 其他分享 >xml转coco

xml转coco

时间:2023-01-11 17:44:06浏览次数:49  
标签:xml box savename getElementsByTagName print coco path size

import json
import os
from xml.dom.minidom import parse
import xml.dom.minidom

path = '/home/zqf/bridge/VOC2007/Annotations'

def convert(size, box):
dw = 1. /float(size[0])
dh = 1. /float(size[1])
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)

original_name = os.listdir(path)
print(original_name)

for i in original_name:
filename=os.path.join(path,i)
bret = filename.endswith(".xml")
if bret:
savename = os.path.splitext(filename)[0]
print(savename)
savename=savename+".txt"
print(filename)
print(savename)
savename=os.path.join(path,savename)

DOMTree = xml.dom.minidom.parse(filename)
collection = DOMTree.documentElement

size = collection.getElementsByTagName("size")

xwidth = size[0].getElementsByTagName('width')[0]
width=xwidth.childNodes[0].data
print(width)

xheight = size[0].getElementsByTagName('height')[0]
height=xheight.childNodes[0].data
print(height)


objects = collection.getElementsByTagName("object")

for object in objects:

bndbox = object.getElementsByTagName('bndbox')[0]

xmin = bndbox.getElementsByTagName('xmin')[0]
x1=xmin.childNodes[0].data

ymin = bndbox.getElementsByTagName('ymin')[0]
y1=ymin.childNodes[0].data

xmax = bndbox.getElementsByTagName('xmax')[0]
x2=xmax.childNodes[0].data

ymax = bndbox.getElementsByTagName('ymax')[0]
y2=ymax.childNodes[0].data

b = (float(x1), float(x2), float(y1),float(y2))
bb = convert((width, height), b)


#retstr='0 '+'%.6f'%a1+' '+'%.6f'%b1+' '+'%.6f'%a2+' '+'%.6f'%b2+'\n'
#print(retstr)
with open(savename, 'a') as f:
f.write("0 " + " ".join([str(a) for a in bb]) + '\n')






 

标签:xml,box,savename,getElementsByTagName,print,coco,path,size
From: https://www.cnblogs.com/kn-zheng/p/17044498.html

相关文章

  • 社区版idea创建maven项目失败,pom.xml为空(解决)
    产生问题如下,使用maven的web模板创建项目后项目pom文件为空,项目也没有按需生成src目录  最后解决是换了一个jdk,之前的jdk17后面换成1.8后项目结构就能够正常创建了,如......
  • log4j2.xml
    <?xmlversion="1.0"encoding="utf-8"?><configuration><properties><!--文件输出格式--><propertyname="PATTERN">%d{yyyy-MM-ddHH:mm:ss......
  • cocos creator模块
    cocoscreator的模块系统用的不是标准的common/esm,但实现了common/esm的功能(虽然有些小问题)1支持esm和common.js两种形式//getName.jscommonjs形式functiongetNa......
  • mapserver服务请求服务xml文件
    mapserver服务请求服务xml文件方法:在链接后面添加  REQUEST=getcapabilities例如:http://127.0.0.1/mapserver/vmap/WMTS/1.0/XZQH/11?REQUEST=getcapabilities ......
  • xml2Obj,xml转java对象
      importcom.aaa.WorkWeixinEncrypt;importjavax.xml.bind.JAXBContext;importjavax.xml.bind.JAXBException;importjavax.xml.bind.Marshaller;importjavax......
  • Springboot .properties或.yml配置文件读取pom.xml文件值
    需要在pom.xml中打开过滤才可以<build><resources><resource><directory>src/main/resources</directory><fi......
  • SSIS_数据库表数据导出为XML格式
    1.测试表如下,select*fromtb/*姓名课程分数--------------张三语文90张三数学83李四语文74李四数学84李四物理94*/--将转换以下格式输出SELE......
  • SQLServer 从xml 文件中提取节点数据到数据库中
    <?xmlversion="1.0"encoding="utf-8"?><!--editedwithXMLSpyv2010(http://www.altova.com)byfengshuai(founder)--><Root><Frame><Item><ID>0</ID><Nu......
  • cocos creator教程:框架 - 引导
    【muzzik教程】:框架-引导之前策划给了一个新手引导的需求,有40多步,再用之前那种随便写写的方式肯定不行了,所以我就写了一个初步的引导框架,这里分享给大家实现功能......
  • golang 解析xml 同时解析 attr 和 value
    xml长这样<ModifyKeys><ModifyKeyVKCode="91">LWin</ModifyKey><ModifyKeyVKCode="92">RWin</ModifyKey><ModifyKeyV......