Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_draw_instances():
img, lbl, label_names = _get_img_and_lbl()
labels_and_masks = {l: lbl == l for l in np.unique(lbl) if l != 0}
labels, masks = zip(*labels_and_masks.items())
masks = np.asarray(masks)
bboxes = labelme.utils.masks_to_bboxes(masks)
captions = [label_names[l] for l in labels]
viz = labelme.utils.draw_instances(img, bboxes, labels, captions=captions)
assert viz.shape[:2] == img.shape[:2]
assert viz.dtype == np.uint8
maker.name(class_name), # label name
maker.pose(""), # pose info, doesn't matter
maker.truncated("0"), # truncated info, doesn't matter
maker.difficult("0"), # diificulty, doesn't matter
maker.bndbox( # bbox(up-left corner and bottom-right corner points)
maker.xmin(str(xmin)),
maker.ymin(str(ymin)),
maker.xmax(str(xmax)),
maker.ymax(str(ymax)),
),
)
)
# caption for visualize drawing
captions = [class_names[l] for l in labels]
viz = labelme.utils.draw_instances(
img, bboxes, labels, captions=captions
)
PIL.Image.fromarray(viz).save(out_viz_file)
# another visualize format (colored mask in bbox)
label_name_to_value = {'_background_': 0}
for shape in sorted(region['objects'], key=lambda x: x['name']):
label_name = shape['name']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
maker.name(class_name), # label name
maker.pose(""), # pose info, doesn't matter
maker.truncated("0"), # truncated info, doesn't matter
maker.difficult("0"), # diificulty, doesn't matter
maker.bndbox( # bbox(up-left corner and bottom-right corner points)
maker.xmin(str(xmin)),
maker.ymin(str(ymin)),
maker.xmax(str(xmax)),
maker.ymax(str(ymax)),
),
)
)
# caption for visualize drawing
captions = [class_names[l] for l in labels]
viz = labelme.utils.draw_instances(
img, bboxes, labels, captions=captions
)
PIL.Image.fromarray(viz).save(out_viz_file)
# another visualize format (colored mask in bbox)
label_name_to_value = {'_background_': 0}
for shape in sorted(data['shapes'], key=lambda x: x['label']):
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value