-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
core.py
320 lines (275 loc) · 12.5 KB
/
core.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT
import json
import logging
import os
import requests
from io import BytesIO
import mimetypes
from time import sleep
from PIL import Image
from .definition import ResourceType
log = logging.getLogger(__name__)
class CLI():
def __init__(self, session, api, credentials):
self.api = api
self.session = session
self.login(credentials)
def tasks_data(self, task_id, resource_type, resources, **kwargs):
""" Add local, remote, or shared files to an existing task. """
url = self.api.tasks_id_data(task_id)
data = {}
files = None
if resource_type == ResourceType.LOCAL:
files = {'client_files[{}]'.format(i): open(f, 'rb') for i, f in enumerate(resources)}
elif resource_type == ResourceType.REMOTE:
data = {'remote_files[{}]'.format(i): f for i, f in enumerate(resources)}
elif resource_type == ResourceType.SHARE:
data = {'server_files[{}]'.format(i): f for i, f in enumerate(resources)}
data['image_quality'] = 50
## capture additional kwargs
if 'image_quality' in kwargs:
data['image_quality'] = kwargs.get('image_quality')
if 'frame_step' in kwargs:
data['frame_filter'] = f"step={kwargs.get('frame_step')}"
response = self.session.post(url, data=data, files=files)
response.raise_for_status()
def tasks_list(self, use_json_output, **kwargs):
""" List all tasks in either basic or JSON format. """
url = self.api.tasks
response = self.session.get(url)
response.raise_for_status()
output = []
page = 1
json_data_list = []
while True:
response_json = response.json()
output += response_json['results']
for r in response_json['results']:
if use_json_output:
json_data_list.append(r)
else:
log.info('{id},{name},{status}'.format(**r))
if not response_json['next']:
log.info(json.dumps(json_data_list, indent=4))
return output
page += 1
url = self.api.tasks_page(page)
response = self.session.get(url)
response.raise_for_status()
return output
def tasks_create(self, name, labels, overlap, segment_size, bug, resource_type, resources,
annotation_path='', annotation_format='CVAT XML 1.1',
completion_verification_period=20,
git_completion_verification_period=2,
dataset_repository_url='',
project_id=None,
lfs=False, **kwargs):
""" Create a new task with the given name and labels JSON and
add the files to it. """
url = self.api.tasks
labels = [] if project_id is not None else labels
data = {'name': name,
'labels': labels,
'overlap': overlap,
'segment_size': segment_size,
'bug_tracker': bug,
}
if project_id:
data.update({'project_id': project_id})
response = self.session.post(url, json=data)
response.raise_for_status()
response_json = response.json()
log.info('Created task ID: {id} NAME: {name}'.format(**response_json))
task_id = response_json['id']
self.tasks_data(task_id, resource_type, resources, **kwargs)
if annotation_path != '':
url = self.api.tasks_id_status(task_id)
response = self.session.get(url)
response_json = response.json()
log.info('Awaiting data compression before uploading annotations...')
while response_json['state'] != 'Finished':
sleep(completion_verification_period)
response = self.session.get(url)
response_json = response.json()
logger_string= '''Awaiting compression for task {}.
Status={}, Message={}'''.format(task_id,
response_json['state'],
response_json['message'])
log.info(logger_string)
self.tasks_upload(task_id, annotation_format, annotation_path, **kwargs)
if dataset_repository_url:
response = self.session.post(
self.api.git_create(task_id),
json={
'path': dataset_repository_url,
'lfs': lfs,
'tid': task_id})
response_json = response.json()
rq_id = response_json['rq_id']
log.info(f"Create RQ ID: {rq_id}")
check_url = self.api.git_check(rq_id)
response = self.session.get(check_url)
response_json = response.json()
while response_json['status'] != 'finished':
log.info('''Awaiting a dataset repository to be created for the task. Response status: {}'''.format(
response_json['status']))
sleep(git_completion_verification_period)
response = self.session.get(check_url)
response_json = response.json()
if response_json['status'] == 'failed' or response_json['status'] == 'unknown':
log.error(f'Dataset repository creation request for task {task_id} failed'
f'with status {response_json["status"]}.')
break
log.info(f"Dataset repository creation completed with status: {response_json['status']}.")
def tasks_delete(self, task_ids, **kwargs):
""" Delete a list of tasks, ignoring those which don't exist. """
for task_id in task_ids:
url = self.api.tasks_id(task_id)
response = self.session.delete(url)
try:
response.raise_for_status()
log.info('Task ID {} deleted'.format(task_id))
except requests.exceptions.HTTPError as e:
if response.status_code == 404:
log.info('Task ID {} not found'.format(task_id))
else:
raise e
def tasks_frame(self, task_id, frame_ids, outdir='', quality='original', **kwargs):
""" Download the requested frame numbers for a task and save images as
task_<ID>_frame_<FRAME>.jpg."""
for frame_id in frame_ids:
url = self.api.tasks_id_frame_id(task_id, frame_id, quality)
response = self.session.get(url)
response.raise_for_status()
im = Image.open(BytesIO(response.content))
mime_type = im.get_format_mimetype() or 'image/jpg'
im_ext = mimetypes.guess_extension(mime_type)
# FIXME It is better to use meta information from the server
# to determine the extension
# replace '.jpe' or '.jpeg' with a more used '.jpg'
if im_ext == '.jpe' or '.jpeg' or None:
im_ext = '.jpg'
outfile = 'task_{}_frame_{:06d}{}'.format(task_id, frame_id, im_ext)
im.save(os.path.join(outdir, outfile))
def tasks_dump(self, task_id, fileformat, filename, **kwargs):
""" Download annotations for a task in the specified format
(e.g. 'YOLO ZIP 1.0')."""
url = self.api.tasks_id(task_id)
response = self.session.get(url)
response.raise_for_status()
response_json = response.json()
url = self.api.tasks_id_annotations_filename(task_id,
response_json['name'],
fileformat)
while True:
response = self.session.get(url)
response.raise_for_status()
log.info('STATUS {}'.format(response.status_code))
if response.status_code == 201:
break
response = self.session.get(url + '&action=download')
response.raise_for_status()
with open(filename, 'wb') as fp:
fp.write(response.content)
def tasks_upload(self, task_id, fileformat, filename, **kwargs):
""" Upload annotations for a task in the specified format
(e.g. 'YOLO ZIP 1.0')."""
url = self.api.tasks_id_annotations_format(task_id, fileformat)
while True:
response = self.session.put(
url,
files={'annotation_file': open(filename, 'rb')}
)
response.raise_for_status()
if response.status_code == 201:
break
logger_string = "Upload job for Task ID {} ".format(task_id) +\
"with annotation file {} finished".format(filename)
log.info(logger_string)
def tasks_export(self, task_id, filename, export_verification_period=3, **kwargs):
""" Export and download a whole task """
url = self.api.tasks_id(task_id)
export_url = url + '?action=export'
while True:
response = self.session.get(export_url)
response.raise_for_status()
log.info('STATUS {}'.format(response.status_code))
if response.status_code == 201:
break
sleep(export_verification_period)
response = self.session.get(url + '?action=download')
response.raise_for_status()
with open(filename, 'wb') as fp:
fp.write(response.content)
logger_string = "Task {} has been exported sucessfully. ".format(task_id) +\
"to {}".format(os.path.abspath(filename))
log.info(logger_string)
def tasks_import(self, filename, import_verification_period=3, **kwargs):
""" Import a task"""
url = self.api.tasks + '?action=import'
with open(filename, 'rb') as input_file:
response = self.session.post(
url,
files={'task_file': input_file}
)
response.raise_for_status()
response_json = response.json()
rq_id = response_json['rq_id']
while True:
sleep(import_verification_period)
response = self.session.post(
url,
data={'rq_id': rq_id}
)
response.raise_for_status()
if response.status_code == 201:
break
task_id = response.json()['id']
logger_string = "Task has been imported sucessfully. Task ID: {}".format(task_id)
log.info(logger_string)
def login(self, credentials):
url = self.api.login
auth = {'username': credentials[0], 'password': credentials[1]}
response = self.session.post(url, auth)
response.raise_for_status()
if 'csrftoken' in response.cookies:
self.session.headers['X-CSRFToken'] = response.cookies['csrftoken']
class CVAT_API_V1():
""" Build parameterized API URLs """
def __init__(self, host, https=False):
if host.startswith('https://'):
https = True
if host.startswith('http://') or host.startswith('https://'):
host = host.replace('http://', '')
host = host.replace('https://', '')
scheme = 'https' if https else 'http'
self.base = '{}://{}/api/v1/'.format(scheme, host)
self.git = f'{scheme}://{host}/git/repository/'
@property
def tasks(self):
return self.base + 'tasks'
def tasks_page(self, page_id):
return self.tasks + '?page={}'.format(page_id)
def tasks_id(self, task_id):
return self.tasks + '/{}'.format(task_id)
def tasks_id_data(self, task_id):
return self.tasks_id(task_id) + '/data'
def tasks_id_frame_id(self, task_id, frame_id, quality):
return self.tasks_id(task_id) + '/data?type=frame&number={}&quality={}'.format(frame_id, quality)
def tasks_id_status(self, task_id):
return self.tasks_id(task_id) + '/status'
def tasks_id_annotations_format(self, task_id, fileformat):
return self.tasks_id(task_id) + '/annotations?format={}' \
.format(fileformat)
def tasks_id_annotations_filename(self, task_id, name, fileformat):
return self.tasks_id(task_id) + '/annotations?format={}&filename={}' \
.format(fileformat, name)
def git_create(self, task_id):
return self.git + f'create/{task_id}'
def git_check(self, rq_id):
return self.git + f'check/{rq_id}'
@property
def login(self):
return self.base + 'auth/login'