import os, h5py, cv2, sys, shutil
import numpy as np
from xml.dom.minidom import Document rootdir = "G:/MTCNNTraining/faceData/widerFace"
convet2yoloformat = True
convert2vocformat = True
resized_dim = (48, 48) # 最小取1大小的脸,并且补齐
minsize2select = 1
usepadding = True datasetprefix = "G:/MTCNNTraining/faceData/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('widerFace'))
owner.appendChild(flickrid_o)
name_o = doc.createElement('name')
name_o.appendChild(doc.createTextNode('widerFace'))
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(''))
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(''))
objects.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode(''))
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")

widerface---VOC的更多相关文章

  1. YOLO3训练widerface数据集

    因为YOLO3速度精度都很棒,所以想训练一下人脸模型,废话不多,进入正题 1写所有的配置文件 1.1 YOLO3-face.cfg 个人感觉YOLO的配置文件骑士和caffe差不多 在cfg/YOLO ...

  2. Average Precision of VOC

    转一篇文章,主要是关于VOC中Average Precision指标的 原文出处:https://sanchom.wordpress.com/tag/average-precision/ 还有一篇文章 ...

  3. 搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型

    原文地址:搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型 0x00 环境 OS: Ubuntu 1810 x64 Anaconda: 4.6.12 P ...

  4. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)

    前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...

  5. VOC数据集生成代码使用说明

    #split.py 文件 输入格式为images ,和标签txt文件,txt中的数据为坐标值共8个. import os import numpy as np import math import c ...

  6. 在Ubuntu内制作自己的VOC数据集

    一.VOC数据集的简介 PASCAL VOC为图像的识别和分类提供了一整套标准化的优秀数据集,基本上就是目标检测数据集的模板.现在有VOC2007,VOC2012.主要有20个类.而现在主要的模型评估 ...

  7. Python生成PASCAL VOC格式的xml标注文件

    Python生成PASCAL VOC格式的xml标注文件 PASCAL VOC数据集的标注文件是xml格式的.对于py-faster-rcnn,通常以下示例的字段是合适的: <annotatio ...

  8. caffe生成voc格式lmdb

    要训练ssd基本都是在liu wei框架下改,生成lmdb这一关照葫芦画瓢总遇坑,记录之: 1. labelmap_voc.prototxt要根据自己的分类修改,比如人脸检测改成这样: item { ...

  9. PASCAL VOC数据集分析(转)

    PASCAL VOC数据集分析 PASCAL VOC为图像识别和分类提供了一整套标准化的优秀的数据集,从2005年到2012年每年都会举行一场图像识别challenge. 本文主要分析PASCAL V ...

  10. 用python将MSCOCO和Caltech行人检测数据集转化成VOC格式

    代码:转换用的代码放在这里 之前用Tensorflow提供的object detection API可以很方便的进行fine-tuning实现所需的特定物体检测模型(看这里).那么现在的主要问题就是数 ...

随机推荐

  1. python数据类型之元组类型

    #为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,2,3],'a',(1,2)) #t=tuple((1,[1,2,3],'a',(1,2))) # print(type(t ...

  2. 关于信号打断正在读取终端的read与select来监视0文件描述符的问题

    首先说一下对于这个问题外的一些话: 我觉得我们应该有种质疑的态度,因为接下来的这个问题就和我们平常所想的不一样. 介绍一下问题: 曾经一直听说信号可以打断一个正在阻塞的进程,但是今天我试了一下关于信号 ...

  3. 2018软件工程W班第一次助教小结

    我是数计学院实验教学中心的一名老师,机缘巧合之下,这个学期跟着汪老师上<软件工程实践>这门课.之前有陆续听说过<构建之法>这本书,记得好像学院还有主办过研讨会.对于这门实践课, ...

  4. log4j2教程【RollingFileAppender】

    说明 rollover 表示的是当日志文件大小满足指定大小后,就生成一个新的文件的过程. RollingFileAppender RollingFileAppender是一个OutputStreamA ...

  5. 手把手教你如何安装Pycharm

    手把手教你如何安装Pycharm——靠谱的Pycharm安装详细教程     今天小编给大家分享如何在本机上下载和安装Pycharm,具体的教程如下: 1.首先去Pycharm官网,或者直接输入网址: ...

  6. java爬取免费HTTP代理 code-for-fun

    偶然看到一个提供免费HTTP 代理IP的网站,该网站一两个小时就会更新一次,很有用.之后自己就用Java写了一个爬虫,爬取网站上的代理IP,以备后用. 网站源码: <!DOCTYPE html& ...

  7. web开发前端面试知识点目录整理

    web开发前端面试知识点目录整理 基本功考察 关于Html 1. html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 2. h5中新增的属性; 如自定义属性data, ...

  8. java串口通信丢包

    java串口通信丢包问题 前段时间公司要求做一个java应用和pos串口通信的工具,调试好了好久每次都是只能接收到一包数据后续的数据都丢失了. 经过修改读写的流的缓存大小亲测都正常代码如下: seri ...

  9. 原生js开发,无依赖、轻量级的现代浏览器图片懒加载插件,适合在移动端开发使用

    优势 1.原生js开发,不依赖任何框架或库 2.支持将各种宽高不一致的图片,自动剪切成默认图片的宽高 比如说你的默认图片是一张正方形的图片,则各种宽度高度不一样的图片,自动剪切成正方形. 完美解决移动 ...

  10. goldengate密码加密

    ----------------ogg加密GGSCI (ogghost) 10> encrypt password goldengate,ENCRYPTKEY defaultUsing defa ...