-
Notifications
You must be signed in to change notification settings - Fork 5
/
label.py
402 lines (339 loc) · 12.8 KB
/
label.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import json
import os
from datetime import datetime
import logging
import geojson
from geojson import FeatureCollection
from PIL import Image, ImageDraw
from shapely.geometry import MultiPolygon, Polygon, box, shape
from shapely.strtree import STRtree
import itertools
import shutil
import tqdm
from ohsome2label.palette import palette
from ohsome2label.tile import (
Bbox,
Tile,
apply_transform,
get_bbox,
tile_get_transform,
xy,
)
from ohsome2label.utils import get_area
nx = 256
ny = 256
log = logging.getLogger(__name__)
class TaskError(Exception):
"""Wrong task"""
pass
def bounds_to_bbox(bounds):
(minx, miny, maxx, maxy) = bounds
bbox = [(minx, miny), (maxx, miny), (maxx, maxy), (minx, maxy), (minx, miny)]
return bbox
def gen_anno(coords, idx, imgIdx, catIdx):
anno = {}
anno["id"] = idx
anno["category_id"] = catIdx
anno["iscrowd"] = 0
seg = []
anno["image_id"] = imgIdx
xs = [x for x, _ in coords]
ys = [y for _, y in coords]
for _x, _y in zip(xs, ys):
seg.append(_x)
seg.append(_y)
anno["segmentation"] = [seg]
anno["area"] = get_area(xs, ys)
anno["bbox"] = [min(xs), min(ys), max(xs) - min(xs), max(ys) - min(ys)]
# log.info((imgIdx, catIdx))
return anno
def expolde_multipolygon(multipoly):
coords_list = []
for poly in multipoly:
coords = list(poly.exterior.coords)
coords_list.append(coords)
return coords_list
def trunc_polygon(coords, nx=256, ny=256):
"""truncate the polygon according to the tile size
:param coords: image-based coordinates
:param nx: image width
:param ny: image length
"""
xs = [x for x, _ in coords]
xs = [_ if _ > 0 else 0 for _ in xs]
xs = [_ if _ < nx else nx for _ in xs]
ys = [y for _, y in coords]
ys = [_ if _ > 0 else 0 for _ in ys]
ys = [_ if _ < ny else ny for _ in ys]
return [(x, y) for x, y in zip(xs, ys)]
def parse_polygon(coordinates, trans, nx=256, ny=256):
"""parse polygon
:param coordinates: wgs84 coordinates
:param trans: translation to get image-based coordinate system coordinate
:param nx: image width
:param ny: image length
"""
exterior = coordinates[0]
exterior = [xy(*coord) for coord in exterior]
exterior = trunc_polygon((apply_transform(exterior, trans)), nx, ny)
if len(coordinates) > 1:
interiors = coordinates[1:]
interiors = [[xy(*coord) for coord in interior] for interior in interiors]
interiors = [apply_transform(interior, trans) for interior in interiors]
else:
interiors = []
return Polygon(exterior, interiors).buffer(0)
def check_topo(feats, tile, nx=256, ny=256):
"""check features geometry, due to coco cannot recognize segmentation with
holes, return sorted list of (geometry, label) tuple
:param feats: geojson feature
:param tile: tile of the feature
:param nx: image width
:param ny: image length
"""
trans = tile_get_transform(tile, nx, ny)
bbox = box(0, 0, 255, 255)
# geoms element is (geometry, label) tuple
geoms = []
# construct tile-based coordinate-system shapely geometry
idx = 0
for feat in feats:
idx += 1
geometry = feat["geometry"]
label = feat["properties"]["label"]
if geometry["type"] == "Polygon":
coordinates = geometry["coordinates"]
poly = parse_polygon(coordinates, trans, nx, ny)
# log.info(label)
geoms.append((poly.area, poly, label))
elif feat["geometry"]["type"] == "MultiPolygon":
coordinates = geometry["coordinates"]
try:
poly = MultiPolygon(
[parse_polygon(coords, trans, nx, ny) for coords in coordinates]
)
geoms.append((poly.area, poly, label))
except Exception:
assert 0
# put larger area geometry on the top of list
geoms.sort(key=lambda x: x[0], reverse=True)
# Due to coco not support segmentation with holes.
# So the exterior will be used as the segmentation
for _, geom, label in geoms:
geom = bbox.intersection(geom).buffer(0)
if geom.area > 0:
yield (geom, label)
def burn_tile(geoms, task, pal, fname, nx=256, ny=256):
"""Burn a tile
:param geoms: (geom, label) tuple
:param pal: palette
:param fname: path to store the output image
:param nx: image width
:param ny: image length
"""
draws = []
im = Image.new(mode="RGB", size=(nx, ny), color="#000000")
draw = ImageDraw.Draw(im)
i = 0
for geom, label in geoms:
i += 1
if task == "segmentation":
if geom.type == "Polygon":
draws.append((list(geom.exterior.coords), label))
elif geom.type == "MultiPolygon":
draws += [(list(g.exterior.coords), label) for g in geom]
elif task == "object detection":
bbox = bounds_to_bbox(geom.bounds)
draws.append((bbox, label))
else:
raise TaskError
if i != 0:
for idx, (coords, label) in enumerate(draws):
if task == "segmentation":
draw.polygon(coords, fill=pal.color(label))
elif task == "object detection":
draw.line(coords, fill=pal.color(label))
yield (idx, label, coords)
# log.warning("draw num:{}".format(idx))
im.save(fname, "PNG")
def get_tile_list(cfg, workspace):
"""Get potential label list according to the osm geojson
:param cfg: ohsome2label config
:param workspace: workspace
"""
tile_dir = workspace.tile
geoms = []
feats = {}
tile_feats = {}
# open downloaded geojson file
if cfg.api == "ohsome":
for tag in cfg.tags:
fname = "{lab}_{k}_{v}.geojson".format(
lab=tag["label"], k=tag["key"], v=tag["value"]
)
fpath = os.path.join(workspace.raw, fname)
# get valid tile
with open(fpath, encoding="utf-8") as f:
data = geojson.loads(f.read().replace("'", ""))
features = data["features"]
for feature in features:
geom = shape(feature["geometry"])
geoms.append(geom)
feature["properties"]["label"] = tag["label"]
feats[geom.to_wkb()] = feature
elif cfg.api == "overpass":
fname = "overpass_query.geojson"
fpath = os.path.join(workspace.raw, fname)
with open(fpath, encoding="utf-8") as f:
data = geojson.loads(f.read().replace("'", ""))
features = data["features"]
for feature in features:
for tag in cfg.tags:
key = tag.get("key", "")
value = tag.get("value", "")
if value == "" and key in feature["properties"]:
geom = shape(feature["geometry"])
geoms.append(geom)
feature["properties"]["label"] = tag["label"]
feats[geom.to_wkb()] = feature
break
elif feature["properties"].get(key, "") == value:
geom = shape(feature["geometry"])
geoms.append(geom)
feature["properties"]["label"] = tag["label"]
feats[geom.to_wkb()] = feature
break
tree = STRtree(geoms)
# clip by tile into small tile geojson
for t in cfg.tiles:
_box = box(*get_bbox(t))
r = tree.query(_box)
if len(r) != 0:
tile = tile_feats.get(t, [])
for g in r:
tile.append(feats[g.to_wkb()])
tile_feats[t] = tile
# free the variable for gc
del feats
list_path = os.path.join(workspace.other, "tile_list")
with open(list_path, "w", encoding="utf-8") as f:
for imgIdx, tile in enumerate(tqdm.tqdm(tile_feats)):
feats = tile_feats[tile]
# store geojson
fc = FeatureCollection(feats)
tile_name = "{0.z}.{0.x}.{0.y}".format(tile)
f.write(tile_name + "\n")
tile_path = os.path.join(tile_dir, tile_name + ".geojson")
with open(tile_path, "w", encoding="utf-8") as gj:
try:
geojson.dump(fc, gj)
except Exception:
log.error("{}.geojson dump wrong!".format(tile_name))
assert 0
class geococo(object):
""" class for generate geo coco
https://www.immersivelimit.com/tutorials/create-coco-annotations-from-scratch
"""
def __init__(self, config):
self.tags_to_cats(config.tags)
# coco info
info = {}
info["description"] = config.name
info["date_created"] = datetime.now().strftime("%Y/%m/%d")
self.info = info
# coco licenses
self.lic = [
{
"url": "http://creativecommons.org/licenses/by/2.0/",
"id": 4,
"name": "Attribution License",
}
]
self.imgs = []
self.annos = []
def __enter__(self):
return self
def __exit__(self, *exc):
return True
def tags_to_cats(self, tags):
"""Convert tags to coco categories."""
self.catIdxs = {}
self.cats = []
_label = {}
for tag in tags:
if tag["label"] not in _label:
_label[tag["label"]] = len(_label)
for tag in tags:
cat = {}
cat["supercategory"] = tag["label"]
cat["id"] = _label[tag["label"]] + 1
cat["name"] = tag["label"]
if cat in self.cats:
continue
else:
self.cats.append(cat)
self.catIdxs[tag["label"]] = cat["id"]
def to_json(self):
coco = {}
coco["info"] = self.info
coco["licenses"] = self.lic
coco["images"] = self.imgs
coco["annotations"] = self.annos
coco["categories"] = self.cats
return coco
def gen_label(cfg, workspace):
"""Generate label and annotations in coco format
:param cfg: ohsome2label config
:param workspace: workspace
"""
tmp_dir = workspace.tmp
img_dir = workspace.img
label_dir = workspace.label
tile_dir = workspace.tile
list_path = os.path.join(workspace.other, "tile_list")
geoms = []
feats = {}
pal = palette(cfg.tags, os.path.join(workspace.other, "colors"))
cocoPath = os.path.join(workspace.anno, "geococo.json")
with open(list_path, "r", encoding="utf-8") as lf:
with open(cocoPath, "w", encoding="utf-8") as f:
with geococo(cfg) as coco:
imgIdx = 0
for tile_name in tqdm.tqdm(os.listdir(tmp_dir)):
zoom, tx, ty = [_ for _ in tile_name.split(".")[:3]]
tile = Tile(int(tx), int(ty), int(zoom))
geojson_path = os.path.join(
tile_dir, tile_name.replace("png", "geojson")
)
label_path = os.path.join(label_dir, tile_name)
with open(geojson_path) as gj:
feats = geojson.loads(gj.read().replace("'", ""))["features"]
img = {}
img["id"] = imgIdx
img["width"] = nx
img["height"] = ny
img["file_name"] = tile_name
geoms = check_topo(feats, tile, nx, ny)
burned_feats = burn_tile(
geoms, cfg.task, pal, label_path, nx, ny
)
burned_feats, _ = itertools.tee(burned_feats)
if sum(1 for x in _) == 0:
log.info("{} has no geometry, skip it!".format(tile))
else:
coco.imgs.append(img)
base_idx = len(coco.annos)
for idx, label, coords in burned_feats:
catIdx = coco.catIdxs[label]
try:
coco.annos.append(
gen_anno(coords, base_idx + idx, imgIdx, catIdx)
)
except Exception:
log.warning(imgIdx)
imgIdx += 1
shutil.copy(
os.path.join(tmp_dir, tile_name),
os.path.join(img_dir, tile_name),
)
json.dump(coco.to_json(), f, indent=2)