Skip to content

Commit a716355

Browse files
ncianeonmanovic
authored andcommitted
Add upload annotation function to cli (#958)
* add upload annotation function to cli * Update core.py Removing whitespace
1 parent e1c1a1b commit a716355

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

utils/cli/cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def main():
2323
'delete': CLI.tasks_delete,
2424
'ls': CLI.tasks_list,
2525
'frames': CLI.tasks_frame,
26-
'dump': CLI.tasks_dump}
26+
'dump': CLI.tasks_dump,
27+
'upload': CLI.tasks_upload}
2728
args = parser.parse_args()
2829
config_log(args.loglevel)
2930
with requests.Session() as session:

utils/cli/core/core.py

+20
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ def tasks_dump(self, task_id, fileformat, filename, **kwargs):
112112
with open(filename, 'wb') as fp:
113113
fp.write(response.content)
114114

115+
def tasks_upload(self, task_id, fileformat, filename, **kwargs):
116+
""" Upload annotations for a task in the specified format
117+
(e.g. 'YOLO ZIP 1.0')."""
118+
url = self.api.tasks_id_annotations_format(task_id, fileformat)
119+
while True:
120+
response = self.session.put(
121+
url,
122+
files={'annotation_file':open(filename, 'rb')}
123+
)
124+
response.raise_for_status()
125+
if response.status_code == 201:
126+
break
127+
128+
log.info('Upload job for Task ID {} \
129+
with annotation file {} finished'.format(task_id, filename))
130+
115131

116132
class CVAT_API_V1():
117133
""" Build parameterized API URLs """
@@ -135,6 +151,10 @@ def tasks_id_data(self, task_id):
135151
def tasks_id_frame_id(self, task_id, frame_id):
136152
return self.tasks_id(task_id) + '/frames/{}'.format(frame_id)
137153

154+
def tasks_id_annotations_format(self, task_id, fileformat):
155+
return self.tasks_id(task_id) + '/annotations?format={}' \
156+
.format(fileformat)
157+
138158
def tasks_id_annotations_filename(self, task_id, name, fileformat):
139159
return self.tasks_id(task_id) + '/annotations/{}?format={}' \
140160
.format(name, fileformat)

utils/cli/core/definition.py

+26
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,29 @@ def argparse(s):
208208
default='CVAT XML 1.1 for images',
209209
help='annotation format (default: %(default)s)'
210210
)
211+
212+
#######################################################################
213+
# Upload Annotations
214+
#######################################################################
215+
216+
upload_parser = task_subparser.add_parser(
217+
'upload',
218+
description='Upload annotations for a CVAT task.'
219+
)
220+
upload_parser.add_argument(
221+
'task_id',
222+
type=int,
223+
help='task ID'
224+
)
225+
upload_parser.add_argument(
226+
'filename',
227+
type=str,
228+
help='upload file'
229+
)
230+
upload_parser.add_argument(
231+
'--format',
232+
dest='fileformat',
233+
type=str,
234+
default='CVAT XML 1.1',
235+
help='annotation format (default: %(default)s)'
236+
)

0 commit comments

Comments
 (0)