验证工具代码:

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要求输入为方形,不然会挤压图片造成变形,因此需要对此做些处理.

 
  1. import os,h5py,cv2,sys,shutil

  2. import numpy as np

  3. from xml.dom.minidom import Document

  4. rootdir="../"

  5. convet2yoloformat=True

  6. convert2vocformat=True

  7. resized_dim=(48, 48)

  8. #最小取20大小的脸,并且补齐

  9. minsize2select=20

  10. usepadding=True

  11. datasetprefix="/home/yanhe/data/widerface"#

  12. def gen_hdf5():

  13. imgdir=rootdir+"/WIDER_train/images"

  14. gtfilepath=rootdir+"/wider_face_split/wider_face_train_bbx_gt.txt"

  15. index =0

  16. with open(gtfilepath,'r') as gtfile:

  17. faces=[]

  18. labels=[]

  19. while(True ):#and len(faces)<10

  20. imgpath=gtfile.readline()[:-1]

  21. if(imgpath==""):

  22. break;

  23. print index,imgpath

  24. img=cv2.imread(imgdir+"/"+imgpath)

  25. numbbox=int(gtfile.readline())

  26. bbox=[]

  27. for i in range(numbbox):

  28. line=gtfile.readline()

  29. line=line.split()

  30. line=line[0:4]

  31. if(int(line[3])<=0 or int(line[2])<=0):

  32. continue

  33. bbox=(int(line[0]),int(line[1]),int(line[2]),int(line[3]))

  34. face=img[int(line[1]):int(line[1])+int(line[3]),int(line[0]):int(line[0])+int(line[2])]

  35. face=cv2.resize(face, resized_dim)

  36. faces.append(face)

  37. labels.append(1)

  38. cv2.rectangle(img,(int(line[0]),int(line[1])),(int(line[0])+int(line[2]),int(line[1])+int(line[3])),(255,0,0))

  39. #cv2.imshow("img",img)

  40. #cv2.waitKey(1)

  41. index=index+1

  42. faces=np.asarray(faces)

  43. labels=np.asarray(labels)

  44. f=h5py.File('train.h5','w')

  45. f['data']=faces.astype(np.float32)

  46. f['label']=labels.astype(np.float32)

  47. f.close()

  48. def viewginhdf5():

  49. f = h5py.File('train.h5','r')

  50. f.keys()

  51. faces=f['data'][:]

  52. for face in faces:

  53. face=face.astype(np.uint8)

  54. cv2.imshow("img",face)

  55. cv2.waitKey(1)

  56. f.close()

  57. def convertimgset(img_set="train"):

  58. imgdir=rootdir+"/WIDER_"+img_set+"/images"

  59. gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"

  60. imagesdir=rootdir+"/images"

  61. vocannotationdir=rootdir+"/Annotations"

  62. labelsdir=rootdir+"/labels"

  63. if not os.path.exists(imagesdir):

  64. os.mkdir(imagesdir)

  65. if convet2yoloformat:

  66. if not os.path.exists(labelsdir):

  67. os.mkdir(labelsdir)

  68. if convert2vocformat:

  69. if not os.path.exists(vocannotationdir):

  70. os.mkdir(vocannotationdir)

  71. index=0

  72. with open(gtfilepath,'r') as gtfile:

  73. while(True ):#and len(faces)<10

  74. filename=gtfile.readline()[:-1]

  75. if(filename==""):

  76. break;

  77. sys.stdout.write("\r"+str(index)+":"+filename+"\t\t\t")

  78. sys.stdout.flush()

  79. imgpath=imgdir+"/"+filename

  80. img=cv2.imread(imgpath)

  81. if not img.data:

  82. break;

  83. imgheight=img.shape[0]

  84. imgwidth=img.shape[1]

  85. maxl=max(imgheight,imgwidth)

  86. paddingleft=(maxl-imgwidth)>>1

  87. paddingright=(maxl-imgwidth)>>1

  88. paddingbottom=(maxl-imgheight)>>1

  89. paddingtop=(maxl-imgheight)>>1

  90. saveimg=cv2.copyMakeBorder(img,paddingtop,paddingbottom,paddingleft,paddingright,cv2.BORDER_CONSTANT,value=0)

  91. showimg=saveimg.copy()

  92. numbbox=int(gtfile.readline())

  93. bboxes=[]

  94. for i in range(numbbox):

  95. line=gtfile.readline()

  96. line=line.split()

  97. line=line[0:4]

  98. if(int(line[3])<=0 or int(line[2])<=0):

  99. continue

  100. x=int(line[0])+paddingleft

  101. y=int(line[1])+paddingtop

  102. width=int(line[2])

  103. height=int(line[3])

  104. bbox=(x,y,width,height)

  105. x2=x+width

  106. y2=y+height

  107. #face=img[x:x2,y:y2]

  108. if width>=minsize2select and height>=minsize2select:

  109. bboxes.append(bbox)

  110. cv2.rectangle(showimg,(x,y),(x2,y2),(0,255,0))

  111. #maxl=max(width,height)

  112. #x3=(int)(x+(width-maxl)*0.5)

  113. #y3=(int)(y+(height-maxl)*0.5)

  114. #x4=(int)(x3+maxl)

  115. #y4=(int)(y3+maxl)

  116. #cv2.rectangle(img,(x3,y3),(x4,y4),(255,0,0))

  117. else:

  118. cv2.rectangle(showimg,(x,y),(x2,y2),(0,0,255))

  119. filename=filename.replace("/","_")

  120. if len(bboxes)==0:

  121. print "warrning: no face"

  122. continue

  123. cv2.imwrite(imagesdir+"/"+filename,saveimg)

  124. if convet2yoloformat:

  125. height=saveimg.shape[0]

  126. width=saveimg.shape[1]

  127. txtpath=labelsdir+"/"+filename

  128. txtpath=txtpath[:-3]+"txt"

  129. ftxt=open(txtpath,'w')

  130. for i in range(len(bboxes)):

  131. bbox=bboxes[i]

  132. xcenter=(bbox[0]+bbox[2]*0.5)/width

  133. ycenter=(bbox[1]+bbox[3]*0.5)/height

  134. wr=bbox[2]*1.0/width

  135. hr=bbox[3]*1.0/height

  136. txtline="0 "+str(xcenter)+" "+str(ycenter)+" "+str(wr)+" "+str(hr)+"\n"

  137. ftxt.write(txtline)

  138. ftxt.close()

  139. if convert2vocformat:

  140. xmlpath=vocannotationdir+"/"+filename

  141. xmlpath=xmlpath[:-3]+"xml"

  142. doc = Document()

  143. annotation = doc.createElement('annotation')

  144. doc.appendChild(annotation)

  145. folder = doc.createElement('folder')

  146. folder_name = doc.createTextNode('widerface')

  147. folder.appendChild(folder_name)

  148. annotation.appendChild(folder)

  149. filenamenode = doc.createElement('filename')

  150. filename_name = doc.createTextNode(filename)

  151. filenamenode.appendChild(filename_name)

  152. annotation.appendChild(filenamenode)

  153. source = doc.createElement('source')

  154. annotation.appendChild(source)

  155. database = doc.createElement('database')

  156. database.appendChild(doc.createTextNode('wider face Database'))

  157. source.appendChild(database)

  158. annotation_s = doc.createElement('annotation')

  159. annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007'))

  160. source.appendChild(annotation_s)

  161. image = doc.createElement('image')

  162. image.appendChild(doc.createTextNode('flickr'))

  163. source.appendChild(image)

  164. flickrid = doc.createElement('flickrid')

  165. flickrid.appendChild(doc.createTextNode('-1'))

  166. source.appendChild(flickrid)

  167. owner = doc.createElement('owner')

  168. annotation.appendChild(owner)

  169. flickrid_o = doc.createElement('flickrid')

  170. flickrid_o.appendChild(doc.createTextNode('yanyu'))

  171. owner.appendChild(flickrid_o)

  172. name_o = doc.createElement('name')

  173. name_o.appendChild(doc.createTextNode('yanyu'))

  174. owner.appendChild(name_o)

  175. size = doc.createElement('size')

  176. annotation.appendChild(size)

  177. width = doc.createElement('width')

  178. width.appendChild(doc.createTextNode(str(saveimg.shape[1])))

  179. height = doc.createElement('height')

  180. height.appendChild(doc.createTextNode(str(saveimg.shape[0])))

  181. depth = doc.createElement('depth')

  182. depth.appendChild(doc.createTextNode(str(saveimg.shape[2])))

  183. size.appendChild(width)

  184. size.appendChild(height)

  185. size.appendChild(depth)

  186. segmented = doc.createElement('segmented')

  187. segmented.appendChild(doc.createTextNode('0'))

  188. annotation.appendChild(segmented)

  189. for i in range(len(bboxes)):

  190. bbox=bboxes[i]

  191. objects = doc.createElement('object')

  192. annotation.appendChild(objects)

  193. object_name = doc.createElement('name')

  194. object_name.appendChild(doc.createTextNode('face'))

  195. objects.appendChild(object_name)

  196. pose = doc.createElement('pose')

  197. pose.appendChild(doc.createTextNode('Unspecified'))

  198. objects.appendChild(pose)

  199. truncated = doc.createElement('truncated')

  200. truncated.appendChild(doc.createTextNode('1'))

  201. objects.appendChild(truncated)

  202. difficult = doc.createElement('difficult')

  203. difficult.appendChild(doc.createTextNode('0'))

  204. objects.appendChild(difficult)

  205. bndbox = doc.createElement('bndbox')

  206. objects.appendChild(bndbox)

  207. xmin = doc.createElement('xmin')

  208. xmin.appendChild(doc.createTextNode(str(bbox[0])))

  209. bndbox.appendChild(xmin)

  210. ymin = doc.createElement('ymin')

  211. ymin.appendChild(doc.createTextNode(str(bbox[1])))

  212. bndbox.appendChild(ymin)

  213. xmax = doc.createElement('xmax')

  214. xmax.appendChild(doc.createTextNode(str(bbox[0]+bbox[2])))

  215. bndbox.appendChild(xmax)

  216. ymax = doc.createElement('ymax')

  217. ymax.appendChild(doc.createTextNode(str(bbox[1]+bbox[3])))

  218. bndbox.appendChild(ymax)

  219. f=open(xmlpath,"w")

  220. f.write(doc.toprettyxml(indent = ''))

  221. f.close()

  222. #cv2.imshow("img",showimg)

  223. #cv2.waitKey()

  224. index=index+1

  225. def generatetxt(img_set="train"):

  226. gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"

  227. f=open(rootdir+"/"+img_set+".txt","w")

  228. with open(gtfilepath,'r') as gtfile:

  229. while(True ):#and len(faces)<10

  230. filename=gtfile.readline()[:-1]

  231. if(filename==""):

  232. break;

  233. filename=filename.replace("/","_")

  234. imgfilepath=datasetprefix+"/images/"+filename

  235. f.write(imgfilepath+'\n')

  236. numbbox=int(gtfile.readline())

  237. for i in range(numbbox):

  238. line=gtfile.readline()

  239. f.close()

  240. def generatevocsets(img_set="train"):

  241. if not os.path.exists(rootdir+"/ImageSets"):

  242. os.mkdir(rootdir+"/ImageSets")

  243. if not os.path.exists(rootdir+"/ImageSets/Main"):

  244. os.mkdir(rootdir+"/ImageSets/Main")

  245. gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"

  246. f=open(rootdir+"/ImageSets/Main/"+img_set+".txt",'w')

  247. with open(gtfilepath,'r') as gtfile:

  248. while(True ):#and len(faces)<10

  249. filename=gtfile.readline()[:-1]

  250. if(filename==""):

  251. break;

  252. filename=filename.replace("/","_")

  253. imgfilepath=filename[:-4]

  254. f.write(imgfilepath+'\n')

  255. numbbox=int(gtfile.readline())

  256. for i in range(numbbox):

  257. line=gtfile.readline()

  258. f.close()

  259. def convertdataset():

  260. img_sets=["train","val"]

  261. for img_set in img_sets:

  262. convertimgset(img_set)

  263. generatetxt(img_set)

  264. generatevocsets(img_set)

  265. if __name__=="__main__":

  266. convertdataset()

  267. shutil.move(rootdir+"/"+"train.txt",rootdir+"/"+"trainval.txt")

  268. shutil.move(rootdir+"/"+"val.txt",rootdir+"/"+"test.txt")

  269. shutil.move(rootdir+"/ImageSets/Main/"+"train.txt",rootdir+"/ImageSets/Main/"+"trainval.txt")

  270. shutil.move(rootdir+"/ImageSets/Main/"+"val.txt",rootdir+"/ImageSets/Main/"+"test.txt")

