Python 生成VOC格式的标签实例
作者:疯狂的小猪oO 时间:2021-09-09 04:39:07
常用目标检测模型基本都是读取的PASCAL VOC格式的标签,下面代码用于生成VOC格式的代码,根据需要修改即可:
from lxml import etree, objectify
def gen_txt(filename, h, w, c):
E = objectify.ElementMaker(annotate=False)
anno_tree = E.annotation(
E.folder('VOC_OPEN_IMAGE'),
E.filename(filename),
E.source(
E.database('The VOC2007 Database'),
E.annotation('PASCAL VOC2007'),
E.image('flickr'),
E.flickrid("341012865")
),
E.size(
E.width(w),
E.height(h),
E.depth(c)
),
E.segmented(0),
E.object(
E.name('1'),
E.pose('left'),
E.truncated('1'),
E.difficult('0'),
E.bndbox(
E.xmin('0'),
E.ymin('0'),
E.xmax('0'),
E.ymax('0')
)
),
)
etree.ElementTree(anno_tree).write('ann/'+filename[:-4]+".xml", pretty_print=True)
补充知识: python对PASCAL VOC标注数据进行统计
用于统计训练数据中的类别,以及所有目标的个数:
# coding:utf-8
import xml.etree.cElementTree as ET
import os
from collections import Counter
import shutil
# Counter({'towCounter({'tower': 3074, 'windpower': 2014, 'thermalpower': 689, 'hydropower': 261, 'transformer': 225})
# total_num: 6263
def count(pathdir,despath):
category = []
path = pathdir + '/XML/'
for index,xml in enumerate(os.listdir(path)):
# print(str(index) + ' xml: '+ xml)
root = ET.parse(os.path.join(path, xml))
objects = root.findall('object')
# ==================select images which has a special object=============
for obj in objects:
obj_label = obj.find('name').text
if obj_label == 'transformer':
print(xml)
imgfile = pathdir + 'JPEG/' + xml.replace('xml', 'jpg')
img_despath = despath + xml.replace('xml', 'jpg')
# if not os.path.exists(img_despath):
shutil.copyfile(imgfile, img_despath)
# ==================select images which has a special object=============
category += [ob.find('name').text for ob in objects]
print(Counter(category))
total_num = sum([value for key, value in Counter(category).items()])
print('total_num:',total_num)
if __name__ == '__main__':
# pathdirs = list(set(os.listdir('./')) ^ set(['tools','count.py']))
# print(pathdirs)
# for pathdir in pathdirs:
pathdir = '/summer/Desktop/power_traindata/'
despath = '/transformer/'
count(pathdir,despath)
来源:https://blog.csdn.net/u014657795/article/details/81154731
标签:Python,VOC,标签
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python将视频转换为全字符视频
2023-08-08 21:01:02
![](https://img.aspxhome.com/file/2023/8/61748_0s.gif)
IE下中英文字体不能对齐原因及解决
2008-08-11 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20088/11/2008811125353679s.jpg)
Python使用pandas对数据进行差分运算的方法
2021-09-28 06:56:07
MS Server和Oracle中对NULL处理的一些细节差异
2009-06-10 17:35:00
Python线性网络实现分类糖尿病病例
2022-03-13 11:23:25
![](https://img.aspxhome.com/file/2023/9/70179_0s.png)
Go学习笔记之Zap日志的使用
2023-09-19 01:21:36
PHP中Http协议post请求参数
2023-11-16 18:38:38
怎样在SQL Server中去除表中不可见字符
2009-02-05 15:23:00
XML卷之实战锦囊(1):动态排序
2008-09-05 17:20:00
gethostbyaddr在Python3中引发UnicodeDecodeError
2023-06-15 09:34:33
Go语言HTTPServer开发的六种方式小结
2023-06-22 21:48:21
ASP中SESSION无法保存问题的解决办法
2023-06-27 15:40:12
![](https://img.aspxhome.com/file/UploadPic/201211/30/20121130203846956s.jpg)
Python selenium环境搭建实现过程解析
2023-10-06 06:18:48
![](https://img.aspxhome.com/file/2023/1/63271_0s.png)
Python三元运算与lambda表达式实例解析
2023-06-14 16:34:09
![](https://img.aspxhome.com/file/2023/9/59149_0s.png)
PHP字典树(Trie树)定义与实现方法示例
2023-11-15 00:39:50
![](https://img.aspxhome.com/file/2023/1/55821_0s.png)
如何绕过ODBC直接访问SQL Server?
2010-05-18 18:13:00
Python中列表与元组的乘法操作示例
2021-05-09 17:11:25
oracle下实现恢复一个丢失的数据文件的代码
2009-03-02 11:02:00
php连接MySQL的两种方式对比
2023-11-16 23:34:26
MySQL6.0新增特性
2010-03-25 10:08:00