main.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. _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='comma separated list of label specific flags OR file containing flags',
  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, 'label_flags'):
  117. if os.path.isfile(args.label_flags):
  118. with codecs.open(args.label_flags, 'r', encoding='utf-8') as f:
  119. args.label_flags = [l.strip() for l in f if l.strip()]
  120. else:
  121. args.label_flags = [l for l in args.label_flags.split(',') if l]
  122. if hasattr(args, 'labels'):
  123. if os.path.isfile(args.labels):
  124. with codecs.open(args.labels, 'r', encoding='utf-8') as f:
  125. args.labels = [l.strip() for l in f if l.strip()]
  126. else:
  127. args.labels = [l for l in args.labels.split(',') if l]
  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. app = QtWidgets.QApplication(sys.argv)
  148. app.setApplicationName(__appname__)
  149. app.setWindowIcon(newIcon('icon'))
  150. win = MainWindow(
  151. config=config,
  152. filename=filename,
  153. output_file=output_file,
  154. output_dir=output_dir,
  155. )
  156. if reset_config:
  157. logger.info('Resetting Qt config: %s' % win.settings.fileName())
  158. win.settings.clear()
  159. sys.exit(0)
  160. win.show()
  161. win.raise_()
  162. sys.exit(app.exec_())
  163. # this main block is required to generate executable by pyinstaller
  164. if __name__ == '__main__':
  165. main()