main.py 4.4 KB

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