将NWPU VHR-10数据集转换为coco格式

news/2024/7/4 3:01:57
"""
Created on 5/11
读取txt文件,划分训练集和测试集并且生成coco格式的json文件
@author: Wu
"""
import json
import os
import numpy as np
import pandas as pd
import re
import cv2
import shutil
from PIL import Image
from sklearn.model_selection import train_test_split

# generate class name dict
class_name = ['airplane', 'ship', 'storage tank', 'baseball diamond', 'tennis court',
 'basketball court', 'ground track field', 'harbor', 'bridge', 'vehicle']
label_dict = {}
i = 1
for hahah in class_name:
    label_dict[i] = hahah
    i += 1
# output path
im_path = 'E:/dataset/NWPU VHR-10/NWPU VHR-10 dataset/positive image set'
ann_path = 'E:/dataset/NWPU VHR-10/NWPU VHR-10 dataset/ground truth'
output_ann_path = ['E:/dataset/NWPU VHR-10/NWPU VHR-10 dataset/train.json', 'E:/dataset/NWPU VHR-10/NWPU VHR-10 dataset/test.json']

def transform_vhr2coco(ann_path, im_path, output_ann_path):

    '''
    Param:
        ann_path txt标注所在路径
        im_path positive 图片所在路径
        out_ann_path 输出文件路径及命名
    '''

    # 初始化dataset
    datasets = [dict(), dict()]
    annotation_id = [0, 0]
    for dataset in datasets:
        dataset['images'] = []
        dataset['type'] = 'instances'
        dataset['annotations'] = []
        dataset['categories'] = []
        dataset['info'] = None
        dataset['licenses'] = None
    
    # add dataset['categories']
    for category_id, category_name in label_dict.items():
        category_item = dict()
        category_item['supercategory'] = category_name
        category_item['id'] = category_id
        category_item['name'] = category_name
        for dataset in datasets:
            dataset['categories'].append(category_item)

    # split train test set
    ann_list = os.listdir(ann_path)
    train_ids, test_ids = train_test_split(ann_list, test_size=0.3)
    train_val = {}
    for haha in train_ids:
        train_val[haha] = 0 # train 
    for haha in test_ids:
        train_val[haha] = 1
    # iter through every txt to generate train.json and test.json
    for i, name_list in enumerate((train_ids, test_ids)):
        for index, ann_filename in enumerate(name_list):  
            print(f'processing {index} th txt in {i}th dataset')
            # add dataset['images']
            img_name = ann_filename[0:-3]+'jpg'
            image = dict()
            image['id'] = index
            image['file_name'] = img_name
            img = cv2.imread(os.path.join(im_path, img_name))
            image['width'] = img.shape[1]
            image['height'] = img.shape[0]
            datasets[i]['images'].append(image)

            ann_filepath = os.path.join(ann_path, ann_filename)
            ann_df = pd.read_csv(ann_filepath, header=None)
            # iter through every annotation on one image
            for _, ann in ann_df.iterrows():
                # add annotation
                x = int(ann[0][1:])
                y = int(ann[1][0:-1])
                w = int(ann[2][1:]) - x
                h = int(ann[3][0:-1]) - y
                label = int(ann[4])
                annotation_item = dict()
                annotation_item['segmentation'] = [[x, y, x, y+h, x+w, y+h, x+w, y]]
                annotation_item['image_id'] = image['id']
                annotation_item['iscrowd'] = 0
                annotation_item['bbox'] = [x, y, w, h]
                annotation_item['area'] = w * h
                annotation_item['id'] = annotation_id[i]
                annotation_id[i] = annotation_id[i] + 1
                annotation_item['category_id'] = label
                datasets[i]['annotations'].append(annotation_item)
        json.dump(datasets[i], open(output_ann_path[i], 'w'))

if __name__ == '__main__':
    transform_vhr2coco(ann_path=ann_path, im_path=im_path, output_ann_path=output_ann_path)

http://www.niftyadmin.cn/n/3658025.html

相关文章

yolov4官方源码在自己的数据集上运行笔记

地址:https://github.com/AlexeyAB/darknet源码是c的不是python的,所以配置稍微有点麻烦: 首先要先配好环境,否则编译会出错,就算编译不出错,运行也会出错,环境要求看官方文档(也就是…

如何知道可执行文件是32-bit还是64-bit

可以使用GetBinaryType API来获得这个信息:BOOL GetBinaryType(LPCTSTR lpApplicationName,LPDWORD lpBinaryType);Binary Type可以是下面的值。ValueMeaningSCS_32BIT_BINARYA 32-bit Windows-based applicationSCS_64BIT_BINARYA 64-bit Windows-based applicatio…

SSD pytorch 运行debug

出于某些原因需要在一个数据集上测试SSD 源代码来自github 高star的repos:https://github.com/amdegroot/ssd.pytorch为了在自己的数据集上进行训练和测试,首先是要按下面这篇blog的流程先修改: https://blog.csdn.net/dingkm666/article/det…

转载:如何指定程序在Vista上面需要提升权限运行(Elevated)

在Manifest中加上下面内容即可:level""asInvoker""uiAccess""false"" />全文请见:http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx

CornerNet跑自己数据集笔记

原repos地址为:https://github.com/princeton-vl/CornerNet为了在服务器的cuda10电脑上用自己的数据集跑这份代码,主要有三点: 修改数据集接口适应cuda10和pytorch1.5训练配置 接下来就分点详述: 修改数据集接口: 有…

转贴:Mark Russinovich的Inside Vista Kernel系列文章,讲到了Vista内核的调度,IO,内存管理,缓存,事务处理,安全等众多新特性

Mark Russinovich的Inside Vista Kernel系列文章。Mark是SysInternals的创始人之一,是多个著名系统工具的作者,是Windows系统内核方面的高手。不久前他接受了Microsoft的Technical Fellow职位,参与Windows Kernel开发。本文讲到了Vista内核的…

2-6月以来密集的项目实践的总结

2-4月做了三件事,为MICCAI会议投稿论文准备一些实验,打了个超分辨率比赛NTIRE,得了大概是第7名/200多个参赛队伍,做了一个新冠病毒相关肺部图像诊断的项目;4-6月以来做了三件事情,写了一份(实际…

今天David Solomon的为期三天的Windows Internal培训刚结束

今天David Solomon的为期三天的Windows Internal培训刚结束,很累,但是也受益匪浅。讲座基本上是他和Mark合著的Windows Internal书最新的Vista相关内容,讲的非常清晰透彻,不愧是David。(其实搞技术不一定非要写程序&am…