test_app.py 2.4 KB

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