python实现基本需求,可以在此基础上修改
import h5py
from skimage import io
import shutil
import random
headstr = """\
<annotation>
<folder>VOC2007</folder>
<filename>%06d.jpg</filename>
<source>
<database>My Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>facevise</name>
</owner>
<size>
<width>%d</width>
<height>%d</height>
<depth>%d</depth>
</size>
<segmented>0</segmented>
"""
objstr = """\
<object>
<name>%s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%d</xmin>
<ymin>%d</ymin>
<xmax>%d</xmax>
<ymax>%d</ymax>
</bndbox>
</object>
"""
tailstr ='''\
</annotation>
'''
def writexml(idx, head, objs, tail):
filename = "Annotations/%06d.xml" % (idx)
f = open(filename, "w")
f.write(head)
f.write(objs)
f.write(tail)
f.close()
def clear_dir():
if shutil.os.path.exists('Annotations'):
shutil.rmtree('Annotations')
if shutil.os.path.exists('ImageSets'):
shutil.rmtree('ImageSets')
if shutil.os.path.exists('JPEGImages'):
shutil.rmtree('JPEGImages')
shutil.os.mkdir('Annotations')
shutil.os.makedirs('ImageSets/Main')
shutil.os.mkdir('JPEGImages')
def excute_datasets(idx, datatype):
f = open('ImageSets/Main/'+datatype+'.txt', 'a')
mat = h5py.File('wider_face_split/wider_face_'+datatype+'.mat', 'r')
file_list = mat['file_list'][:]
event_list = mat['event_list'][:]
bbx_list = mat['face_bbx_list'][:]
for i in range(file_list.size):
file_list_sub = mat[file_list[0,i]][:]
bbx_list_sub = mat[bbx_list[0, i]][:]
event_value = ''.join(chr(x) for x in mat[event_list[0,i]][:])
for j in range(file_list_sub.size):
root = 'WIDER_'+datatype+'/images/'+event_value+'/'
filename = root + ''.join([chr(x) for x in mat[file_list_sub[0, j]][:]])+'.jpg'
im = io.imread(filename)
head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])
bboxes = mat[bbx_list_sub[0, j]][:]
objs = ''.join([objstr % ('face', \
bboxes[0,k],bboxes[1,k], bboxes[0,k]+bboxes[2,k]-1,bboxes[1,k]+bboxes[3,k]-1) \
for k in range(bboxes.shape[1])])
writexml(idx, head, objs, tailstr)
shutil.copyfile(filename, 'JPEGImages/%06d.jpg' % (idx))
f.write('%06d\n' % (idx))
idx +=1
f.close()
return idx
#打乱样本
def shuffle_file(filename):
f = open(filename, 'r+')
lines = f.readlines()
random.shuffle(lines)
f.seek(0)
f.truncate()
f.writelines(lines)
f.close()
if __name__ == '__main__':
clear_dir()
idx = 1
idx = excute_datasets(idx, 'train')
idx = excute_datasets(idx, 'val')
from skimage import io
import shutil
import random
headstr = """\
<annotation>
<folder>VOC2007</folder>
<filename>%06d.jpg</filename>
<source>
<database>My Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>facevise</name>
</owner>
<size>
<width>%d</width>
<height>%d</height>
<depth>%d</depth>
</size>
<segmented>0</segmented>
"""
objstr = """\
<object>
<name>%s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%d</xmin>
<ymin>%d</ymin>
<xmax>%d</xmax>
<ymax>%d</ymax>
</bndbox>
</object>
"""
tailstr ='''\
</annotation>
'''
def writexml(idx, head, objs, tail):
filename = "Annotations/%06d.xml" % (idx)
f = open(filename, "w")
f.write(head)
f.write(objs)
f.write(tail)
f.close()
def clear_dir():
if shutil.os.path.exists('Annotations'):
shutil.rmtree('Annotations')
if shutil.os.path.exists('ImageSets'):
shutil.rmtree('ImageSets')
if shutil.os.path.exists('JPEGImages'):
shutil.rmtree('JPEGImages')
shutil.os.mkdir('Annotations')
shutil.os.makedirs('ImageSets/Main')
shutil.os.mkdir('JPEGImages')
def excute_datasets(idx, datatype):
f = open('ImageSets/Main/'+datatype+'.txt', 'a')
mat = h5py.File('wider_face_split/wider_face_'+datatype+'.mat', 'r')
file_list = mat['file_list'][:]
event_list = mat['event_list'][:]
bbx_list = mat['face_bbx_list'][:]
for i in range(file_list.size):
file_list_sub = mat[file_list[0,i]][:]
bbx_list_sub = mat[bbx_list[0, i]][:]
event_value = ''.join(chr(x) for x in mat[event_list[0,i]][:])
for j in range(file_list_sub.size):
root = 'WIDER_'+datatype+'/images/'+event_value+'/'
filename = root + ''.join([chr(x) for x in mat[file_list_sub[0, j]][:]])+'.jpg'
im = io.imread(filename)
head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])
bboxes = mat[bbx_list_sub[0, j]][:]
objs = ''.join([objstr % ('face', \
bboxes[0,k],bboxes[1,k], bboxes[0,k]+bboxes[2,k]-1,bboxes[1,k]+bboxes[3,k]-1) \
for k in range(bboxes.shape[1])])
writexml(idx, head, objs, tailstr)
shutil.copyfile(filename, 'JPEGImages/%06d.jpg' % (idx))
f.write('%06d\n' % (idx))
idx +=1
f.close()
return idx
#打乱样本
def shuffle_file(filename):
f = open(filename, 'r+')
lines = f.readlines()
random.shuffle(lines)
f.seek(0)
f.truncate()
f.writelines(lines)
f.close()
if __name__ == '__main__':
clear_dir()
idx = 1
idx = excute_datasets(idx, 'train')
idx = excute_datasets(idx, 'val')