保存标注对象到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是双列集合的根 ...
随机推荐
- js列表添加内容清除内容,时钟
<div id="wai"> <div id="zuo"> <select multiple="multiple&quo ...
- 自己绘制table,加分页
- redis 字符串(string)函数
字符串(string)函数 get 命令/方法/函数 Description Get the value related to the specified key 取得与指定的键值相关联的值 Para ...
- SQL kaggle learn : WHERE AND
WHERE trip_start_timestamp Between '2017-01-01' And '2017-07-01' and trip_seconds > 0 and trip_mi ...
- python之堡垒机(第九天)
本节作业: 通过使用paramiko和sqlalchemy实现堡垒机功能 主要功能实现: 1.用户登录堡垒机后,无需知道密码或密钥可以SSH登录远端服务器: 2.用户对一个组内所有主机批量执行指定命令 ...
- python中sorted()和set()去重,排序
前言 在看一个聊天机器人的神经网络模型训练前准备训练数据,需要对训练材料做处理(转化成张量)需要先提炼词干,然后对词干做去重和排序 words = sorted(list(set(words))) 对 ...
- Python数据分析Pandas库之熊猫(10分钟一)
pandas熊猫10分钟教程 排序 df.sort_index(axis=0/1,ascending=False/True) df.sort_values(by='列名') import numpy ...
- java线程学习之synchronized关键字
关键字synchronized的作用是实现线程间的同步.它的任务是对同步的代码加锁.一个代码块同时只能有同一个线程进行读和写操作,从而保证线程间是安全的. 线程安全的概念是:当多个线程访问某一个类(对 ...
- 防止sql注入(简单)
(1)mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 使用方法如下: $sql = "select count ...
- dell-7559安装deepin15.8
这大概是我第三次尝试安装deepin了,上一次是显卡问题解决不了,这一次迷迷糊糊问题就解决了. 但是也是尝试了三五十次开机吧233333. 最终是EFI启动,grub引导deepin和win7. 我是 ...