瀏覽代碼

Add more tests with OpenDir

Kentaro Wada 5 年之前
父節點
當前提交
5bb577eb32

+ 10 - 2
labelme/testing.py

@@ -1,6 +1,7 @@
 import json
 import os.path as osp
 
+import imgviz
 import labelme.utils
 
 
@@ -12,10 +13,17 @@ def assert_labelfile_sanity(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)
+        parent_dir = osp.dirname(filename)
+        img_file = osp.join(parent_dir, data['imagePath'])
+        assert osp.exists(img_file)
+        img = imgviz.io.imread(img_file)
+    else:
+        img = labelme.utils.img_b64_to_arr(imageData)
 
     H, W = img.shape[:2]
+    assert H == data['imageHeight']
+    assert W == data['imageWidth']
+
     assert 'shapes' in data
     for shape in data['shapes']:
         assert 'label' in shape

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000003.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000003.jpg

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000003.json

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000003.json

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000006.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000006.jpg

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000006.json

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000006.json

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000025.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000025.jpg

+ 1 - 0
tests/labelme_tests/data/annotated/2011_000025.json

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000025.json

+ 1 - 0
tests/labelme_tests/data/annotated_with_data/apc2016_obj3.jpg

@@ -0,0 +1 @@
+../../../../examples/tutorial/apc2016_obj3.jpg

+ 1 - 0
tests/labelme_tests/data/annotated_with_data/apc2016_obj3.json

@@ -0,0 +1 @@
+../../../../examples/tutorial/apc2016_obj3.json

+ 0 - 1
tests/labelme_tests/data/apc2016_obj3.jpg

@@ -1 +0,0 @@
-../../../examples/tutorial/apc2016_obj3.jpg

+ 0 - 1
tests/labelme_tests/data/apc2016_obj3.json

@@ -1 +0,0 @@
-../../../examples/tutorial/apc2016_obj3.json

+ 1 - 0
tests/labelme_tests/data/raw/2011_000003.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000003.jpg

+ 1 - 0
tests/labelme_tests/data/raw/2011_000006.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000006.jpg

+ 1 - 0
tests/labelme_tests/data/raw/2011_000025.jpg

@@ -0,0 +1 @@
+../../../../examples/instance_segmentation/data_annotated/2011_000025.jpg

+ 58 - 26
tests/labelme_tests/test_app.py

@@ -11,6 +11,16 @@ here = osp.dirname(osp.abspath(__file__))
 data_dir = osp.join(here, 'data')
 
 
+def _win_show_and_wait_imageData(qtbot, win):
+    win.show()
+
+    def check_imageData():
+        assert hasattr(win, 'imageData')
+        assert win.imageData is not None
+
+    qtbot.waitUntil(check_imageData)  # wait for loadFile
+
+
 def test_MainWindow_open(qtbot):
     win = labelme.app.MainWindow()
     qtbot.addWidget(win)
@@ -18,53 +28,75 @@ def test_MainWindow_open(qtbot):
     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)
+def test_MainWindow_open_img(qtbot):
+    img_file = osp.join(data_dir, 'raw/2011_000003.jpg')
+    win = labelme.app.MainWindow(filename=img_file)
     qtbot.addWidget(win)
-    win.show()
+    _win_show_and_wait_imageData(qtbot, win)
     win.close()
 
 
+def test_MainWindow_open_json(qtbot):
+    json_files = [
+        osp.join(data_dir, 'annotated_with_data/apc2016_obj3.json'),
+        osp.join(data_dir, 'annotated/2011_000003.json'),
+    ]
+    for json_file in json_files:
+        labelme.testing.assert_labelfile_sanity(json_file)
+
+        win = labelme.app.MainWindow(filename=json_file)
+        qtbot.addWidget(win)
+        _win_show_and_wait_imageData(qtbot, win)
+        win.close()
+
+
+def test_MainWindow_open_dir(qtbot):
+    directory = osp.join(data_dir, 'raw')
+    win = labelme.app.MainWindow(filename=directory)
+    qtbot.addWidget(win)
+    _win_show_and_wait_imageData(qtbot, win)
+    return win
+
+
+def test_MainWindow_openNextImg(qtbot):
+    win = test_MainWindow_open_dir(qtbot)
+    win.openNextImg()
+
+
+def test_MainWindow_openPrevImg(qtbot):
+    win = test_MainWindow_open_dir(qtbot)
+    win.openNextImg()
+
+
 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_file = osp.join(tmp_dir, 'apc2016_obj3.json')
+    input_file = osp.join(data_dir, 'raw/2011_000003.jpg')
+    out_file = osp.join(tmp_dir, '2011_000003.json')
 
     config = labelme.config.get_default_config()
     win = labelme.app.MainWindow(
         config=config,
-        filename=filename,
-        output_file=output_file,
+        filename=input_file,
+        output_file=out_file,
     )
     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
+    _win_show_and_wait_imageData(qtbot, win)
 
-    label = 'shelf'
+    label = 'whole'
     points = [
-        (26, 70),
-        (176, 730),
-        (986, 742),
-        (1184, 102),
+        (100, 100),
+        (100, 238),
+        (400, 238),
+        (400, 100),
     ]
     shapes = [dict(
         label=label,
         points=points,
-        line_color=None,
-        fill_color=None,
         shape_type='polygon',
         flags={}
     )]
     win.loadLabels(shapes)
     win.saveFile()
 
-    labelme.testing.assert_labelfile_sanity(output_file)
+    labelme.testing.assert_labelfile_sanity(out_file)
+    shutil.rmtree(tmp_dir)

+ 2 - 2
tests/labelme_tests/utils_tests/test_image.py

@@ -16,7 +16,7 @@ def test_img_b64_to_arr():
 
 
 def test_img_arr_to_b64():
-    img_file = osp.join(data_dir, 'apc2016_obj3.jpg')
+    img_file = osp.join(data_dir, 'annotated_with_data/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)
@@ -24,7 +24,7 @@ def test_img_arr_to_b64():
 
 
 def test_img_data_to_png_data():
-    img_file = osp.join(data_dir, 'apc2016_obj3.jpg')
+    img_file = osp.join(data_dir, 'annotated_with_data/apc2016_obj3.jpg')
     with open(img_file, 'rb') as f:
         img_data = f.read()
     png_data = image_module.img_data_to_png_data(img_data)

+ 1 - 1
tests/labelme_tests/utils_tests/util.py

@@ -10,7 +10,7 @@ data_dir = osp.join(here, '../data')
 
 
 def get_img_and_data():
-    json_file = osp.join(data_dir, 'apc2016_obj3.json')
+    json_file = osp.join(data_dir, 'annotated_with_data/apc2016_obj3.json')
     data = json.load(open(json_file))
     img_b64 = data['imageData']
     img = image_module.img_b64_to_arr(img_b64)