目标检测 把遥感目标检测RSOD数据集转换为MSCOCO格式

一、RSOD数据集文件组织格式

        RSOD数据集是一个用于遥感图像中目标检测的开放数据集。该数据集包括飞机、油罐、游乐场和立交桥四类目标。数据集的格式是PASCAL VOC格式。

        数据集包括4个文件夹,每个文件夹对应一种目标类型。

  1. 飞机数据集(aircraft),包含446幅图像中的4993架飞机。
  2. 操场(playground),包含189幅图像中的191个游乐场。
  3. 立交桥(overpass),包含176幅图像中的180个立交桥。
  4. 油罐(oiltank),包含165幅图像中的1586个油罐​。

        在每一个类别的文件下,包含Annotation和JPEGImages。Annotation下包括label(txt)和xml两个文件夹。这就是RSOD的文件结构。

 

二、RSOD2COCO.py

        以下代码将读取所有类别的所有标注,转换到一个trainval.json中,包含了所有目标信息,没有进行数据集分割。

import json
import os
from PIL import Image

if __name__ == '__main__':
    image_dir = './RSOD/JPEGImages/'
    anno_dir = './RSOD/txt/'
    json_save_path = './RSOD/'
    class_id = {'aircraft': 1, 'oiltank': 2, 'overpass': 3, 'playground': 4}
    write_json_context = dict()  # 写入.json文件的大字典
    write_json_context['info'] = {'description': '', 'year': 2023, 'contributor': '',
                                  'date_created': '2023-02-06'}
    write_json_context['licenses'] = [{'id': None, 'name': None}]
    write_json_context['categories'] = [{"supercategory": "RSOD", "id": 1, "name": "aircraft"},
                                        {"supercategory": "RSOD", "id": 2, "name": "oiltank"},
                                        {"supercategory": "RSOD", "id": 3, "name": "overpass"},
                                        {"supercategory": "RSOD", "id": 4, "name": "playground"}]

    write_json_context['images'] = []
    write_json_context['annotations'] = []
    image_id_init = 0
    box_id = 0
    image_file_list = os.listdir(image_dir)
    for i, image_file in enumerate(image_file_list):
        image_path = os.path.join(image_dir, image_file)
        image = Image.open(image_path)
        w, h = image.size
        img_context = {'id': image_id_init + i, 'file_name': image_file, 'height': h, 'width': w}
        write_json_context['images'].append(img_context)

        txt_name = str(image_file.split('.')[0]) + '.txt'
        txt_file = os.path.join(anno_dir, txt_name)

        with open(txt_file) as f:
            lines = f.readlines()

        for j, line in enumerate(lines):
            img_name, class_name, xmin, ymin, xmax, ymax = line.strip().split()

            assert (int(xmax) > int(xmin)), "xmax <= xmin, {}".format(line)
            assert (int(ymax) > int(ymin)), "ymax <= ymin, {}".format(line)

            o_width = abs(int(xmax) - int(xmin))

            o_height = abs(int(ymax) - int(ymin))

            bbox_context = {}
            box_id += 1
            bbox_context['id'] = box_id

            bbox_context['image_id'] = image_id_init + i
            bbox_context['category_id'] = class_id[class_name]
            bbox_context['iscrowd'] = 0
            bbox_context['area'] = o_width * o_height
            bbox_context['bbox'] = [abs(int(xmin)), abs(int(ymin)), o_width, o_height]
            bbox_context['segmentation'] = []
            write_json_context['annotations'].append(bbox_context)
    name = os.path.join(json_save_path, 'trainval.json')

    with open(name, 'w') as fw:
        json.dump(write_json_context, fw, indent=2)

然后,可以通过我另一篇文章的代码,将coco数据集分割。见:分割COCO数据集为train,val,test

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值