test_app.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. def create_MainWindow_with_directory(qtbot):
  42. directory = osp.join(data_dir, "raw")
  43. win = labelme.app.MainWindow(filename=directory)
  44. qtbot.addWidget(win)
  45. _win_show_and_wait_imageData(qtbot, win)
  46. return win
  47. @pytest.mark.gui
  48. def test_MainWindow_openNextImg(qtbot):
  49. win = create_MainWindow_with_directory(qtbot)
  50. win.openNextImg()
  51. @pytest.mark.gui
  52. def test_MainWindow_openPrevImg(qtbot):
  53. win = create_MainWindow_with_directory(qtbot)
  54. win.openNextImg()
  55. @pytest.mark.gui
  56. def test_MainWindow_annotate_jpg(qtbot):
  57. tmp_dir = tempfile.mkdtemp()
  58. input_file = osp.join(data_dir, "raw/2011_000003.jpg")
  59. out_file = osp.join(tmp_dir, "2011_000003.json")
  60. config = labelme.config.get_default_config()
  61. win = labelme.app.MainWindow(
  62. config=config,
  63. filename=input_file,
  64. output_file=out_file,
  65. )
  66. qtbot.addWidget(win)
  67. _win_show_and_wait_imageData(qtbot, win)
  68. label = "whole"
  69. points = [
  70. (100, 100),
  71. (100, 238),
  72. (400, 238),
  73. (400, 100),
  74. ]
  75. shapes = [
  76. dict(
  77. label=label,
  78. group_id=None,
  79. points=points,
  80. shape_type="polygon",
  81. mask=None,
  82. flags={},
  83. other_data={},
  84. )
  85. ]
  86. win.loadLabels(shapes)
  87. win.saveFile()
  88. labelme.testing.assert_labelfile_sanity(out_file)
  89. shutil.rmtree(tmp_dir)