main.py 4.4 KB

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