1、算法将检测的目标名称和目标位置保存到txt文本

图片名  xmin ymin xmax ymax

(4).avi237face.jpg
4
smoke 83 234 142 251
hand 119 255 271 306
eye 178 148 216 163
eye 111 156 148 173

#!/usr/bin/python
# -*- coding: UTF-8 -*- import os, h5py, cv2, sys, shutil
import numpy as np
from xml.dom.minidom import Document rootdir = "G:/MTCNNTraining/faceData/train"
convet2yoloformat = True
convert2vocformat = True
resized_dim = (48, 48) # 最小取20大小的脸,并且补齐
minsize2select = 1
usepadding = True def convertimgset(img_set="train"):
imgdir = rootdir + "/trainImages"
gtfilepath = rootdir + "/SSDSave.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 = []
bnames=[]
for i in range(numbbox):
line_read = gtfile.readline()
line_cor = line_read.strip().split(" ")
obj_name = line_cor[0]
#line = line_cor[1:5]
line = list(map(int,line_cor[1:5])) if (int(line[3]) <= 0 or int(line[2]) <= 0):
continue
x = int(line[0]) + paddingleft #左上角顶点x
y = int(line[1]) + paddingtop #左上角顶点y
width = int(line[2]) - int(line[0]) + 1 #宽度
height = int(line[3]) - int(line[1])+ 1 #高度
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)
bnames.append(obj_name)
#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')
bnames_var = str(bnames[i]) object_name.appendChild(doc.createTextNode(bnames_var))
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 convertdataset():
img_sets = ["train"]
for img_set in img_sets:
convertimgset(img_set) if __name__ == "__main__":
convertdataset()

保存标注对象到txt 制作xml的更多相关文章

  1. OpenCV训练分类器制作xml文档

    OpenCV训练分类器制作xml文档 (2011-08-25 15:50:06) 转载▼ 标签: 杂谈 分类: 学习 我的问题:有了opencv自带的那些xml人脸检测文档,我们就可以用cvLoad( ...

  2. Adobe AIR and Flex - 保存序列化对象文件(译)

    创建任何桌面应用程序几乎总是需要在本地存储数据,通过Adobe AIR我们有几下面几个选择,一个是我们能够使用内置的 SQLite 数据库支持,对于少量的数据这是大材小用了.另外一个选择是我们通过把数 ...

  3. Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML

    同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...

  4. 网站robots.txt & sitemap.xml

    1. 如何查看网站的robots.txt 网址/robots.txt, 比如小米  https://www.mi.com/robots.txt sitemap.xml

  5. NSUserDefault 保存自定义对象

    由于NSUserDefaults 不支持保存自定类,保存的对象需要实现NSCoding协议,不过自定的类型就算实现了NSCoding也不可以保存,可以通过以下方法实现: //h文件 #import & ...

  6. solr6.6 导入 文本(txt/json/xml/csv)文件

    参照:solr6.6 导入 pdf文件 重点就是三个配置文件 1.建立的data-config.xml 内容如下: <dataConfig> <dataSource name=&qu ...

  7. Tomcat关闭后,重新启动,session中保存的对象为什么还存在解决方法

    Tomcat关闭后,重新启动,session中保存的对象为什么还存在各们朋友大家好:         当我关闭Tomcat,重新启动后,session中保存的对象还依然存在,仍然可以使用,不知这是什么 ...

  8. 在MySQL中保存Java对象

    需要在MySQL中保存Java对象. 说明: 对象必须实现序列化 MySQL中对应字段设置为blob 将Java对象序列化为byte[] public static byte[] obj2byte(O ...

  9. Map集合的遍历方式以及TreeMap集合保存自定义对象实现比较的Comparable和Comparator两种方式

    Map集合的特点 1.Map集合中保存的都是键值对,键和值是一一对应的 2.一个映射不能包含重复的值 3.每个键最多只能映射到一个值上 Map接口和Collection接口的不同 Map是双列集合的根 ...

随机推荐

  1. eclipse配置tomcat添加外部项目

    在eclipse中配置tomcat,添加外部项目. 添加外部项目 然后直接启动服务器,服务器里面不能添加项目.

  2. 容器技术研究-Kubernetes基本概念

    最近在研究容器技术,作为入门,基本概念必须搞明白,今天整理一下Kubernetes的基本概念. 一.什么是Kubernetes Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部 ...

  3. Android-Gradle(四)

    当你在开发一个app,通常你会有几个版本.大多数情况是你需要一个开发版本,用来测试app和弄清它的质量,然后还需要一个生产版本.这些版本通常有不同的设置,例如不同的URL地址.更可能的是你可能需要一个 ...

  4. ftp 上传和下载

    ftp 下载 #!/bin/bash #auth liwei #date DATE=$(date -d today +%Y%m%d) #data files path SRCDIR=/home/web ...

  5. python 类的属性__slots__ (了解一点点)

    当一个类需要创建大量实例时,可以通过__slots__声明实例所需要的属性, 优点: 1)更快的属性访问速度 2)减少内存消耗 3)限定一个类创建的实例只能有固定的实例属性(实例变量),不允许对象添加 ...

  6. linux下wrk的安装

    wrk是linux下开源的性能测试工具,并且只能在linux下运行,下面介绍下安装教程(以ubantu18.04环境为例): 1.预先安装git,如:apt install git 2.从git上拉取 ...

  7. react-native 导航器 react-navigation 3.x 使用

    React-navigation 介绍 React Navigation 源于 React Native 社区对一个可扩展且易于使用的导航解决方案的需求,它完全使用 JavaScript 编写. (如 ...

  8. Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数

    使用路由对象$route获取参数: 1.params: 参数获取:使用$route.params获取参数: 参数传递: URL传参:例 <route-linke to : "/food ...

  9. opencv学习之路(41)、人脸识别

    一.人脸检测并采集个人图像 //take_photo.cpp #include<opencv2/opencv.hpp> using namespace cv; using namespac ...

  10. SKCTF Writeup

    签到题 请打开微信关注,发送give me flag,即可获得. Encode 1.ACSCLL 首先看到这类题,我们肯定是要使用ASCLL的(这么明显的提示大家肯定一眼就能看出来),我们可以对照As ...