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

相关文章

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Mysql常见的SQL语句格式及实用技巧

《Mysql常见的SQL语句格式及实用技巧》本文系统梳理MySQL常见SQL语句格式,涵盖数据库与表的创建、删除、修改、查询操作,以及记录增删改查和多表关联等高级查询,同时提供索引优化、事务处理、临时... 目录一、常用语法汇总二、示例1.数据库操作2.表操作3.记录操作 4.高级查询三、实用技巧一、常用语

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

浅析如何保证MySQL与Redis数据一致性

《浅析如何保证MySQL与Redis数据一致性》在互联网应用中,MySQL作为持久化存储引擎,Redis作为高性能缓存层,两者的组合能有效提升系统性能,下面我们来看看如何保证两者的数据一致性吧... 目录一、数据不一致性的根源1.1 典型不一致场景1.2 关键矛盾点二、一致性保障策略2.1 基础策略:更新数