-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeOBBAnnotations.py
76 lines (60 loc) · 2.5 KB
/
MakeOBBAnnotations.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
69
70
71
72
73
74
75
76
import json
import numpy as np
import os
import cv2
def polygon_to_obb(polygon):
"""
Convert polygon coordinates to an oriented bounding box (OBB).
Parameters:
polygon (list of tuples): List of (x, y) coordinates representing the polygon vertices.
Returns:
box (list of tuples): List of (x, y) coordinates representing the vertices of the OBB.
"""
# Convert the list of points to a numpy array for OpenCV
points = np.array(polygon, dtype=np.float32)
# Get the minimum area rectangle
rect = cv2.minAreaRect(points)
# Extract the box points from the rect, and convert them to integer
box = cv2.boxPoints(rect)
box = np.int0(box)
# Convert to list of tuples for easy readability
obb = [(int(point[0]), int(point[1])) for point in box]
return obb
# Change 'DATA_ROOT' into your own data path.
DATA_ROOT = '/nas2/YJ/DATA/WHUbuilding'
datasets = ['train', 'test', 'val']
for dataset in datasets :
print ("Transforming {} dataset".format(dataset))
with open(os.path.join(DATA_ROOT, './annotations/instances_{}.json'.format(dataset)), 'r') as f:
bldg_data = json.load(f)
Img_indexes = {}
for img_info in bldg_data['images'] :
Img_indexes[img_info['id']]=[]
for i, anno in enumerate(bldg_data['annotations']) :
img_id = anno['image_id']
Img_indexes[img_id].append(i)
ROOT = os.path.join(DATA_ROOT, 'OBB/{}'.format(dataset))
if not os.path.exists(ROOT) :
os.makedirs(ROOT)
for img_info in bldg_data['images'] :
img_id = img_info['id']
filename = os.path.join(ROOT, '{}.txt'.format(img_id))
with open(filename, 'w') as f :
for i in Img_indexes[img_id] :
anno = bldg_data['annotations'][i]
polygon = np.array(anno['segmentation'])
obb = np.array(polygon_to_obb(polygon.reshape(polygon.shape[1]//2,2))).reshape(-1)
bbox = np.array(anno['bbox'])
area = bbox[2] * bbox[3]
if area < 1024 :
difficulty = 2
elif area < 9216 :
difficulty = 1
else :
difficulty = 0
outputs = ''
for coord in obb :
outputs += '{} '.format(coord)
outputs += 'building {}\n'.format(difficulty)
f.write(outputs)
print ('{} files are completed'.format(len(bldg_data['images'])))