#coding=utf-8
import os
import cv2
from xml.dom.minidom import Document def create_xml(boxes_dict,target_xml_dir):
file_name = boxes_dict["filename"]
fname = file_name.split('.')[0]
boxes = boxes_dict["boxes"]
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation) folder = doc.createElement('folder')
folder.appendChild(doc.createTextNode('widerface'))
annotation.appendChild(folder) filename = doc.createElement('filename')
filename.appendChild(doc.createTextNode(file_name))
annotation.appendChild(filename) source = doc.createElement('source')
database = doc.createElement('database')
database.appendChild(doc.createTextNode('baidu'))
source.appendChild(database)
annotation.appendChild(source) size = doc.createElement('size')
width = doc.createElement('width')
width.appendChild(doc.createTextNode(str(300)))
size.appendChild(width)
height = doc.createElement('height')
height.appendChild(doc.createTextNode(str(300)))
size.appendChild(height)
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode(str(3)))
size.appendChild(depth)
annotation.appendChild(size) segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode(str(0)))
annotation.appendChild(segmented) # write the coordinates of the b-box
for b_box in boxes:
#print b_box
if(b_box[0]<0):
b_box[0] = 0
if (b_box[1] < 0):
b_box[1] = 0 object = doc.createElement('object')
name = doc.createElement('name')
name.appendChild(doc.createTextNode('face'))
#name.appendChild(doc.createTextNode(x[0]))
object.appendChild(name) difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode(''))
object.appendChild(difficult) truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode(''))
object.appendChild(truncated) pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode('undefined'))
object.appendChild(pose) bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
xmin.appendChild(doc.createTextNode(str(b_box[0])))
bndbox.appendChild(xmin)
object.appendChild(bndbox)
ymin = doc.createElement('ymin')
ymin.appendChild(doc.createTextNode(str(b_box[1])))
bndbox.appendChild(ymin)
xmax = doc.createElement('xmax')
xmax.appendChild(doc.createTextNode(str(b_box[0]+b_box[2])))
bndbox.appendChild(xmax)
ymax = doc.createElement('ymax')
ymax.appendChild(doc.createTextNode(str(b_box[1]+b_box[3])))
bndbox.appendChild(ymax)
annotation.appendChild(object) xml_name = fname+'.xml'
target_xml_path = os.path.join(target_xml_dir,xml_name)
with open(target_xml_path, 'wb') as f:
f.write(doc.toprettyxml(indent='\t', encoding='utf-8')) def draw_and_save(image_list,src_img_dir = None, tar_img_dir = None):
name_list = os.path.join(tar_img_dir,"val.txt")
with open(name_list,'a') as fw:
for item in image_list:
sub_path = item["path"]
path_seg = sub_path.split("/")
path = os.path.join(src_img_dir,sub_path)
boxes = item["boxes"]
img = cv2.imread(path)
height,width,channel = img.shape
box_num = 0
target_size = 300
boxes_dict = {}
boxes_dict["filename"] = path_seg[1]
new_boxes = []
for box in boxes:
new_box = []
ord = box.split(" ")
x, y, w, h = int(ord[0]),int(ord[1]),int(ord[2]),int(ord[3])
wh = width
if width > height:
wh = height
img = img[0:wh,0:wh]
if x+w > wh or y+h > wh : #过滤掉超出图片范围的人脸
print "Face has been out of picture"
continue scale = float(target_size)/wh #缩放比
x_new = int(x*scale)
y_new = int(y*scale)
w_new = int(w*scale)
h_new = int(h*scale) if w_new*h_new < 64: # 过滤面积小于64像素平方的框,因为第一个用于检测的特征图的stride=8
print "Box: (width: {} height: {}) is too small".format(w_new,h_new)
continue img = cv2.resize(img,(target_size,target_size)) #缩放到300×300
new_box.append(x_new)
new_box.append(y_new)
new_box.append(w_new)
new_box.append(h_new)
cv2.rectangle(img,(x_new,y_new),(x_new+w_new,y_new+h_new),(0,255,0), 1)
print new_box
box_num+=1
new_boxes.append(new_box)
boxes_dict["boxes"] = new_boxes
if box_num == 0:
continue img_tar_dir = os.path.join(tar_img_dir,"JPEGImages")
if not os.path.exists(img_tar_dir):
os.mkdir(img_tar_dir)
tar_path = os.path.join(img_tar_dir,path_seg[1])
cv2.imwrite(tar_path,img) xml_tar_dir = os.path.join(tar_img_dir, "Annotations")
if not os.path.exists(xml_tar_dir):
os.mkdir(xml_tar_dir)
create_xml(boxes_dict,xml_tar_dir)
fw.write(path_seg[1].split('.')[0]+'\n')
fw.flush() def parse(label_file_path, src_img_dir, tar_img_dir):
fr = open(label_file_path,'r')
image_list = []
line = fr.readline().rstrip()
while line:
mdict = {}
path = line
mdict["path"] = path
num = fr.readline().rstrip()
boxes_list = []
for n in range(int(num)):
box = fr.readline().rstrip()
boxes_list.append(box)
mdict["boxes"]=boxes_list
image_list.append(mdict)
line = fr.readline().rstrip()
draw_and_save(image_list,src_img_dir,tar_img_dir) if __name__=="__main__":
file_path = "/projects/DSOD/wider_face/datasets/wider_face_split/wider_face_val_bbx_gt.txt"
source_img_dir = "/projects/DSOD/wider_face/datasets/val/images"
target_img_dir = "/projects/DSOD/wider_face/datasets/drew"
if not os.path.exists(target_img_dir):
os.mkdir(target_img_dir)
parse(file_path,source_img_dir,target_img_dir)

