main.py 5.3 KB

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