test_app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os.path as osp
  2. import shutil
  3. import tempfile
  4. import pytest
  5. import labelme.app
  6. import labelme.config
  7. import labelme.testing
  8. here = osp.dirname(osp.abspath(__file__))
  9. data_dir = osp.join(here, "data")
  10. def _win_show_and_wait_imageData(qtbot, win):
  11. win.show()
  12. def check_imageData():
  13. assert hasattr(win, "imageData")
  14. assert win.imageData is not None
  15. qtbot.waitUntil(check_imageData) # wait for loadFile
  16. @pytest.mark.gui
  17. def test_MainWindow_open(qtbot):
  18. win = labelme.app.MainWindow()
  19. qtbot.addWidget(win)
  20. win.show()
  21. win.close()
  22. @pytest.mark.gui
  23. def test_MainWindow_open_img(qtbot):
  24. img_file = osp.join(data_dir, "raw/2011_000003.jpg")
  25. win = labelme.app.MainWindow(filename=img_file)
  26. qtbot.addWidget(win)
  27. _win_show_and_wait_imageData(qtbot, win)
  28. win.close()
  29. @pytest.mark.gui
  30. def test_MainWindow_open_json(qtbot):
  31. json_files = [
  32. osp.join(data_dir, "annotated_with_data/apc2016_obj3.json"),
  33. osp.join(data_dir, "annotated/2011_000003.json"),
  34. ]
  35. for json_file in json_files:
  36. labelme.testing.assert_labelfile_sanity(json_file)
  37. win = labelme.app.MainWindow(filename=json_file)
  38. qtbot.addWidget(win)
  39. _win_show_and_wait_imageData(qtbot, win)
  40. win.close()
  41. @pytest.mark.gui
  42. def test_MainWindow_open_dir(qtbot):
  43. directory = osp.join(data_dir, "raw")
  44. win = labelme.app.MainWindow(filename=directory)
  45. qtbot.addWidget(win)
  46. _win_show_and_wait_imageData(qtbot, win)
  47. return win
  48. @pytest.mark.gui
  49. def test_MainWindow_openNextImg(qtbot):
  50. win = test_MainWindow_open_dir(qtbot)
  51. win.openNextImg()
  52. @pytest.mark.gui
  53. def test_MainWindow_openPrevImg(qtbot):
  54. win = test_MainWindow_open_dir(qtbot)
  55. win.openNextImg()
  56. @pytest.mark.gui
  57. def test_MainWindow_annotate_jpg(qtbot):
  58. tmp_dir = tempfile.mkdtemp()
  59. input_file = osp.join(data_dir, "raw/2011_000003.jpg")
  60. out_file = osp.join(tmp_dir, "2011_000003.json")
  61. config = labelme.config.get_default_config()
  62. win = labelme.app.MainWindow(
  63. config=config, filename=input_file, output_file=out_file,
  64. )
  65. qtbot.addWidget(win)
  66. _win_show_and_wait_imageData(qtbot, win)
  67. label = "whole"
  68. points = [
  69. (100, 100),
  70. (100, 238),
  71. (400, 238),
  72. (400, 100),
  73. ]
  74. shapes = [
  75. dict(
  76. label=label,
  77. group_id=None,
  78. points=points,
  79. shape_type="polygon",
  80. flags={},
  81. other_data={},
  82. )
  83. ]
  84. win.loadLabels(shapes)
  85. win.saveFile()
  86. labelme.testing.assert_labelfile_sanity(out_file)
  87. shutil.rmtree(tmp_dir)