如果没有时间自己转换,也可以下载已经转换好的文件,百度网盘,密码:xsdt

将widerface标注转换为VOC格式相关推荐

  1. 将fddb标注转换为VOC格式标注

    fddb是用来评估人脸检测性能事实上的标准,它给出了10折验证椭圆格式的标注,这和faster-rcnn以及SSD等的要求是不一样的,因此需要转换为VOC格式. 难点在于如何得到椭圆的外接矩形,我采用 ...

  2. 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 ...

  3. 将Yolo格式标注文件转换为VOC格式

    这篇文章主要参考博客Yolo标准数据集格式转Voc数据集中的代码,对原博客代码进行一定修改.添加注释,此外还在后面添加了我自己写的一段关于对转换后的标注文件进行整理的脚本代码. Yolo标注的格式与V ...

  4. yolo 标注转VOC格式(标注转换器)

    参考文章1:yolo标注转voc 参考文章2:YOLOV3 将自己的txt转为XML,再将XML转为符合YOLO要求的txt格式 参考文章3:yolov3 制作voc数据格式:xml转换成txt 参考 ...

  5. 使用自己训练的yolov3或yolov4模型自动标注成voc格式数据

    yolo数据格式转voc格式: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time :2021/8/31 下午3:06 # @Author : ...

  6. 将车辆数据集kitti转换为VOC格式(车辆检测)

    Kitti数据集的下载只需要一个图片集(12G)和标注文件即可(data_object_image_2.zip.data_object_label_2.zip). 下载地址:http://datase ...

  7. 将数据集转换为VOC格式

    xml文件操作 方式一 import sysimport timeimport stringfrom lxml import etree#设置默认字符集为UTF8 不然有些时候转码会出问题defaul ...

  8. Visdrone2019数据集.txt标签文件转换为voc格式.XML标签文件

    最近有同学问是否有Visdrone数据集的xml文件,由于本人之前训练数据的时候没有保存xml文件,所以无法共享. 为了解决这个问题,重新写了转换代码并贴出,供大家共同学习使用.(文末附上数据下载网盘 ...

  9. IndexError: list index out of range coco数据集转换为voc格式出现的错误

    问题: 出现这个问题的地方: 解决办法: 成功!

最新文章

  1. AI一分钟 | AI机器人竟混入大学哲学课堂并顺利结业,居然无人察觉!
  2. linear,swizzle,tile
  3. css3 动画 火箭,CSS3 火箭发射动画 寓意创新起航
  4. 从一个小demo开始,体验“API经济”的大魅力
  5. More than one file was found with OS independent path 'lib/arm64-v8a/libsqlite.so'
  6. jQuery函数的等价原生函数代码示例
  7. Django 入门项目案例开发(中)
  8. html 文字如何和阴影齐平,求助!Html Div齐平无效
  9. flask mysql环境配置_Flask教程4:数据库
  10. 记录.NET Core部署到Linux之.NET Core环境搭建(1)
  11. 【Oracle】Oracle错误编码大全
  12. 别催更啦!手淘全链路性能优化下篇--容器极速之路
  13. Flutter笔记(9)flutter中baseline基准线布局
  14. 注册时出现服务器错误,创建Apple ID时出现服务器错误,导致无法完成注册是什么原因...
  15. Android 新浪微博 授权失败 21337
  16. 猫和老鼠服务器维护多久结束,猫和老鼠手游:长时间不玩游戏,再次进入游戏后会发生这些事...
  17. qt程序报错error C2248: “ThreadTest::ThreadTest”: 无法访问 private 成员(在“ThreadTest”类中声明)
  18. sco unix和linux区别,SCOUNIX到Linux操作系统的程序移植问题有哪些呢?
  19. 折半插入排序的最强版
  20. 对象存储场景化开发实践-马毅-专题视频课程

热门文章

  1. post postman 传值_postman参数传递
  2. android 反调试 方案,Android Native反调试—检测TracerPid值
  3. 三次样条插值三弯矩matlab_三次样条(cubic spline)插值
  4. 计算机利用公式计算实发工资怎么弄,2019新个税Excel计算器公式 助你轻松算出工资...
  5. 新型消防机器人作文_消防机器人
  6. html自适应pc窗口大小_自适应技术很难吗?为什么Shopyy平台将网站分为PC端和移动端...
  7. 注册与验证码php源代码,PHP验证码处理源代码
  8. matlab中repmat的用法,Matlab: sum的用法、每一行求和、repmat的用法、sum和repmat结合使用减少循环...
  9. 交换机端口mtu值最大_大中型监控系统如何正确选择交换机
  10. IDEA Tips:Debug跳转任意行