Python 生成VOC格式的標(biāo)簽實(shí)例
常用目標(biāo)檢測模型基本都是讀取的PASCAL VOC格式的標(biāo)簽,下面代碼用于生成VOC格式的代碼,根據(jù)需要修改即可:
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)
補(bǔ)充知識(shí): python對(duì)PASCAL VOC標(biāo)注數(shù)據(jù)進(jìn)行統(tǒng)計(jì)
用于統(tǒng)計(jì)訓(xùn)練數(shù)據(jù)中的類別,以及所有目標(biāo)的個(gè)數(shù):
# 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)
以上這篇Python 生成VOC格式的標(biāo)簽實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pytest實(shí)現(xiàn)多種調(diào)用方式
pytest是一個(gè)非常成熟的全功能的Python測試框架,本文主要介紹了pytest多種調(diào)用方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Python使用urllib模塊的urlopen超時(shí)問題解決方法
這篇文章主要介紹了Python使用urllib模塊的urlopen超時(shí)問題解決方法,本文使用socket模塊中的setdefaulttimeout函數(shù)解決了超時(shí)問題,需要的朋友可以參考下2014-11-11
python腳本框架webpy模板控制結(jié)構(gòu)
這篇文章主要為大家介紹了python腳本框架webpy模板控制結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練實(shí)例
在本篇文章里小編給大家整理的是關(guān)于Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練相關(guān)知識(shí)點(diǎn),有需要的朋友們參考下。2019-09-09
Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作詳解
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之消息提示與異常處理操作,結(jié)合實(shí)例形式分析了flask框架表單登陸消息提示、錯(cuò)誤模板調(diào)用及異常處理相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
使用django的ORM框架按月統(tǒng)計(jì)近一年內(nèi)的數(shù)據(jù)方法
今天小編就為大家分享一篇使用django的ORM框架按月統(tǒng)計(jì)近一年內(nèi)的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07

