main.py 5.6 KB

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