DIOR数据集转化为COCO格式

2023-11-08 01:30
文章标签 数据 格式 转化 coco dior

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

DIOR数据集转化为COCO格式

为了实验方便,需要将DIOR数据集转化为COCO格式,在此将代码共享,希望能帮助到同样研究方向的人。

解压DIOR数据集的压缩文件之后,你的路径应该是这样的:

第一个参数是你电脑里上图的路径,第二个参数是你想输出COCO格式文件的路径

DIOR缺少很多COCO格式的数据,所以缺少的项都为空,代码如下

import os
import cv2
from tqdm import tqdm
import json
import xml.dom.minidomcategory_list = ['airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge', 'chimney', 'dam','Expressway-Service-area', 'Expressway-toll-station', 'golffield', 'groundtrackfield', 'harbor','overpass', 'ship', 'stadium', 'storagetank', 'tenniscourt', 'trainstation', 'vehicle', 'windmill']def convert_to_cocodetection(dir, output_dir):"""input:dir:the path to DIOR datasetoutput_dir:the path write the coco form json file"""annotations_path = dirnamelist_path = os.path.join(dir, "Main")trainval_images_path = os.path.join(dir, "JPEGImages-trainval")test_images_path = os.path.join(dir, "JPEGImages-test")id_num = 0categories = [{"id": 0, "name": "Airplane"},{"id": 1, "name": "Airport"},{"id": 2, "name": "Baseball field"},{"id": 3, "name": "Basketball court"},{"id": 4, "name": "Bridge"},{"id": 5, "name": "Chimney"},{"id": 6, "name": "Dam"},{"id": 7, "name": "Expressway service area"},{"id": 8, "name": "Expressway toll station"},{"id": 9, "name": "Golf course"},{"id": 10, "name": "Ground track field"},{"id": 11, "name": "Harbor"},{"id": 12, "name": "Overpass"},{"id": 13, "name": "Ship"},{"id": 14, "name": "Stadium"},{"id": 15, "name": "Storage tank"},{"id": 16, "name": "Tennis court"},{"id": 17, "name": "Train station"},{"id": 18, "name": "Vehicle"},{"id": 19, "name": "Wind mill"},]for mode in ["train", "val"]:images = []annotations = []print(f"start loading {mode} data...")if mode == "train":f = open(namelist_path + "/" + "train.txt", "r")images_path = trainval_images_pathelse:f = open(namelist_path + "/" + "val.txt", "r")images_path = trainval_images_pathfor name in tqdm(f.readlines()):# image partimage = {}name = name.replace("\n", "")image_name = name + ".jpg"annotation_name = name + ".xml"height, width = cv2.imread(images_path + "/" + image_name).shape[:2]image["file_name"] = image_nameimage["height"] = heightimage["width"] = widthimage["id"] = nameimages.append(image)# anno partdom = xml.dom.minidom.parse(dir + "/" + annotation_name)root_data = dom.documentElementfor i in range(len(root_data.getElementsByTagName('name'))):annotation = {}category = root_data.getElementsByTagName('name')[i].firstChild.datatop_left_x = root_data.getElementsByTagName('xmin')[i].firstChild.datatop_left_y = root_data.getElementsByTagName('ymin')[i].firstChild.dataright_bottom_x = root_data.getElementsByTagName('xmax')[i].firstChild.dataright_bottom_y = root_data.getElementsByTagName('ymax')[i].firstChild.databbox = [top_left_x, top_left_y, right_bottom_x, right_bottom_y]bbox = [int(i) for i in bbox]bbox = xyxy_to_xywh(bbox)annotation["image_id"] = nameannotation["bbox"] = bboxannotation["category_id"] = category_list.index(category)annotation["id"] = id_numannotation["iscrowd"] = 0annotation["segmentation"] = []annotation["area"] = bbox[2] * bbox[3]id_num += 1annotations.append(annotation)dataset_dict = {}dataset_dict["images"] = imagesdataset_dict["annotations"] = annotationsdataset_dict["categories"] = categoriesjson_str = json.dumps(dataset_dict)with open(f'{output_dir}/DIOR_{mode}_coco.json', 'w') as json_file:json_file.write(json_str)print("json file write done...")def get_test_namelist(dir, out_dir):full_path = out_dir + "/" + "test.txt"file = open(full_path, 'w')for name in tqdm(os.listdir(dir)):name = name.replace(".txt", "")file.write(name + "\n")file.close()return Nonedef centerxywh_to_xyxy(boxes):"""args:boxes:list of center_x,center_y,width,height,return:boxes:list of x,y,x,y,cooresponding to top left and bottom right"""x_top_left = boxes[0] - boxes[2] / 2y_top_left = boxes[1] - boxes[3] / 2x_bottom_right = boxes[0] + boxes[2] / 2y_bottom_right = boxes[1] + boxes[3] / 2return [x_top_left, y_top_left, x_bottom_right, y_bottom_right]def centerxywh_to_topleftxywh(boxes):"""args:boxes:list of center_x,center_y,width,height,return:boxes:list of x,y,x,y,cooresponding to top left and bottom right"""x_top_left = boxes[0] - boxes[2] / 2y_top_left = boxes[1] - boxes[3] / 2width = boxes[2]height = boxes[3]return [x_top_left, y_top_left, width, height]def xyxy_to_xywh(boxes):width = boxes[2] - boxes[0]height = boxes[3] - boxes[1]return [boxes[0], boxes[1], width, height]def clamp(coord, width, height):if coord[0] < 0:coord[0] = 0if coord[1] < 0:coord[1] = 0if coord[2] > width:coord[2] = widthif coord[3] > height:coord[3] = heightreturn coordif __name__ == '__main__':convert_to_cocodetection(r"path to your DIOR dataset", r"the path you want to write the coco format json file")

 

这篇关于DIOR数据集转化为COCO格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

MySQL数据脱敏的实现方法

《MySQL数据脱敏的实现方法》本文主要介绍了MySQL数据脱敏的实现方法,包括字符替换、加密等方法,通过工具类和数据库服务整合,确保敏感信息在查询结果中被掩码处理,感兴趣的可以了解一下... 目录一. 数据脱敏的方法二. 字符替换脱敏1. 创建数据脱敏工具类三. 整合到数据库操作1. 创建服务类进行数据库

MySQL中处理数据的并发一致性的实现示例

《MySQL中处理数据的并发一致性的实现示例》在MySQL中处理数据的并发一致性是确保多个用户或应用程序同时访问和修改数据库时,不会导致数据冲突、数据丢失或数据不一致,MySQL通过事务和锁机制来管理... 目录一、事务(Transactions)1. 事务控制语句二、锁(Locks)1. 锁类型2. 锁粒

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3