将widerface标注转换为VOC格式
验证工具代码:
https://github.com/wondervictor/WiderFace-Evaluation
将widerface标注转换为VOC格式
原文:https://blog.csdn.net/minstyrain/article/details/77986262
widerface是包含了3万多张总计近40万张人脸的人脸检测库,里面包含了大大小小各式各样的人脸,是不可多得的素材。
请将下面的代码保存至widerface.py,并至于下图所示的eval_tools文件夹下,其他的文件结构一并如图所示。
Update:
由于widerface里包含很多小脸,用SSD训练不一定能收敛,此外SSD要求输入为方形,不然会挤压图片造成变形,因此需要对此做些处理.
import os,h5py,cv2,sys,shutil
import numpy as np
from xml.dom.minidom import Document
rootdir="../"
convet2yoloformat=True
convert2vocformat=True
resized_dim=(48, 48)
#最小取20大小的脸,并且补齐
minsize2select=20
usepadding=True
datasetprefix="/home/yanhe/data/widerface"#
def gen_hdf5():
imgdir=rootdir+"/WIDER_train/images"
gtfilepath=rootdir+"/wider_face_split/wider_face_train_bbx_gt.txt"
index =0
with open(gtfilepath,'r') as gtfile:
faces=[]
labels=[]
while(True ):#and len(faces)<10
imgpath=gtfile.readline()[:-1]
if(imgpath==""):
break;
print index,imgpath
img=cv2.imread(imgdir+"/"+imgpath)
numbbox=int(gtfile.readline())
bbox=[]
for i in range(numbbox):
line=gtfile.readline()
line=line.split()
line=line[0:4]
if(int(line[3])<=0 or int(line[2])<=0):
continue
bbox=(int(line[0]),int(line[1]),int(line[2]),int(line[3]))
face=img[int(line[1]):int(line[1])+int(line[3]),int(line[0]):int(line[0])+int(line[2])]
face=cv2.resize(face, resized_dim)
faces.append(face)
labels.append(1)
cv2.rectangle(img,(int(line[0]),int(line[1])),(int(line[0])+int(line[2]),int(line[1])+int(line[3])),(255,0,0))
#cv2.imshow("img",img)
#cv2.waitKey(1)
index=index+1
faces=np.asarray(faces)
labels=np.asarray(labels)
f=h5py.File('train.h5','w')
f['data']=faces.astype(np.float32)
f['label']=labels.astype(np.float32)
f.close()
def viewginhdf5():
f = h5py.File('train.h5','r')
f.keys()
faces=f['data'][:]
for face in faces:
face=face.astype(np.uint8)
cv2.imshow("img",face)
cv2.waitKey(1)
f.close()
def convertimgset(img_set="train"):
imgdir=rootdir+"/WIDER_"+img_set+"/images"
gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"
imagesdir=rootdir+"/images"
vocannotationdir=rootdir+"/Annotations"
labelsdir=rootdir+"/labels"
if not os.path.exists(imagesdir):
os.mkdir(imagesdir)
if convet2yoloformat:
if not os.path.exists(labelsdir):
os.mkdir(labelsdir)
if convert2vocformat:
if not os.path.exists(vocannotationdir):
os.mkdir(vocannotationdir)
index=0
with open(gtfilepath,'r') as gtfile:
while(True ):#and len(faces)<10
filename=gtfile.readline()[:-1]
if(filename==""):
break;
sys.stdout.write("\r"+str(index)+":"+filename+"\t\t\t")
sys.stdout.flush()
imgpath=imgdir+"/"+filename
img=cv2.imread(imgpath)
if not img.data:
break;
imgheight=img.shape[0]
imgwidth=img.shape[1]
maxl=max(imgheight,imgwidth)
paddingleft=(maxl-imgwidth)>>1
paddingright=(maxl-imgwidth)>>1
paddingbottom=(maxl-imgheight)>>1
paddingtop=(maxl-imgheight)>>1
saveimg=cv2.copyMakeBorder(img,paddingtop,paddingbottom,paddingleft,paddingright,cv2.BORDER_CONSTANT,value=0)
showimg=saveimg.copy()
numbbox=int(gtfile.readline())
bboxes=[]
for i in range(numbbox):
line=gtfile.readline()
line=line.split()
line=line[0:4]
if(int(line[3])<=0 or int(line[2])<=0):
continue
x=int(line[0])+paddingleft
y=int(line[1])+paddingtop
width=int(line[2])
height=int(line[3])
bbox=(x,y,width,height)
x2=x+width
y2=y+height
#face=img[x:x2,y:y2]
if width>=minsize2select and height>=minsize2select:
bboxes.append(bbox)
cv2.rectangle(showimg,(x,y),(x2,y2),(0,255,0))
#maxl=max(width,height)
#x3=(int)(x+(width-maxl)*0.5)
#y3=(int)(y+(height-maxl)*0.5)
#x4=(int)(x3+maxl)
#y4=(int)(y3+maxl)
#cv2.rectangle(img,(x3,y3),(x4,y4),(255,0,0))
else:
cv2.rectangle(showimg,(x,y),(x2,y2),(0,0,255))
filename=filename.replace("/","_")
if len(bboxes)==0:
print "warrning: no face"
continue
cv2.imwrite(imagesdir+"/"+filename,saveimg)
if convet2yoloformat:
height=saveimg.shape[0]
width=saveimg.shape[1]
txtpath=labelsdir+"/"+filename
txtpath=txtpath[:-3]+"txt"
ftxt=open(txtpath,'w')
for i in range(len(bboxes)):
bbox=bboxes[i]
xcenter=(bbox[0]+bbox[2]*0.5)/width
ycenter=(bbox[1]+bbox[3]*0.5)/height
wr=bbox[2]*1.0/width
hr=bbox[3]*1.0/height
txtline="0 "+str(xcenter)+" "+str(ycenter)+" "+str(wr)+" "+str(hr)+"\n"
ftxt.write(txtline)
ftxt.close()
if convert2vocformat:
xmlpath=vocannotationdir+"/"+filename
xmlpath=xmlpath[:-3]+"xml"
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder_name = doc.createTextNode('widerface')
folder.appendChild(folder_name)
annotation.appendChild(folder)
filenamenode = doc.createElement('filename')
filename_name = doc.createTextNode(filename)
filenamenode.appendChild(filename_name)
annotation.appendChild(filenamenode)
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
database.appendChild(doc.createTextNode('wider face Database'))
source.appendChild(database)
annotation_s = doc.createElement('annotation')
annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007'))
source.appendChild(annotation_s)
image = doc.createElement('image')
image.appendChild(doc.createTextNode('flickr'))
source.appendChild(image)
flickrid = doc.createElement('flickrid')
flickrid.appendChild(doc.createTextNode('-1'))
source.appendChild(flickrid)
owner = doc.createElement('owner')
annotation.appendChild(owner)
flickrid_o = doc.createElement('flickrid')
flickrid_o.appendChild(doc.createTextNode('yanyu'))
owner.appendChild(flickrid_o)
name_o = doc.createElement('name')
name_o.appendChild(doc.createTextNode('yanyu'))
owner.appendChild(name_o)
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
width.appendChild(doc.createTextNode(str(saveimg.shape[1])))
height = doc.createElement('height')
height.appendChild(doc.createTextNode(str(saveimg.shape[0])))
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode(str(saveimg.shape[2])))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode('0'))
annotation.appendChild(segmented)
for i in range(len(bboxes)):
bbox=bboxes[i]
objects = doc.createElement('object')
annotation.appendChild(objects)
object_name = doc.createElement('name')
object_name.appendChild(doc.createTextNode('face'))
objects.appendChild(object_name)
pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode('Unspecified'))
objects.appendChild(pose)
truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode('1'))
objects.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode('0'))
objects.appendChild(difficult)
bndbox = doc.createElement('bndbox')
objects.appendChild(bndbox)
xmin = doc.createElement('xmin')
xmin.appendChild(doc.createTextNode(str(bbox[0])))
bndbox.appendChild(xmin)
ymin = doc.createElement('ymin')
ymin.appendChild(doc.createTextNode(str(bbox[1])))
bndbox.appendChild(ymin)
xmax = doc.createElement('xmax')
xmax.appendChild(doc.createTextNode(str(bbox[0]+bbox[2])))
bndbox.appendChild(xmax)
ymax = doc.createElement('ymax')
ymax.appendChild(doc.createTextNode(str(bbox[1]+bbox[3])))
bndbox.appendChild(ymax)
f=open(xmlpath,"w")
f.write(doc.toprettyxml(indent = ''))
f.close()
#cv2.imshow("img",showimg)
#cv2.waitKey()
index=index+1
def generatetxt(img_set="train"):
gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"
f=open(rootdir+"/"+img_set+".txt","w")
with open(gtfilepath,'r') as gtfile:
while(True ):#and len(faces)<10
filename=gtfile.readline()[:-1]
if(filename==""):
break;
filename=filename.replace("/","_")
imgfilepath=datasetprefix+"/images/"+filename
f.write(imgfilepath+'\n')
numbbox=int(gtfile.readline())
for i in range(numbbox):
line=gtfile.readline()
f.close()
def generatevocsets(img_set="train"):
if not os.path.exists(rootdir+"/ImageSets"):
os.mkdir(rootdir+"/ImageSets")
if not os.path.exists(rootdir+"/ImageSets/Main"):
os.mkdir(rootdir+"/ImageSets/Main")
gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"
f=open(rootdir+"/ImageSets/Main/"+img_set+".txt",'w')
with open(gtfilepath,'r') as gtfile:
while(True ):#and len(faces)<10
filename=gtfile.readline()[:-1]
if(filename==""):
break;
filename=filename.replace("/","_")
imgfilepath=filename[:-4]
f.write(imgfilepath+'\n')
numbbox=int(gtfile.readline())
for i in range(numbbox):
line=gtfile.readline()
f.close()
def convertdataset():
img_sets=["train","val"]
for img_set in img_sets:
convertimgset(img_set)
generatetxt(img_set)
generatevocsets(img_set)
if __name__=="__main__":
convertdataset()
shutil.move(rootdir+"/"+"train.txt",rootdir+"/"+"trainval.txt")
shutil.move(rootdir+"/"+"val.txt",rootdir+"/"+"test.txt")
shutil.move(rootdir+"/ImageSets/Main/"+"train.txt",rootdir+"/ImageSets/Main/"+"trainval.txt")
shutil.move(rootdir+"/ImageSets/Main/"+"val.txt",rootdir+"/ImageSets/Main/"+"test.txt")
如果没有时间自己转换,也可以下载已经转换好的文件,百度网盘,密码:xsdt
将widerface标注转换为VOC格式相关推荐
- 将fddb标注转换为VOC格式标注
fddb是用来评估人脸检测性能事实上的标准,它给出了10折验证椭圆格式的标注,这和faster-rcnn以及SSD等的要求是不一样的,因此需要转换为VOC格式. 难点在于如何得到椭圆的外接矩形,我采用 ...
- labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换
目录 1 labelimg标注VOC格式和yolo格式介绍 1.1 voc格式 1.2 yolo数据格式介绍 2 voc格式数据和yolo格式数据相互转换 2.1 voc转yolo代码 2.2 yol ...
- 将Yolo格式标注文件转换为VOC格式
这篇文章主要参考博客Yolo标准数据集格式转Voc数据集中的代码,对原博客代码进行一定修改.添加注释,此外还在后面添加了我自己写的一段关于对转换后的标注文件进行整理的脚本代码. Yolo标注的格式与V ...
- yolo 标注转VOC格式(标注转换器)
参考文章1:yolo标注转voc 参考文章2:YOLOV3 将自己的txt转为XML,再将XML转为符合YOLO要求的txt格式 参考文章3:yolov3 制作voc数据格式:xml转换成txt 参考 ...
- 使用自己训练的yolov3或yolov4模型自动标注成voc格式数据
yolo数据格式转voc格式: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time :2021/8/31 下午3:06 # @Author : ...
- 将车辆数据集kitti转换为VOC格式(车辆检测)
Kitti数据集的下载只需要一个图片集(12G)和标注文件即可(data_object_image_2.zip.data_object_label_2.zip). 下载地址:http://datase ...
- 将数据集转换为VOC格式
xml文件操作 方式一 import sysimport timeimport stringfrom lxml import etree#设置默认字符集为UTF8 不然有些时候转码会出问题defaul ...
- Visdrone2019数据集.txt标签文件转换为voc格式.XML标签文件
最近有同学问是否有Visdrone数据集的xml文件,由于本人之前训练数据的时候没有保存xml文件,所以无法共享. 为了解决这个问题,重新写了转换代码并贴出,供大家共同学习使用.(文末附上数据下载网盘 ...
- IndexError: list index out of range coco数据集转换为voc格式出现的错误
问题: 出现这个问题的地方: 解决办法: 成功!
最新文章
- AI一分钟 | AI机器人竟混入大学哲学课堂并顺利结业,居然无人察觉!
- linear,swizzle,tile
- css3 动画 火箭,CSS3 火箭发射动画 寓意创新起航
- 从一个小demo开始,体验“API经济”的大魅力
- More than one file was found with OS independent path 'lib/arm64-v8a/libsqlite.so'
- jQuery函数的等价原生函数代码示例
- Django 入门项目案例开发(中)
- html 文字如何和阴影齐平,求助!Html Div齐平无效
- flask mysql环境配置_Flask教程4:数据库
- 记录.NET Core部署到Linux之.NET Core环境搭建(1)
- 【Oracle】Oracle错误编码大全
- 别催更啦!手淘全链路性能优化下篇--容器极速之路
- Flutter笔记(9)flutter中baseline基准线布局
- 注册时出现服务器错误,创建Apple ID时出现服务器错误,导致无法完成注册是什么原因...
- Android 新浪微博 授权失败 21337
- 猫和老鼠服务器维护多久结束,猫和老鼠手游:长时间不玩游戏,再次进入游戏后会发生这些事...
- qt程序报错error C2248: “ThreadTest::ThreadTest”: 无法访问 private 成员(在“ThreadTest”类中声明)
- sco unix和linux区别,SCOUNIX到Linux操作系统的程序移植问题有哪些呢?
- 折半插入排序的最强版
- 对象存储场景化开发实践-马毅-专题视频课程
热门文章
- post postman 传值_postman参数传递
- android 反调试 方案,Android Native反调试—检测TracerPid值
- 三次样条插值三弯矩matlab_三次样条(cubic spline)插值
- 计算机利用公式计算实发工资怎么弄,2019新个税Excel计算器公式 助你轻松算出工资...
- 新型消防机器人作文_消防机器人
- html自适应pc窗口大小_自适应技术很难吗?为什么Shopyy平台将网站分为PC端和移动端...
- 注册与验证码php源代码,PHP验证码处理源代码
- matlab中repmat的用法,Matlab: sum的用法、每一行求和、repmat的用法、sum和repmat结合使用减少循环...
- 交换机端口mtu值最大_大中型监控系统如何正确选择交换机
- IDEA Tips:Debug跳转任意行