Step.1 Dat2VOC
需要首先建立好VOC2007文件夹目录结构
import os
import xml.etree.ElementTree as ET
from PIL import Image
import numpy as np
import cv2
'''
VOC2007数据目录结构
root/
| - VOCdevkit/VOC2007
| |- Annotations/ *.xml 标注
| |- JPEGImages/ *.jpg 图片
| |- ImageSets/Main/ *.txt num
| |- voc2name.py 把数据分为 train/val/test
'''
# ------------- @xhh define -----------------------------
_config = {
'txtPath' : 'data/train',
'vocXMLPath' : 'VOCdevkit/VOC2007/Annotations',
'vocJPGPath' : 'VOCdevkit/VOC2007/JPEGImages',
'labels' : ['OKKK'],
'datIsClip' : False,
'datSize' : [64, 1024],
'toSize' : [416, 416],
'depth' : 3
}
# dst = src * _rate
x_rate = _config['toSize'][1] / _config['datSize'][1]
y_rate = _config['toSize'][0] / _config['datSize'][0]
# ------------- @xhh define -----------------------------
def write_xml(imgname, filepath, labeldicts): #参数imagename是图片名(无后缀)
root = ET.Element('Annotation') #创建Annotation根节点
ET.SubElement(root, 'filename').text = str(imgname) #创建filename子节点(无后缀)
sizes = ET.SubElement(root,'size') #创建size子节点
ET.SubElement(sizes, 'width').text = str(_config['toSize'][1])
ET.SubElement(sizes, 'height').text = str(_config['toSize'][0])
ET.SubElement(sizes, 'depth').text = str(_config['depth'] )
for labeldict in labeldicts:
objects = ET.SubElement(root, 'object') #创建object子节点
ET.SubElement(objects, 'name').text = labeldict['name']
#的类别名
ET.SubElement(objects, 'pose').text = 'Unspecified'
ET.SubElement(objects, 'truncated').text = '0'
ET.SubElement(objects, 'difficult').text = '0'
bndbox = ET.SubElement(objects,'bndbox')
ET.SubElement(bndbox, 'xmin').text = str(int(labeldict['xmin']))
ET.SubElement(bndbox, 'ymin').text = str(int(labeldict['ymin']))
ET.SubElement(bndbox, 'xmax').text = str(int(labeldict['xmax']))
ET.SubElement(bndbox, 'ymax').text = str(int(labeldict['ymax']))
tree = ET.ElementTree(root)
tree.write(filepath, encoding='utf-8', xml_declaration=False)
def dat2VOC():
# label: 'OKKK'
for label in _config['labels']:
txtList = os.listdir(_config['txtPath']) # ['1.dat', '1.txt', ...]
txtList.sort(reverse=False)
for item in txtList:
# txt
if item[-3:] == 'txt':
img_id = int(item.split