forked from wkentaro/labelme
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
114 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import numpy as np | ||
|
||
from labelme.utils import draw as draw_module | ||
from labelme.utils import shape as shape_module | ||
|
||
from .util import get_img_and_lbl | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def test_label_colormap(): | ||
N = 255 | ||
colormap = draw_module.label_colormap(N=N) | ||
assert colormap.shape == (N, 3) | ||
|
||
|
||
def test_label2rgb(): | ||
img, lbl, label_names = get_img_and_lbl() | ||
n_labels = len(label_names) | ||
|
||
viz = draw_module.label2rgb(lbl=lbl, n_labels=n_labels) | ||
assert lbl.shape == viz.shape[:2] | ||
assert viz.dtype == np.uint8 | ||
|
||
viz = draw_module.label2rgb(lbl=lbl, img=img, n_labels=n_labels) | ||
assert img.shape[:2] == lbl.shape == viz.shape[:2] | ||
assert viz.dtype == np.uint8 | ||
|
||
|
||
def test_draw_label(): | ||
img, lbl, label_names = get_img_and_lbl() | ||
|
||
viz = draw_module.draw_label(lbl, img, label_names=label_names) | ||
assert viz.shape[:2] == img.shape[:2] == lbl.shape[:2] | ||
assert viz.dtype == np.uint8 | ||
|
||
|
||
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 = shape_module.masks_to_bboxes(masks) | ||
captions = [label_names[l] for l in labels] | ||
viz = draw_module.draw_instances(img, bboxes, labels, captions=captions) | ||
assert viz.shape[:2] == img.shape[:2] | ||
assert viz.dtype == np.uint8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os.path as osp | ||
|
||
import numpy as np | ||
import PIL.Image | ||
|
||
from labelme.utils import image as image_module | ||
|
||
from .util import data_dir | ||
from .util import get_img_and_data | ||
|
||
|
||
def test_img_b64_to_arr(): | ||
img, _ = get_img_and_data() | ||
assert img.dtype == np.uint8 | ||
assert img.shape == (907, 1210, 3) | ||
|
||
|
||
def test_img_arr_to_b64(): | ||
img_file = osp.join(data_dir, 'apc2016_obj3.jpg') | ||
img_arr = np.asarray(PIL.Image.open(img_file)) | ||
img_b64 = image_module.img_arr_to_b64(img_arr) | ||
img_arr2 = image_module.img_b64_to_arr(img_b64) | ||
np.testing.assert_allclose(img_arr, img_arr2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from .util import get_img_and_data | ||
|
||
from labelme.utils import shape as shape_module | ||
|
||
|
||
def test_shapes_to_label(): | ||
img, data = get_img_and_data() | ||
label_name_to_value = {} | ||
for shape in data['shapes']: | ||
label_name = shape['label'] | ||
label_value = len(label_name_to_value) | ||
label_name_to_value[label_name] = label_value | ||
cls = shape_module.shapes_to_label( | ||
img.shape, data['shapes'], label_name_to_value) | ||
assert cls.shape == img.shape[:2] | ||
|
||
|
||
def test_shape_to_mask(): | ||
img, data = get_img_and_data() | ||
for shape in data['shapes']: | ||
points = shape['points'] | ||
mask = shape_module.shape_to_mask(img.shape[:2], points) | ||
assert mask.shape == img.shape[:2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
import os.path as osp | ||
|
||
from labelme.utils import image as image_module | ||
from labelme.utils import shape as shape_module | ||
|
||
|
||
here = osp.dirname(osp.abspath(__file__)) | ||
data_dir = osp.join(here, '../data') | ||
|
||
|
||
def get_img_and_data(): | ||
json_file = osp.join(data_dir, 'apc2016_obj3.json') | ||
data = json.load(open(json_file)) | ||
img_b64 = data['imageData'] | ||
img = image_module.img_b64_to_arr(img_b64) | ||
return img, data | ||
|
||
|
||
def get_img_and_lbl(): | ||
img, data = get_img_and_data() | ||
|
||
label_name_to_value = {'__background__': 0} | ||
for shape in data['shapes']: | ||
label_name = shape['label'] | ||
label_value = len(label_name_to_value) | ||
label_name_to_value[label_name] = label_value | ||
|
||
n_labels = max(label_name_to_value.values()) + 1 | ||
label_names = [None] * n_labels | ||
for label_name, label_value in label_name_to_value.items(): | ||
label_names[label_value] = label_name | ||
|
||
lbl = shape_module.shapes_to_label( | ||
img.shape, data['shapes'], label_name_to_value | ||
) | ||
return img, lbl, label_names |
Empty file.