Python 生成VOC格式的標(biāo)簽實(shí)例
常用目標(biāo)檢測模型基本都是讀取的PASCAL VOC格式的標(biāo)簽,下面代碼用于生成VOC格式的代碼,根據(jù)需要修改即可:
from lxml import etree, objectifydef 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ǔ)充知識: python對PASCAL VOC標(biāo)注數(shù)據(jù)進(jìn)行統(tǒng)計(jì)
用于統(tǒng)計(jì)訓(xùn)練數(shù)據(jù)中的類別,以及所有目標(biāo)的個(gè)數(shù):
# coding:utf-8import xml.etree.cElementTree as ETimport osfrom collections import Counterimport 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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. el-input無法輸入的問題和表單驗(yàn)證失敗問題解決2. vue跳轉(zhuǎn)頁面常用的幾種方法匯總3. XML入門的常見問題(三)4. JavaScript中顏色模型的基礎(chǔ)知識與應(yīng)用詳解5. 不要在HTML中濫用div6. JavaScript快速實(shí)現(xiàn)一個(gè)顏色選擇器7. react腳手架配置代理的實(shí)現(xiàn)8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. Jquery使用原生AJAX方法請求數(shù)據(jù)10. React實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)hook組件實(shí)戰(zhàn)示例
