Skip to content

Commit

Permalink
Refactor tests/utils_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Mar 16, 2019
1 parent 9a8c729 commit 530ad3a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 114 deletions.
Empty file added tests/__init__.py
Empty file.
114 changes: 0 additions & 114 deletions tests/test_utils.py

This file was deleted.

Empty file added tests/utils_tests/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions tests/utils_tests/test_draw.py
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
23 changes: 23 additions & 0 deletions tests/utils_tests/test_image.py
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)
23 changes: 23 additions & 0 deletions tests/utils_tests/test_shape.py
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]
37 changes: 37 additions & 0 deletions tests/utils_tests/util.py
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 added tests/widgets_tests/__init__.py
Empty file.

0 comments on commit 530ad3a

Please sign in to comment.