保存标注对象到txt 制作xml
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的更多相关文章
- OpenCV训练分类器制作xml文档
OpenCV训练分类器制作xml文档 (2011-08-25 15:50:06) 转载▼ 标签: 杂谈 分类: 学习 我的问题:有了opencv自带的那些xml人脸检测文档,我们就可以用cvLoad( ...
- Adobe AIR and Flex - 保存序列化对象文件(译)
创建任何桌面应用程序几乎总是需要在本地存储数据,通过Adobe AIR我们有几下面几个选择,一个是我们能够使用内置的 SQLite 数据库支持,对于少量的数据这是大材小用了.另外一个选择是我们通过把数 ...
- Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML
同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...
- 网站robots.txt & sitemap.xml
1. 如何查看网站的robots.txt 网址/robots.txt, 比如小米 https://www.mi.com/robots.txt sitemap.xml
- NSUserDefault 保存自定义对象
由于NSUserDefaults 不支持保存自定类,保存的对象需要实现NSCoding协议,不过自定的类型就算实现了NSCoding也不可以保存,可以通过以下方法实现: //h文件 #import & ...
- solr6.6 导入 文本(txt/json/xml/csv)文件
参照:solr6.6 导入 pdf文件 重点就是三个配置文件 1.建立的data-config.xml 内容如下: <dataConfig> <dataSource name=&qu ...
- Tomcat关闭后,重新启动,session中保存的对象为什么还存在解决方法
Tomcat关闭后,重新启动,session中保存的对象为什么还存在各们朋友大家好: 当我关闭Tomcat,重新启动后,session中保存的对象还依然存在,仍然可以使用,不知这是什么 ...
- 在MySQL中保存Java对象
需要在MySQL中保存Java对象. 说明: 对象必须实现序列化 MySQL中对应字段设置为blob 将Java对象序列化为byte[] public static byte[] obj2byte(O ...
- Map集合的遍历方式以及TreeMap集合保存自定义对象实现比较的Comparable和Comparator两种方式
Map集合的特点 1.Map集合中保存的都是键值对,键和值是一一对应的 2.一个映射不能包含重复的值 3.每个键最多只能映射到一个值上 Map接口和Collection接口的不同 Map是双列集合的根 ...
随机推荐
- oracle中 sql%rowcount 用法
sql%rowcount用于记录修改的条数,必须放在一个更新或者删除等修改类语句后面执行,select语句用于查询的话无法使用, 当你执行多条修改语句时,按照sql%rowcount 之前执行的最后一 ...
- PHP7的异常处理机制,set_error_handler和set_exception_handler方法介绍
https://blog.csdn.net/zhang197093/article/details/75094816
- [小程序] 微信小程序 picker 中range-key中必须带单引号
原文地址:http://blog.csdn.net/u012329294/article/details/74906504 <view class="section"> ...
- Python实现图像直方图均衡化算法
title: "Python实现图像直方图均衡化算法" date: 2018-06-12T17:10:48+08:00 tags: [""] categorie ...
- Remastersys打包你自己的ubuntu成iso文件
采用Remastersys3.0.4.ubuntu版本是ubuntu14.04 LTS amd64. (1)软件下载安装: 下载: 到http://www.easy-vdr.de/downloads/ ...
- arcgis for js 根据多边形自动缩放
交代背景:多边形已经渲染在图层上,然后根据多边形自动缩放值合适的大小: 思路:获取图层信息,获取图层中的几何信息,获取图形范围信息,在地图上设置范围:(下面的方法有封装)记一下思路就好 var pol ...
- Maven不能下载SNAPSHOT包但是能下载RELEASE包的解决办法
在使用过程中,Maven默认配置是不能下载SNAPSHOT包的,这是基于一种代码稳定性进行考量得出的结论.引入SNAPSHOT包最大的问题就是,由于SNAPSHOT允许重复上传,所以引用一个这样的包开 ...
- Linux Spi驱动移植小结
2012-01-07 22:21:29 效果图: 理论学习后,主要是linux中spi子系统设备框架的了解后,主控制器与设备分离的思想,那么我要开始动手了. 1, make menuconfig添加 ...
- python模块部分----模块、包、常用模块
0.来源:https://www.cnblogs.com/jin-xin/articles/9987155.html 1.导入模块 1.1模块就是一个python文件,模块名是文件名 1.2导入模块的 ...
- domain
babibobucecicudadedidufafugeguhehujijukakekulalelilumimomunapapipopuqiqurerirusasesisutatetituwawowu ...