diff --git a/labelme/__init__.py b/labelme/__init__.py index ad38eaac622178c0d48aad9ec471351da19d6a6c..84b71d3b4fe09b9b86eff82dff199edd4edadc32 100644 --- a/labelme/__init__.py +++ b/labelme/__init__.py @@ -1,3 +1,4 @@ # flake8: noqa +from labelme import testing from labelme import utils diff --git a/labelme/testing.py b/labelme/testing.py new file mode 100644 index 0000000000000000000000000000000000000000..587298ec75f36b7077242d02e0758e6692a601e6 --- /dev/null +++ b/labelme/testing.py @@ -0,0 +1,25 @@ +import json +import os.path as osp + +import labelme.utils + + +def assert_labelfile_sanity(filename): + assert osp.exists(filename) + + data = json.load(open(filename)) + + assert 'imagePath' in data + imageData = data.get('imageData', None) + if imageData is None: + assert osp.exists(data['imagePath']) + img = labelme.utils.img_b64_to_arr(imageData) + + H, W = img.shape[:2] + assert 'shapes' in data + for shape in data['shapes']: + assert 'label' in shape + assert 'points' in shape + for x, y in shape['points']: + assert 0 <= x <= W + assert 0 <= y <= H diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000000000000000000000000000000000000..42a8c4cc7a1c7e3a1eb97dcafea129d86be4090b --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,58 @@ +import os.path as osp +import shutil +import tempfile + +import labelme.app +import labelme.testing + + +here = osp.dirname(osp.abspath(__file__)) +data_dir = osp.join(here, 'data') + + +def test_MainWindow_open(qtbot): + win = labelme.app.MainWindow() + qtbot.addWidget(win) + win.show() + win.close() + + +def test_MainWindow_open_json(qtbot): + filename = osp.join(data_dir, 'apc2016_obj3.json') + labelme.testing.assert_labelfile_sanity(filename) + win = labelme.app.MainWindow(filename=filename) + qtbot.addWidget(win) + win.show() + win.close() + + +def test_MainWindow_annotate_jpg(qtbot): + tmp_dir = tempfile.mkdtemp() + filename = osp.join(tmp_dir, 'apc2016_obj3.jpg') + shutil.copy(osp.join(data_dir, 'apc2016_obj3.jpg'), + filename) + output = osp.join(tmp_dir, 'apc2016_obj3.json') + + win = labelme.app.MainWindow(filename=filename, output=output) + qtbot.addWidget(win) + win.show() + + def check_imageData(): + assert hasattr(win, 'imageData') + assert win.imageData is not None + + qtbot.waitUntil(check_imageData) # wait for loadFile + + label = 'shelf' + points = [ + (26, 70), + (176, 730), + (986, 742), + (1184, 102), + ] + shape = label, points, None, None + shapes = [shape] + win.loadLabels(shapes) + win.saveFile() + + labelme.testing.assert_labelfile_sanity(output) diff --git a/tests/test_labelDialog.py b/tests/test_labelDialog.py index 068acdb0492c5f0a93df5508f0a841a46cebe14e..15648e1f6d3d79b236413628d9b892059bd6c9e9 100644 --- a/tests/test_labelDialog.py +++ b/tests/test_labelDialog.py @@ -57,9 +57,9 @@ def test_LabelDialog_popUp(qtbot): # popUp(text='cat') def interact(): - qtbot.keyClick(widget.edit, QtCore.Qt.Key_P) # enter 'p' for 'person' - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) + qtbot.keyClick(widget.edit, QtCore.Qt.Key_P) # enter 'p' for 'person' # NOQA + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA QtCore.QTimer.singleShot(500, interact) text = widget.popUp('cat') @@ -68,8 +68,8 @@ def test_LabelDialog_popUp(qtbot): # popUp() def interact(): - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA QtCore.QTimer.singleShot(500, interact) text = widget.popUp() @@ -78,9 +78,9 @@ def test_LabelDialog_popUp(qtbot): # popUp() + key_Up def interact(): - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Up) # 'person' -> 'dog' - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) - qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Up) # 'person' -> 'dog' # NOQA + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA + qtbot.keyClick(widget.edit, QtCore.Qt.Key_Enter) # NOQA QtCore.QTimer.singleShot(500, interact) text = widget.popUp()