-
Notifications
You must be signed in to change notification settings - Fork 0
/
wv_util.py
147 lines (118 loc) · 5.17 KB
/
wv_util.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Copyright 2018 Defense Innovation Unit Experimental
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from PIL import Image
import numpy as np
import json
from tqdm import tqdm
"""
xView processing helper functions for use in data processing.
"""
def scale(x,range1=(0,0),range2=(0,0)):
"""
Linear scaling for a value x
"""
return range2[0]*(1 - (x-range1[0]) / (range1[1]-range1[0])) + range2[1]*((x-range1[0]) / (range1[1]-range1[0]))
def get_image(fname):
"""
Get an image from a filepath in ndarray format
"""
return np.array(Image.open(fname))
def get_labels(fname):
"""
Gets label data from a geojson label file
Args:
fname: file path to an xView geojson label file
Output:
Returns three arrays: coords, chips, and classes corresponding to the
coordinates, file-names, and classes for each ground truth.
"""
with open(fname) as f:
data = json.load(f)
coords = np.zeros((len(data['features']),4))
chips = np.zeros((len(data['features'])),dtype="object")
classes = np.zeros((len(data['features'])))
for i in tqdm(range(len(data['features']))):
if data['features'][i]['properties']['bounds_imcoords'] != []:
b_id = data['features'][i]['properties']['image_id']
val = np.array([int(num) for num in data['features'][i]['properties']['bounds_imcoords'].split(",")])
chips[i] = b_id
classes[i] = data['features'][i]['properties']['type_id']
if val.shape[0] != 4:
print("Issues at %d!" % i)
else:
coords[i] = val
else:
chips[i] = 'None'
return coords, chips, classes
def boxes_from_coords(coords):
"""
Processes a coordinate array from a geojson into (xmin,ymin,xmax,ymax) format
Args:
coords: an array of bounding box coordinates
Output:
Returns an array of shape (N,4) with coordinates in proper format
"""
nc = np.zeros((coords.shape[0],4))
for ind in range(coords.shape[0]):
x1,x2 = coords[ind,:,0].min(),coords[ind,:,0].max()
y1,y2 = coords[ind,:,1].min(),coords[ind,:,1].max()
nc[ind] = [x1,y1,x2,y2]
return nc
def chip_image(img,coords,classes,shape=(300,300)):
"""
Chip an image and get relative coordinates and classes. Bounding boxes that pass into
multiple chips are clipped: each portion that is in a chip is labeled. For example,
half a building will be labeled if it is cut off in a chip. If there are no boxes,
the boxes array will be [[0,0,0,0]] and classes [0].
Note: This chip_image method is only tested on xView data-- there are some image manipulations that can mess up different images.
Args:
img: the image to be chipped in array format
coords: an (N,4) array of bounding box coordinates for that image
classes: an (N,1) array of classes for each bounding box
shape: an (W,H) tuple indicating width and height of chips
Output:
An image array of shape (M,W,H,C), where M is the number of chips,
W and H are the dimensions of the image, and C is the number of color
channels. Also returns boxes and classes dictionaries for each corresponding chip.
"""
height,width,_ = img.shape
wn,hn = shape
w_num,h_num = (int(width/wn),int(height/hn))
images = np.zeros((w_num*h_num,hn,wn,3))
total_boxes = {}
total_classes = {}
k = 0
for i in range(w_num):
for j in range(h_num):
x = np.logical_or( np.logical_and((coords[:,0]<((i+1)*wn)),(coords[:,0]>(i*wn))),
np.logical_and((coords[:,2]<((i+1)*wn)),(coords[:,2]>(i*wn))))
out = coords[x]
y = np.logical_or( np.logical_and((out[:,1]<((j+1)*hn)),(out[:,1]>(j*hn))),
np.logical_and((out[:,3]<((j+1)*hn)),(out[:,3]>(j*hn))))
outn = out[y]
out = np.transpose(np.vstack((np.clip(outn[:,0]-(wn*i),0,wn),
np.clip(outn[:,1]-(hn*j),0,hn),
np.clip(outn[:,2]-(wn*i),0,wn),
np.clip(outn[:,3]-(hn*j),0,hn))))
box_classes = classes[x][y]
if out.shape[0] != 0:
total_boxes[k] = out
total_classes[k] = box_classes
else:
total_boxes[k] = np.array([[0,0,0,0]])
total_classes[k] = np.array([0])
chip = img[hn*j:hn*(j+1),wn*i:wn*(i+1),:3]
images[k]=chip
k = k + 1
return images.astype(np.uint8),total_boxes,total_classes