把class里面的每个class转换成你的类
import os
import xml.etree.ElementTree as ET
classid={'class1':0,'class2':1,'class3':2,'class4':3,'class5':4,'class6':5,'class7':6} #类别列表,与训练配置文件中的顺序保持一直
annotation='/data/' #xml所在的文件
savepath='/data/labels/' #写好的txt放在labels下的train和val
for xmlfile in ('train/','val/'):
file=os.listdir(annotation+xmlfile)
for i in file:
infile=annotation+xmlfile+i
outfile=open(savepath+xmlfile+i[:-4]+'.txt','w')
tree=ET.parse(infile)
root=tree.getroot()
size=root.find('size')
w_image=float(size.find('width').text)
h_image=float(size.find('height').text)
for obj in root.iter('object'):
classname=obj.find('name').text
cls_id=classid[classname]
xmlbox=obj.find('bndbox')
xmin=float(xmlbox.find('xmin').text)
xmax=float(xmlbox.find('xmax').text)
ymin=float(xmlbox.find('ymin').text)
ymax=float(xmlbox.find('ymax').text)
x_center=((xmin+xmax)/2-1)/w_image
y_center=((ymin+ymax)/2-1)/h_image
w=(xmax-xmin)/w_image
h=(ymax-ymin)/h_image
outfile.write(str(cls_id)+" "+str(x_center)+" "+str(y_center)+" "+str(w)+" "+str(h)+'\n')
outfile.close()
voc数据集转换为txt
原创
©著作权归作者所有:来自51CTO博客作者AI韬哥的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:map()
下一篇:k8s重要概念及部署k8s集群
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python PDF转换为图片
PDF 另存为图片,pdf python操作,python 转换PDF, Python PDF图片
Python 转换 图片 pdf -
VOC格式数据集转YOLO格式数据集xml txt文件 javascript
-
分项一个VOC数据集转YOLO数据集TXT的脚本
【代码】分项一个VOC数据集转YOLO数据集TXT的脚本。
YOLO python 开发语言 xml xml文件 -
基于Springcloud的学生成绩管理系统
OAuth 2 有四种授权模式,分别是授权码模式(authorization code)、简化模式(implicit)、密码模式(resource owner password credentials)、客户端模式(client credentials),具体 OAuth2 是什么,可以参考这篇文章。 本文我们将使用授权码模式和密码模式两种方式来实现用户认证和授权管理。OAuth2 其
application实现网页计数 java实现用户登录 jsp+jdbc实现用户登录 oauth2 单点登录