Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/DOTA2COCO.py
Views: 475
import dota_utils as util1import os2import cv23import json4from PIL import Image5import os.path as osp67wordname_15 = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',8'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']910wordname_16 = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',11'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter', 'container-crane']1213wordname_18 = [ 'plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship',14'tennis-court', 'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor',15'swimming-pool', 'helicopter', 'container-crane', 'airport', 'helipad']1617hrsc_2016 = ['ship']18ucas_aod = ['car', 'airplane']1920def DOTA2COCOTrain(srcpath, destfile, cls_names, difficult='2'):21# set difficult to filter '2', '1', or do not filter, set '-1'2223imageparent = os.path.join(srcpath, 'images')24labelparent = os.path.join(srcpath, 'labelTxt')2526data_dict = {}27data_dict['images'] = []28data_dict['categories'] = []29data_dict['annotations'] = []30for idex, name in enumerate(cls_names):31single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}32data_dict['categories'].append(single_cat)3334inst_count = 135image_id = 136with open(destfile, 'w') as f_out:37filenames = util.GetFileFromThisRootDir(labelparent)38for file in filenames:39basename = util.custombasename(file)40# image_id = int(basename[1:])4142imagepath = os.path.join(imageparent, basename + '.png')43img = cv2.imread(imagepath)44height, width, c = img.shape4546single_image = {}47single_image['file_name'] = basename + '.png'48single_image['id'] = image_id49single_image['width'] = width50single_image['height'] = height51data_dict['images'].append(single_image)5253# annotations54objects = util.parse_dota_poly2(file)55for obj in objects:56if obj['difficult'] == difficult:57print('difficult: ', difficult)58continue59single_obj = {}60single_obj['area'] = obj['area']6162if obj['name'] not in cls_names: # 去掉别的类63print('This classname not found: ', obj['name'])64continue6566single_obj['category_id'] = cls_names.index(obj['name']) + 167single_obj['segmentation'] = []68single_obj['segmentation'].append(obj['poly'])69single_obj['iscrowd'] = 070# xmin, ymin, xmax, ymax = min(obj['poly'][0::2]), min(obj['poly'][1::2]), \71# max(obj['poly'][0::2]), max(obj['poly'][1::2])72x1 = obj['poly'][0]73y1 = obj['poly'][1]74x2 = obj['poly'][2]75y2 = obj['poly'][3]76x3 = obj['poly'][4]77y3 = obj['poly'][5]78x4 = obj['poly'][6]79y4 = obj['poly'][7]8081# width, height = xmax - xmin, ymax - ymin82single_obj['bbox'] = x1, y1, x2, y2, x3, y3, x4, y483single_obj['image_id'] = image_id84data_dict['annotations'].append(single_obj)85single_obj['id'] = inst_count86inst_count = inst_count + 187image_id = image_id + 188json.dump(data_dict, f_out)8990def DOTA2COCOTest(srcpath, destfile, cls_names):91imageparent = os.path.join(srcpath, 'images')92data_dict = {}9394data_dict['images'] = []95data_dict['categories'] = []96for idex, name in enumerate(cls_names):97single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}98data_dict['categories'].append(single_cat)99100image_id = 1101with open(destfile, 'w') as f_out:102filenames = util.GetFileFromThisRootDir(imageparent)103for file in filenames:104basename = util.custombasename(file)105imagepath = os.path.join(imageparent, basename + '.png')106img = Image.open(imagepath)107height = img.height108width = img.width109110single_image = {}111single_image['file_name'] = basename + '.png'112single_image['id'] = image_id113single_image['width'] = width114single_image['height'] = height115data_dict['images'].append(single_image)116117image_id = image_id + 1118json.dump(data_dict, f_out)119120if __name__ == '__main__':121122DOTA2COCOTrain(r'data/dataset_demo_split',123osp.join('data/dataset_demo_split', 'train_datasetdemo.json'),124wordname_15)125print('Train DONE')126# DOTA2COCOTrain(r'/dataset/Dota/Dota_V1.0/train_sp/',127# r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Train/trainval_coco_8point.json',128# ucas_aod)129DOTA2COCOTest(r'data/dataset_demo_split',130osp.join('data/dataset_demo_split', 'test_datasetdemo.json'),131wordname_15)132# DOTA2COCOTest(r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Test/',133# r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Test/test_coco_8point.json',134# ucas_aod)135136print('Test DONE')137138139