main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import argparse
  2. import codecs
  3. import os
  4. import sys
  5. from qtpy import QtWidgets
  6. from labelme import __appname__
  7. from labelme import __version__
  8. from labelme.app import MainWindow
  9. from labelme.config import get_config
  10. from labelme import logger
  11. from labelme.utils import newIcon
  12. def main():
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument(
  15. '--version', '-V', action='store_true', help='show version'
  16. )
  17. parser.add_argument(
  18. '--reset-config', action='store_true', help='reset qt config'
  19. )
  20. parser.add_argument('filename', nargs='?', help='image or label filename')
  21. parser.add_argument('--output', '-O', '-o', help='output label name')
  22. default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
  23. parser.add_argument(
  24. '--config',
  25. dest='config_file',
  26. help='config file (default: %s)' % default_config_file,
  27. default=default_config_file,
  28. )
  29. # config for the gui
  30. parser.add_argument(
  31. '--nodata',
  32. dest='store_data',
  33. action='store_false',
  34. help='stop storing image data to JSON file',
  35. default=argparse.SUPPRESS,
  36. )
  37. parser.add_argument(
  38. '--autosave',
  39. dest='auto_save',
  40. action='store_true',
  41. help='auto save',
  42. default=argparse.SUPPRESS,
  43. )
  44. parser.add_argument(
  45. '--nosortlabels',
  46. dest='sort_labels',
  47. action='store_false',
  48. help='stop sorting labels',
  49. default=argparse.SUPPRESS,
  50. )
  51. parser.add_argument(
  52. '--flags',
  53. help='comma separated list of flags OR file containing flags',
  54. default=argparse.SUPPRESS,
  55. )
  56. parser.add_argument(
  57. '--labels',
  58. help='comma separated list of labels OR file containing labels',
  59. default=argparse.SUPPRESS,
  60. )
  61. parser.add_argument(
  62. '--validatelabel',
  63. dest='validate_label',
  64. choices=['exact', 'instance'],
  65. help='label validation types',
  66. default=argparse.SUPPRESS,
  67. )
  68. parser.add_argument(
  69. '--keep-prev',
  70. action='store_true',
  71. help='keep annotation of previous frame',
  72. default=argparse.SUPPRESS,
  73. )
  74. parser.add_argument(
  75. '--epsilon',
  76. type=float,
  77. help='epsilon to find nearest vertex on canvas',
  78. default=argparse.SUPPRESS,
  79. )
  80. args = parser.parse_args()
  81. if args.version:
  82. print('{0} {1}'.format(__appname__, __version__))
  83. sys.exit(0)
  84. if hasattr(args, 'flags'):
  85. if os.path.isfile(args.flags):
  86. with codecs.open(args.flags, 'r', encoding='utf-8') as f:
  87. args.flags = [l.strip() for l in f if l.strip()]
  88. else:
  89. args.flags = [l for l in args.flags.split(',') if l]
  90. if hasattr(args, 'labels'):
  91. if os.path.isfile(args.labels):
  92. with codecs.open(args.labels, 'r', encoding='utf-8') as f:
  93. args.labels = [l.strip() for l in f if l.strip()]
  94. else:
  95. args.labels = [l for l in args.labels.split(',') if l]
  96. config_from_args = args.__dict__
  97. config_from_args.pop('version')
  98. reset_config = config_from_args.pop('reset_config')
  99. filename = config_from_args.pop('filename')
  100. output = config_from_args.pop('output')
  101. config_file = config_from_args.pop('config_file')
  102. config = get_config(config_from_args, config_file)
  103. if not config['labels'] and config['validate_label']:
  104. logger.error('--labels must be specified with --validatelabel or '
  105. 'validate_label: true in the config file '
  106. '(ex. ~/.labelmerc).')
  107. sys.exit(1)
  108. app = QtWidgets.QApplication(sys.argv)
  109. app.setApplicationName(__appname__)
  110. app.setWindowIcon(newIcon('icon'))
  111. win = MainWindow(config=config, filename=filename, output=output)
  112. if reset_config:
  113. print('Resetting Qt config: %s' % win.settings.fileName())
  114. win.settings.clear()
  115. sys.exit(0)
  116. win.show()
  117. win.raise_()
  118. sys.exit(app.exec_())
  119. if __name__ == '__main__':
  120. main()