-
Notifications
You must be signed in to change notification settings - Fork 39
/
DOTA2COCO.py
68 lines (60 loc) · 2.76 KB
/
DOTA2COCO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import dota_utils as util
import os
import cv2
import json
wordname_15 = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']
def DOTA2COCO(srcpath, destfile):
imageparent = os.path.join(srcpath, 'images')
labelparent = os.path.join(srcpath, 'labelTxt')
data_dict = {}
info = {'contributor': 'captain group',
'data_created': '2018',
'description': 'This is 1.0 version of DOTA dataset.',
'url': 'http://captain.whu.edu.cn/DOTAweb/',
'version': '1.0',
'year': 2018}
data_dict['info'] = info
data_dict['images'] = []
data_dict['categories'] = []
data_dict['annotations'] = []
for idex, name in enumerate(wordname_15):
single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
data_dict['categories'].append(single_cat)
inst_count = 1
image_id = 1
with open(destfile, 'w') as f_out:
filenames = util.GetFileFromThisRootDir(labelparent)
for file in filenames:
basename = util.custombasename(file)
# image_id = int(basename[1:])
imagepath = os.path.join(imageparent, basename + '.png')
img = cv2.imread(imagepath)
height, width, c = img.shape
single_image = {}
single_image['file_name'] = basename + '.png'
single_image['id'] = image_id
single_image['width'] = width
single_image['height'] = height
data_dict['images'].append(single_image)
# annotations
objects = util.parse_dota_poly2(file)
for obj in objects:
single_obj = {}
single_obj['area'] = obj['area']
single_obj['category_id'] = wordname_15.index(obj['name']) + 1
single_obj['segmentation'] = []
single_obj['segmentation'].append(obj['poly'])
single_obj['iscrowd'] = 0
xmin, ymin, xmax, ymax = min(obj['poly'][0::2]), min(obj['poly'][1::2]), \
max(obj['poly'][0::2]), max(obj['poly'][1::2])
width, height = xmax - xmin, ymax - ymin
single_obj['bbox'] = xmin, ymin, width, height
single_obj['image_id'] = image_id
data_dict['annotations'].append(single_obj)
single_obj['id'] = inst_count
inst_count = inst_count + 1
image_id = image_id + 1
json.dump(data_dict, f_out)
if __name__ == '__main__':
DOTA2COCO(r'/data0/data_dj/1024_new', r'/data0/data_dj/1024_new/DOTA_trainval1024.json')