Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
n_neg_ex = int(kwargs['background_ratio'] * len(pos_examples))
neg_examples = np.random.choice(neg_examples, n_neg_ex, replace=False).tolist()
tile_results = {k: tile_results.get(k) for k in pos_examples + neg_examples}
print('Using sparse mode; subselected {} background tiles'.format(n_neg_ex))
# write out labels as numpy arrays
labels_file = op.join(dest_folder, 'labels.npz')
print('Writing out labels to {}'.format(labels_file))
np.savez(labels_file, **tile_results)
# write out labels as GeoJSON or PNG
if ml_type == 'classification':
features = []
for tile, label in tile_results.items():
feat = feature(Tile(*[int(t) for t in tile.split('-')]))
features.append(Feature(geometry=feat['geometry'],
properties=dict(label=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')
if not op.isdir(label_folder):
makedirs(label_folder)
for tile, label in tile_results.items():
# if we have at least one bounding box label
if bool(label.shape[0]):
label_file = '{}.png'.format(tile)
img = Image.new('RGB', (256, 256))
draw = ImageDraw.Draw(img)
for box in label:
draw.rectangle(((box[0], box[1]), (box[2], box[3])), outline=class_color(box[4]))
print('Writing {}'.format(label_file))
with open(fname_csv, 'r') as csvfile:
with open(fname_geojson, 'w') as results:
reader = csv.reader(csvfile)
first_line = True
# Create a FeatureCollection
results.write('{"type":"FeatureCollection","features":[')
next(reader) # Skip header
for row in reader:
# Load as pygeotile using TMS coords
geot = tile_func(*[int(t) for t in row[0].split('-')])
# Create feature with mercantile
feat = feature(Tile(geot.google[0], geot.google[1], geot.zoom))
# Get class prediction confidences
pred = json.loads(','.join(row[1:]))
pred_red = list(map(lambda x: round(x, 2), pred))
if pred_red[thresh_ind] >= thresh:
# Add commas prior to any feature that isn't the first one
if first_line:
first_line = False
else:
results.write(',')
pred_obj = dict(zip(map(lambda x: 'p%s' % x,
range(len(pred_red))), pred_red))
results.write(json.dumps(Feature(geometry=feat['geometry'],
properties=pred_obj)))
col_ys = []
for i, line in enumerate(iter_lines(src)):
obj = json.loads(line)
if isinstance(obj, dict):
x, y, z = obj["tile"][:3]
props = obj.get("properties")
fid = obj.get("id")
elif isinstance(obj, list):
x, y, z = obj[:3]
props = {}
fid = None
else:
raise click.BadParameter("{0}".format(obj), param=input, param_hint="input")
feature = mercantile.feature(
(x, y, z),
fid=fid,
props=props,
projected=projected,
buffer=buffer,
precision=precision,
)
bbox = feature["bbox"]
w, s, e, n = bbox
col_xs.extend([w, e])
col_ys.extend([s, n])
if collect:
features.append(feature)
elif extents:
click.echo(" ".join(map(str, bbox)))