main.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. try:
  16. _main()
  17. except Exception as e:
  18. logger.error(e)
  19. def _main():
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument(
  22. '--version', '-V', action='store_true', help='show version'
  23. )
  24. parser.add_argument(
  25. '--reset-config', action='store_true', help='reset qt config'
  26. )
  27. parser.add_argument(
  28. '--logger-level',
  29. default='info',
  30. choices=['debug', 'info', 'warning', 'fatal', 'error'],
  31. help='logger level',
  32. )
  33. parser.add_argument('filename', nargs='?', help='image or label filename')
  34. parser.add_argument(
  35. '--output',
  36. '-O',
  37. '-o',
  38. help='output file or directory (if it ends with .json it is '
  39. 'recognized as file, else as directory)'
  40. )
  41. default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
  42. parser.add_argument(
  43. '--config',
  44. dest='config_file',
  45. help='config file (default: %s)' % default_config_file,
  46. default=default_config_file,
  47. )
  48. # config for the gui
  49. parser.add_argument(
  50. '--nodata',
  51. dest='store_data',
  52. action='store_false',
  53. help='stop storing image data to JSON file',
  54. default=argparse.SUPPRESS,
  55. )
  56. parser.add_argument(
  57. '--autosave',
  58. dest='auto_save',
  59. action='store_true',
  60. help='auto save',
  61. default=argparse.SUPPRESS,
  62. )
  63. parser.add_argument(
  64. '--nosortlabels',
  65. dest='sort_labels',
  66. action='store_false',
  67. help='stop sorting labels',
  68. default=argparse.SUPPRESS,
  69. )
  70. parser.add_argument(
  71. '--flags',
  72. help='comma separated list of flags OR file containing flags',
  73. default=argparse.SUPPRESS,
  74. )
  75. parser.add_argument(
  76. '--labelflags',
  77. dest='label_flags',
  78. help='yaml string of label specific flags OR file containing json string of label specific flags (ex. {human:[male,female],dog:[big],__all__:[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. # Add not overlapping labels from label flags
  129. if not hasattr(args, 'labels'):
  130. args.labels = []
  131. for label in args.label_flags.keys():
  132. if label != "__all__" and label not in args.labels:
  133. args.labels.append(label)
  134. config_from_args = args.__dict__
  135. config_from_args.pop('version')
  136. reset_config = config_from_args.pop('reset_config')
  137. filename = config_from_args.pop('filename')
  138. output = config_from_args.pop('output')
  139. config_file = config_from_args.pop('config_file')
  140. config = get_config(config_from_args, config_file)
  141. if not config['labels'] and config['validate_label']:
  142. logger.error('--labels must be specified with --validatelabel or '
  143. 'validate_label: true in the config file '
  144. '(ex. ~/.labelmerc).')
  145. sys.exit(1)
  146. output_file = None
  147. output_dir = None
  148. if output is not None:
  149. if output.endswith('.json'):
  150. output_file = output
  151. else:
  152. output_dir = output
  153. app = QtWidgets.QApplication(sys.argv)
  154. app.setApplicationName(__appname__)
  155. app.setWindowIcon(newIcon('icon'))
  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()