main.py 4.8 KB

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