diff --git a/label_maker/label.py b/label_maker/label.py index b491ad6..d0d94a2 100644 --- a/label_maker/label.py +++ b/label_maker/label.py @@ -128,9 +128,14 @@ def make_labels(dest_folder, zoom, country, classes, ml_type, bounding_box, spar if ml_type == 'classification': features = [] for tile, label in tile_results.items(): + label_bool = [int(bool(l)) for l in label] + # if there are no classes, activate the background + if all(v == 0 for v in label_bool): + label_bool[0] = 1 feat = feature(Tile(*[int(t) for t in tile.split('-')])) features.append(Feature(geometry=feat['geometry'], - properties=dict(label=label.tolist()))) + properties=dict(label=label_bool, + label_area=label.tolist()))) json.dump(fc(features), open(op.join(dest_folder, 'classification.geojson'), 'w')) elif ml_type == 'object-detection': label_folder = op.join(dest_folder, 'labels') @@ -159,7 +164,7 @@ def make_labels(dest_folder, zoom, country, classes, ml_type, bounding_box, spar print('Writing {}'.format(label_file)) img.save(op.join(label_folder, label_file)) - + def _mapper(x, y, z, data, args): """Iterate over OSM QA Tiles and return a label for each tile @@ -197,14 +202,15 @@ def _mapper(x, y, z, data, args): if tile['osm']['features']: if ml_type == 'classification': - class_counts = np.zeros(len(classes) + 1, dtype=np.int) - for i, cl in enumerate(classes): - ff = create_filter(cl.get('filter')) - class_counts[i + 1] = int(bool([f for f in tile['osm']['features'] if ff(f)])) - # if there are no classes, activate the background - if np.sum(class_counts) == 0: - class_counts[0] = 1 - return ('{!s}-{!s}-{!s}'.format(x, y, z), class_counts) + class_areas = np.zeros(len(classes) + 1) + for feat in tile['osm']['features']: + for i, cl in enumerate(classes): + ff = create_filter(cl.get('filter')) + if ff(feat): + feat['geometry']['coordinates'] = _convert_coordinates(feat['geometry']['coordinates']) + geo = shape(feat['geometry']) + class_areas[i + 1] = geo.area + return ('{!s}-{!s}-{!s}'.format(x, y, z), class_areas) elif ml_type == 'object-detection': bboxes = _create_empty_label(ml_type, classes) for feat in tile['osm']['features']: @@ -305,7 +311,7 @@ def _tile_results_summary(ml_type, classes): cl_tiles = len([l for l in labels if len(list(filter(_bbox_class(i + 1), l)))]) # pylint: disable=cell-var-from-loop print('{}: {} features in {} tiles'.format(cl.get('name'), cl_features, cl_tiles)) elif ml_type == 'classification': - class_tile_counts = list(np.sum(labels, axis=0)) + class_tile_counts = list(np.count_nonzero(labels, axis=0)) for i, cl in enumerate(classes): print('{}: {} tiles'.format(cl.get('name'), int(class_tile_counts[i + 1]))) elif ml_type == 'segmentation': @@ -318,7 +324,6 @@ def _tile_results_summary(ml_type, classes): def _create_empty_label(ml_type, classes): if ml_type == 'classification': label = np.zeros(len(classes) + 1, dtype=np.int) - label[0] = 1 return label elif ml_type == 'object-detection': return np.empty((0, 5), dtype=np.int)