DOTA数据集转VOC数据集,模仿DIOR数据集类型

2023-12-12 02:10

本文主要是介绍DOTA数据集转VOC数据集,模仿DIOR数据集类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DOTA文本数据集转XML格式

说明:由于本人比较喜欢DIOR数据集格式,所以仿照DIOR数据集转的相同格式

DOTA数据集下载链接:https://pan.baidu.com/s/1lksAYbogYT3OjTBzNuQuNA
提取码:7vwv

DOIR数据集下载链接:
https://pan.baidu.com/s/1QPALicrLHqhblnGu_EBjlw
提取码:jcpg

代码如下(改了网上的代码,此处贴上链接)

import os
from xml.dom.minidom import Document
from xml.dom.minidom import parse
import xml.dom.minidom
import numpy as np
import csv
import cv2
import stringdef WriterXMLFiles(filename,img_name, path, box_list, label_list, w, h, d):# dict_box[filename]=json_dict[filename]doc = xml.dom.minidom.Document()root = doc.createElement('annotation')doc.appendChild(root)# foldername = doc.createElement("folder")# foldername.appendChild(doc.createTextNode("JPEGImages"))# root.appendChild(foldername)nodeFilename = doc.createElement('filename')nodeFilename.appendChild(doc.createTextNode(img_name))root.appendChild(nodeFilename)# pathname = doc.createElement("path")# pathname.appendChild(doc.createTextNode("xxxx"))# root.appendChild(pathname)sourcename=doc.createElement("source")databasename = doc.createElement("database")databasename.appendChild(doc.createTextNode("DOTA"))sourcename.appendChild(databasename)# annotationname = doc.createElement("annotation")# annotationname.appendChild(doc.createTextNode("xxx"))# sourcename.appendChild(annotationname)# imagename = doc.createElement("image")# imagename.appendChild(doc.createTextNode("xxx"))# sourcename.appendChild(imagename)# flickridname = doc.createElement("flickrid")# flickridname.appendChild(doc.createTextNode("0"))# sourcename.appendChild(flickridname)root.appendChild(sourcename)nodesize = doc.createElement('size')nodewidth = doc.createElement('width')nodewidth.appendChild(doc.createTextNode(str(w)))nodesize.appendChild(nodewidth)nodeheight = doc.createElement('height')nodeheight.appendChild(doc.createTextNode(str(h)))nodesize.appendChild(nodeheight)nodedepth = doc.createElement('depth')nodedepth.appendChild(doc.createTextNode(str(d)))nodesize.appendChild(nodedepth)root.appendChild(nodesize)segname = doc.createElement("segmented")segname.appendChild(doc.createTextNode("0"))root.appendChild(segname)for (box, label) in zip(box_list, label_list):nodeobject = doc.createElement('object')nodename = doc.createElement('name')nodename.appendChild(doc.createTextNode(str(label)))nodeobject.appendChild(nodename)nodebndbox = doc.createElement('bndbox')nodex1 = doc.createElement('x1')nodex1.appendChild(doc.createTextNode(str(box[0])))nodebndbox.appendChild(nodex1)nodey1 = doc.createElement('y1')nodey1.appendChild(doc.createTextNode(str(box[1])))nodebndbox.appendChild(nodey1)nodex2 = doc.createElement('x2')nodex2.appendChild(doc.createTextNode(str(box[2])))nodebndbox.appendChild(nodex2)nodey2 = doc.createElement('y2')nodey2.appendChild(doc.createTextNode(str(box[3])))nodebndbox.appendChild(nodey2)nodex3 = doc.createElement('x3')nodex3.appendChild(doc.createTextNode(str(box[4])))nodebndbox.appendChild(nodex3)nodey3 = doc.createElement('y3')nodey3.appendChild(doc.createTextNode(str(box[5])))nodebndbox.appendChild(nodey3)nodex4 = doc.createElement('x4')nodex4.appendChild(doc.createTextNode(str(box[6])))nodebndbox.appendChild(nodex4)nodey4 = doc.createElement('y4')nodey4.appendChild(doc.createTextNode(str(box[7])))nodebndbox.appendChild(nodey4)# ang = doc.createElement('angle')# ang.appendChild(doc.createTextNode(str(angle)))# nodebndbox.appendChild(ang)nodeobject.appendChild(nodebndbox)root.appendChild(nodeobject)fp = open(path + filename, 'w')doc.writexml(fp, indent='\n')fp.close()def load_annoataion(p):'''load annotation from the text file:param p::return:'''text_polys = []text_tags = []if not os.path.exists(p):return np.array(text_polys, dtype=np.float32)with open(p, 'r') as f:for line in f.readlines()[2:]:label = 'text'# strip BOM. \ufeff for python3,  \xef\xbb\bf for python2#line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in line]#print(line)x1, y1, x2, y2, x3, y3, x4, y4 ,label= line.split(' ')[0:9]#print(label)text_polys.append([x1, y1, x2, y2, x3, y3, x4, y4])text_tags.append(label)return np.array(text_polys, dtype=np.float), np.array(text_tags, dtype=np.str)txt_path = r'E:/baidudisk/DOTA/train/labelTxt-v1.5/DOTA-v1.5_train/'
xml_path = r'E:/baidudisk/DOTA/train/labelTxt-v1.5-voc/DOTA-v1.5_train/'
img_path = r'E:/baidudisk/DOTA/train/images/images/'
print(os.path.exists(txt_path))
txts = os.listdir(txt_path)
for count, t in enumerate(txts):path = os.path.join(txt_path,t)print(path)# boxes存储的是八个坐标# labels存储的是标签boxes, labels = load_annoataion(os.path.join(txt_path, t))# P0000.xmlxml_name = t.replace('.txt', '.xml')img_name = t.replace('.txt', '.png')# P0000.pngimg = cv2.imread(os.path.join(img_path, img_name))h, w, d = img.shape#print(xml_name, xml_path, boxes, labels, w, h, d)WriterXMLFiles(xml_name,img_name, xml_path, boxes, labels, w, h, d)if count % 1000 == 0:print(count)

说明
1:请务必将自己的文件路径进行修改
2:导包出现如下图横线,感觉也没有影响程序运行
在这里插入图片描述
3:文件转换最终如下图:
在这里插入图片描述

这篇关于DOTA数据集转VOC数据集,模仿DIOR数据集类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/482891

相关文章

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

MySQL数据目录迁移的完整过程

《MySQL数据目录迁移的完整过程》文章详细介绍了将MySQL数据目录迁移到新硬盘的整个过程,包括新硬盘挂载、创建新的数据目录、迁移数据(推荐使用两遍rsync方案)、修改MySQL配置文件和重启验证... 目录1,新硬盘挂载(如果有的话)2,创建新的 mysql 数据目录3,迁移 MySQL 数据(推荐两

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

C# WebAPI的几种返回类型方式

《C#WebAPI的几种返回类型方式》本文主要介绍了C#WebAPI的几种返回类型方式,包括直接返回指定类型、返回IActionResult实例和返回ActionResult,文中通过示例代码介绍的... 目录创建 Controller 和 Model 类在 Action 中返回 指定类型在 Action

MySQL中的DELETE删除数据及注意事项

《MySQL中的DELETE删除数据及注意事项》MySQL的DELETE语句是数据库操作中不可或缺的一部分,通过合理使用索引、批量删除、避免全表删除、使用TRUNCATE、使用ORDERBY和LIMI... 目录1. 基本语法单表删除2. 高级用法使用子查询删除删除多表3. 性能优化策略使用索引批量删除避免

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE