Skip to content

Commit

Permalink
Add test for labelme/app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 1, 2018
1 parent 657cd54 commit 6e1402b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
1 change: 1 addition & 0 deletions labelme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa

from labelme import testing
from labelme import utils
25 changes: 25 additions & 0 deletions labelme/testing.py
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 8 additions & 8 deletions tests/test_labelDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -68,8 +68,8 @@ def interact():
# 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()
Expand All @@ -78,9 +78,9 @@ def interact():
# 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()
Expand Down

0 comments on commit 6e1402b

Please sign in to comment.