CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/DOTA_devkit/DOTA2COCO.py
Views: 475
1
import dota_utils as util
2
import os
3
import cv2
4
import json
5
from PIL import Image
6
import os.path as osp
7
8
wordname_15 = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
9
'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']
10
11
wordname_16 = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
12
'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter', 'container-crane']
13
14
wordname_18 = [ 'plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship',
15
'tennis-court', 'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor',
16
'swimming-pool', 'helicopter', 'container-crane', 'airport', 'helipad']
17
18
hrsc_2016 = ['ship']
19
ucas_aod = ['car', 'airplane']
20
21
def DOTA2COCOTrain(srcpath, destfile, cls_names, difficult='2'):
22
# set difficult to filter '2', '1', or do not filter, set '-1'
23
24
imageparent = os.path.join(srcpath, 'images')
25
labelparent = os.path.join(srcpath, 'labelTxt')
26
27
data_dict = {}
28
data_dict['images'] = []
29
data_dict['categories'] = []
30
data_dict['annotations'] = []
31
for idex, name in enumerate(cls_names):
32
single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
33
data_dict['categories'].append(single_cat)
34
35
inst_count = 1
36
image_id = 1
37
with open(destfile, 'w') as f_out:
38
filenames = util.GetFileFromThisRootDir(labelparent)
39
for file in filenames:
40
basename = util.custombasename(file)
41
# image_id = int(basename[1:])
42
43
imagepath = os.path.join(imageparent, basename + '.png')
44
img = cv2.imread(imagepath)
45
height, width, c = img.shape
46
47
single_image = {}
48
single_image['file_name'] = basename + '.png'
49
single_image['id'] = image_id
50
single_image['width'] = width
51
single_image['height'] = height
52
data_dict['images'].append(single_image)
53
54
# annotations
55
objects = util.parse_dota_poly2(file)
56
for obj in objects:
57
if obj['difficult'] == difficult:
58
print('difficult: ', difficult)
59
continue
60
single_obj = {}
61
single_obj['area'] = obj['area']
62
63
if obj['name'] not in cls_names: # 去掉别的类
64
print('This classname not found: ', obj['name'])
65
continue
66
67
single_obj['category_id'] = cls_names.index(obj['name']) + 1
68
single_obj['segmentation'] = []
69
single_obj['segmentation'].append(obj['poly'])
70
single_obj['iscrowd'] = 0
71
# xmin, ymin, xmax, ymax = min(obj['poly'][0::2]), min(obj['poly'][1::2]), \
72
# max(obj['poly'][0::2]), max(obj['poly'][1::2])
73
x1 = obj['poly'][0]
74
y1 = obj['poly'][1]
75
x2 = obj['poly'][2]
76
y2 = obj['poly'][3]
77
x3 = obj['poly'][4]
78
y3 = obj['poly'][5]
79
x4 = obj['poly'][6]
80
y4 = obj['poly'][7]
81
82
# width, height = xmax - xmin, ymax - ymin
83
single_obj['bbox'] = x1, y1, x2, y2, x3, y3, x4, y4
84
single_obj['image_id'] = image_id
85
data_dict['annotations'].append(single_obj)
86
single_obj['id'] = inst_count
87
inst_count = inst_count + 1
88
image_id = image_id + 1
89
json.dump(data_dict, f_out)
90
91
def DOTA2COCOTest(srcpath, destfile, cls_names):
92
imageparent = os.path.join(srcpath, 'images')
93
data_dict = {}
94
95
data_dict['images'] = []
96
data_dict['categories'] = []
97
for idex, name in enumerate(cls_names):
98
single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
99
data_dict['categories'].append(single_cat)
100
101
image_id = 1
102
with open(destfile, 'w') as f_out:
103
filenames = util.GetFileFromThisRootDir(imageparent)
104
for file in filenames:
105
basename = util.custombasename(file)
106
imagepath = os.path.join(imageparent, basename + '.png')
107
img = Image.open(imagepath)
108
height = img.height
109
width = img.width
110
111
single_image = {}
112
single_image['file_name'] = basename + '.png'
113
single_image['id'] = image_id
114
single_image['width'] = width
115
single_image['height'] = height
116
data_dict['images'].append(single_image)
117
118
image_id = image_id + 1
119
json.dump(data_dict, f_out)
120
121
if __name__ == '__main__':
122
123
DOTA2COCOTrain(r'data/dataset_demo_split',
124
osp.join('data/dataset_demo_split', 'train_datasetdemo.json'),
125
wordname_15)
126
print('Train DONE')
127
# DOTA2COCOTrain(r'/dataset/Dota/Dota_V1.0/train_sp/',
128
# r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Train/trainval_coco_8point.json',
129
# ucas_aod)
130
DOTA2COCOTest(r'data/dataset_demo_split',
131
osp.join('data/dataset_demo_split', 'test_datasetdemo.json'),
132
wordname_15)
133
# DOTA2COCOTest(r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Test/',
134
# r'/mnt/SSD/lwt_workdir/s2anet_branch/data/ucas_aod/Test/test_coco_8point.json',
135
# ucas_aod)
136
137
print('Test DONE')
138
139