溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python如何生成VOC格式

發布時間:2021-03-18 14:40:35 來源:億速云 閱讀:273 作者:小新 欄目:開發技術

這篇文章主要介紹Python如何生成VOC格式,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

常用目標檢測模型基本都是讀取的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)

以上是“Python如何生成VOC格式”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女