main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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('--version', '-V', action='store_true',
  15. help='show version')
  16. parser.add_argument('filename', nargs='?', help='image or label filename')
  17. parser.add_argument('--output', '-O', '-o', help='output label name')
  18. default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
  19. parser.add_argument(
  20. '--config',
  21. dest='config_file',
  22. help='config file (default: %s)' % default_config_file,
  23. default=default_config_file,
  24. )
  25. # config for the gui
  26. parser.add_argument(
  27. '--nodata',
  28. dest='store_data',
  29. action='store_false',
  30. help='stop storing image data to JSON file',
  31. default=argparse.SUPPRESS,
  32. )
  33. parser.add_argument(
  34. '--autosave',
  35. dest='auto_save',
  36. action='store_true',
  37. help='auto save',
  38. default=argparse.SUPPRESS,
  39. )
  40. parser.add_argument(
  41. '--nosortlabels',
  42. dest='sort_labels',
  43. action='store_false',
  44. help='stop sorting labels',
  45. default=argparse.SUPPRESS,
  46. )
  47. parser.add_argument(
  48. '--flags',
  49. help='comma separated list of flags OR file containing flags',
  50. default=argparse.SUPPRESS,
  51. )
  52. parser.add_argument(
  53. '--labels',
  54. help='comma separated list of labels OR file containing labels',
  55. default=argparse.SUPPRESS,
  56. )
  57. parser.add_argument(
  58. '--validatelabel',
  59. dest='validate_label',
  60. choices=['exact', 'instance'],
  61. help='label validation types',
  62. default=argparse.SUPPRESS,
  63. )
  64. parser.add_argument(
  65. '--keep-prev',
  66. action='store_true',
  67. help='keep annotation of previous frame',
  68. default=argparse.SUPPRESS,
  69. )
  70. parser.add_argument(
  71. '--epsilon',
  72. type=float,
  73. help='epsilon to find nearest vertex on canvas',
  74. default=argparse.SUPPRESS,
  75. )
  76. args = parser.parse_args()
  77. if args.version:
  78. print('{0} {1}'.format(__appname__, __version__))
  79. sys.exit(0)
  80. if hasattr(args, 'flags'):
  81. if os.path.isfile(args.flags):
  82. with codecs.open(args.flags, 'r', encoding='utf-8') as f:
  83. args.flags = [l.strip() for l in f if l.strip()]
  84. else:
  85. args.flags = [l for l in args.flags.split(',') if l]
  86. if hasattr(args, 'labels'):
  87. if os.path.isfile(args.labels):
  88. with codecs.open(args.labels, 'r', encoding='utf-8') as f:
  89. args.labels = [l.strip() for l in f if l.strip()]
  90. else:
  91. args.labels = [l for l in args.labels.split(',') if l]
  92. config_from_args = args.__dict__
  93. config_from_args.pop('version')
  94. filename = config_from_args.pop('filename')
  95. output = config_from_args.pop('output')
  96. config_file = config_from_args.pop('config_file')
  97. config = get_config(config_from_args, config_file)
  98. if not config['labels'] and config['validate_label']:
  99. logger.error('--labels must be specified with --validatelabel or '
  100. 'validate_label: true in the config file '
  101. '(ex. ~/.labelmerc).')
  102. sys.exit(1)
  103. app = QtWidgets.QApplication(sys.argv)
  104. app.setApplicationName(__appname__)
  105. app.setWindowIcon(newIcon('icon'))
  106. win = MainWindow(config=config, filename=filename, output=output)
  107. win.show()
  108. win.raise_()
  109. sys.exit(app.exec_())