WiderFace标注格式转PASCAL VOC2007标注格式的更多相关文章

  1. 目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

    将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as ...

  2. caffe学习笔记(一),ubuntu14.04+GPU (用Pascal VOC2007训练数据,并测试)

    把源代码跑起来了,将实验过程记录如下,用于新手入门. 今天和师兄师姐才跑通,来分享下心得.(预训练网络:ImageNet_model,训练集:PASCAL VOC2007, GPU) 首先,整个tra ...

  3. PDF文件如何标注,怎么使用PDF标注工具

    我们在使用文件的时候需要给文件的部分添加标注,能够更加直观的了解文件,但是有很多小伙伴们对于PDF文件怎么添加标注都不知道,也不知道PDF标注工具要怎么使用,那么下面就跟大家分享一下怎么使用PDF标注 ...

  4. [转] 将DOS格式文本文件转换成UNIX格式

    点击此处阅读原文 用途说明 dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter).DOS下的文本文 ...

  5. 【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式

    小伙伴多次向 Kindle 伴侣提出一个问题,那就是通过 Calibre 将排版精美的 azw3 格式电子书转换成 mobi 格式后推送到 Kindle,排版格式会发生很大的变化,比如行距过窄.内嵌字 ...

  6. Java json设置时间格式,Jackson设置时间格式,json设置单引号

    Java json设置时间格式,Jackson设置时间格式,json设置单引号 >>>>>>>>>>>>>>> ...

  7. web字体格式及几种在线格式转换工具介绍

    原文地址:http://blog.csdn.net/xiaolongtotop/article/details/8316554 目前,文字信息仍是网站最主要的内容,随着CSS3技术的不断成熟,Web字 ...

  8. Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式

    Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式 找到eclipse安装文件夹以下的plugins文件夹,搜索 org.eclipse.text ,找到一个jar ...

  9. ASP:GB2312格式文本文件转换成UTF-8格式

    '-------------------------------------------------'函数名称:gb2utf_file'作用:利用AdoDb.Stream对象来把GB2312格式文本文 ...

随机推荐

  1. 【题解】CF#833 B-The Bakery

    一个非常明显的 \(nk\) dp 状态 \(f[i][k]\) 表示以 \(i\) 为第 \(k\) 段的最后一个元素时所能获得的最大代价.转移的时候枚举上一段的最后一个元素 \(j\)更新状态即可 ...

  2. 对于iOS性能优化的一点看法

    在我们通常的开发工作中,每次需求定下来的时候,开发时间都是很紧张的,于是我们就抓紧时间开发,完成需求.在匆忙开发的过程中,或多或少的会有一些性能问题存在,在开发任务完成以后,我们都要进行性能优化.现将 ...

  3. 洛谷4245:【模板】任意模数NTT——题解

    https://www.luogu.org/problemnew/show/P4245 给两个多项式,求其乘积,每个系数对p取模. 参考: 代码与部分理解参考https://www.luogu.org ...

  4. 洛谷 P2446 [SDOI2010]大陆争霸 解题报告

    P2446 [SDOI2010]大陆争霸 题目背景 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭的神曾·布拉 ...

  5. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  6. 项目管理---git----快速使用git笔记(六)------本地开发与远程仓库的交互----常用git命令

    无论是我们自己把本地的项目新建了一个远程仓库 还是 从远程仓库获取到了 本地,现在我们都在本地有了一份项目代码,服务器上对应有项目代码的信息. 现在我们就开始进行交互操作了. 也就是说明一些在 正常开 ...

  7. bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序对)

    题目大意:给出n个数的序列,每次可以交换相邻的两个数,问把序列变成编号i在编号i+1左边,编号1在编号n右边(一个环)最少需要多少步.如:35421最少交换两次变为34512. 一开始看到这题,只会O ...

  8. Makefile中的 =,:=,?=,+= 的差异

    在Makefile中常常遇见这几种等操作,总结一下具体区别. =  是最基本的赋值 :=  是用右值覆盖左值 ?=  判断,如果左值没有被赋值过就赋以右值,否则,不做赋值动作 += 在左值后面连接右值 ...

  9. dbms_output.put与put_line

    BEGIN DBMS_OUTPUT.ENABLE (buffer_size => NULL);--with no limit on the output. dbms_output.put('a' ...

  10. JAVA、android中常用的一些jar包的作用

    正文: 这里主要介绍的是hibernate使用到的.jar Hibernate一共包括了23个jar包,令人眼花缭乱.本文将详细讲解Hibernate每个jar包的作用,便于你在应用中根据自己的需要进 ...