参考darknet自带的voc_label.py

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join classes = ["dog", "cat"] def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
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) def convert_annotation(rootpath,xmlname):
xmlpath = rootpath + '/labels'
xmlfile = os.path.join(xmlpath,xmlname)
with open(xmlfile, "r") as in_file:
txtname = xmlname[:-4]+'.txt'
txtpath = rootpath + '/labelYOLOs'
if not os.path.exists(txtpath):
os.makedirs(txtpath)
txtfile = os.path.join(txtpath,txtname)
with open(txtfile, "w+") as out_file:
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
out_file.truncate()
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') if __name__ == "__main__":
rootpath='/home/aimhabo/YOLOv3SMP/data'
xmlpath=rootpath+'/labels'
list=os.listdir(xmlpath)
for i in range(0,len(list)) :
path = os.path.join(xmlpath,list[i])
if ('.xml' in path)or('.XML' in path):
convert_annotation(rootpath,list[i])
print('done', i)
else:
print('not xml file',i)

最新文章

  1. linux下查看和添加PATH环境变量
  2. Android系统目录介绍
  3. solrconfig.xml介绍
  4. WPF制作的小型笔记本-仿有道云笔记
  5. webapp开发要点记录
  6. ASP - MSXML2.ServerXMLHTTP & HTTPS & 证书过期 — msxml3.dll '80072f05'
  7. 每日Scrum--No.6
  8. ASP.NET后台JS弹框使前台页面样式丢失 解决办法
  9. VTKMY 3.3 VS 2010 Configuration 配置
  10. python 简单谈谈“类”
  11. 记录使用Hibernate查询bean中字段和数据库列类型不匹配问题
  12. Android游戏源代码合集(主要是AndEngine和Libgdx的)
  13. [Splay伸展树]splay树入门级教程
  14. Chrome 浏览器各版本下载大全【转载】
  15. [Android]利用run-as命令在不root情况下读取data下面的数据
  16. Linux将端口设置进防火墙的白名单
  17. (93)Wangdao.com_第二十六天_鼠标事件
  18. PentestBox在win10里打不开工具
  19. Android TimeAnimator && TimeListener翻译
  20. [phomeflashpic]怎样调用帝国CMS图片幻灯效果

热门文章

  1. Codeforces Round 504
  2. hibernate的merge()
  3. Python3.6连接mysql(一)
  4. python 中的 list dict 与 set 的关系
  5. 【漏洞复现】Tomcat CVE-2017-12615 远程代码执行漏洞
  6. elk-logstash-kibana(三)
  7. http proxy模块参数
  8. jquery parents() next() prev() 找父级别标签 找同级别标签
  9. C#遍历枚举(Enum)值
  10. GitHub 设置首页显示 404 There isn't a GitHub Pages site here.