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