-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtest_tasks.py
2776 lines (2388 loc) · 105 KB
/
test_tasks.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022-2023 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import io
import json
import os
import os.path as osp
import zipfile
from copy import deepcopy
from functools import partial
from http import HTTPStatus
from itertools import chain, product
from math import ceil
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory
from time import sleep, time
from typing import Any, List, Optional, Tuple
import pytest
from cvat_sdk import Client, Config, exceptions
from cvat_sdk.api_client import models
from cvat_sdk.api_client.api_client import ApiClient, ApiException, Endpoint
from cvat_sdk.core.helpers import get_paginated_collection
from cvat_sdk.core.progress import NullProgressReporter
from cvat_sdk.core.proxies.tasks import ResourceType, Task
from cvat_sdk.core.uploading import Uploader
from deepdiff import DeepDiff
from PIL import Image
import shared.utils.s3 as s3
from shared.fixtures.init import docker_exec_cvat, kube_exec_cvat
from shared.utils.config import (
BASE_URL,
USER_PASS,
delete_method,
get_method,
make_api_client,
patch_method,
post_method,
)
from shared.utils.helpers import generate_image_file, generate_image_files, generate_manifest
from .utils import (
CollectionSimpleFilterTestBase,
compare_annotations,
create_task,
export_dataset,
wait_until_task_is_created,
)
def get_cloud_storage_content(username: str, cloud_storage_id: int, manifest: Optional[str] = None):
with make_api_client(username) as api_client:
kwargs = {"manifest_path": manifest} if manifest else {}
(data, _) = api_client.cloudstorages_api.retrieve_content_v2(cloud_storage_id, **kwargs)
return [f"{f['name']}{'/' if str(f['type']) == 'DIR' else ''}" for f in data["content"]]
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetTasks:
def _test_task_list_200(self, user, project_id, data, exclude_paths="", **kwargs):
with make_api_client(user) as api_client:
results = get_paginated_collection(
api_client.tasks_api.list_endpoint,
return_json=True,
project_id=project_id,
**kwargs,
)
assert DeepDiff(data, results, ignore_order=True, exclude_paths=exclude_paths) == {}
def _test_users_to_see_task_list(
self, project_id, tasks, users, is_staff, is_allow, is_project_staff, **kwargs
):
if is_staff:
users = [user for user in users if is_project_staff(user["id"], project_id)]
else:
users = [user for user in users if not is_project_staff(user["id"], project_id)]
assert len(users)
for user in users:
if not is_allow:
# Users outside project or org should not know if one exists.
# Thus, no error should be produced on a list request.
tasks = []
self._test_task_list_200(user["username"], project_id, tasks, **kwargs)
def _test_assigned_users_to_see_task_data(self, tasks, users, is_task_staff, **kwargs):
for task in tasks:
staff_users = [user for user in users if is_task_staff(user["id"], task["id"])]
assert len(staff_users)
for user in staff_users:
with make_api_client(user["username"]) as api_client:
(_, response) = api_client.tasks_api.list(**kwargs)
assert response.status == HTTPStatus.OK
response_data = json.loads(response.data)
assert any(_task["id"] == task["id"] for _task in response_data["results"])
@pytest.mark.parametrize("project_id", [1])
@pytest.mark.parametrize(
"groups, is_staff, is_allow",
[
("admin", False, True),
("business", False, False),
],
)
def test_project_tasks_visibility(
self, project_id, groups, users, tasks, is_staff, is_allow, find_users, is_project_staff
):
users = find_users(privilege=groups)
tasks = list(filter(lambda x: x["project_id"] == project_id, tasks))
assert len(tasks)
self._test_users_to_see_task_list(
project_id, tasks, users, is_staff, is_allow, is_project_staff
)
@pytest.mark.parametrize("project_id, groups", [(1, "user")])
def test_task_assigned_to_see_task(
self, project_id, groups, users, tasks, find_users, is_task_staff
):
users = find_users(privilege=groups)
tasks = list(filter(lambda x: x["project_id"] == project_id and x["assignee"], tasks))
assert len(tasks)
self._test_assigned_users_to_see_task_data(tasks, users, is_task_staff)
@pytest.mark.parametrize("org, project_id", [({"id": 2, "slug": "org2"}, 2)])
@pytest.mark.parametrize(
"role, is_staff, is_allow",
[
("maintainer", False, True),
("supervisor", False, False),
],
)
def test_org_project_tasks_visibility(
self,
org,
project_id,
role,
is_staff,
is_allow,
tasks,
is_task_staff,
is_project_staff,
find_users,
):
users = find_users(org=org["id"], role=role)
tasks = list(filter(lambda x: x["project_id"] == project_id, tasks))
assert len(tasks)
self._test_users_to_see_task_list(
project_id, tasks, users, is_staff, is_allow, is_project_staff, org=org["slug"]
)
@pytest.mark.parametrize("org, project_id, role", [({"id": 2, "slug": "org2"}, 2, "worker")])
def test_org_task_assigneed_to_see_task(
self, org, project_id, role, users, tasks, find_users, is_task_staff
):
users = find_users(org=org["id"], role=role)
tasks = list(filter(lambda x: x["project_id"] == project_id and x["assignee"], tasks))
assert len(tasks)
self._test_assigned_users_to_see_task_data(tasks, users, is_task_staff, org=org["slug"])
@pytest.mark.usefixtures("restore_db_per_function")
def test_can_get_job_validation_summary(self, admin_user, tasks, jobs):
task = next(t for t in tasks if t["jobs"]["count"] > 0 if t["jobs"]["validation"] == 0)
job = next(j for j in jobs if j["task_id"] == task["id"])
with make_api_client(admin_user) as api_client:
api_client.jobs_api.partial_update(
job["id"],
patched_job_write_request=models.PatchedJobWriteRequest(stage="validation"),
)
(server_task, _) = api_client.tasks_api.retrieve(task["id"])
assert server_task.jobs.validation == 1
@pytest.mark.usefixtures("restore_db_per_function")
def test_can_get_job_completed_summary(self, admin_user, tasks, jobs):
task = next(t for t in tasks if t["jobs"]["count"] > 0 if t["jobs"]["completed"] == 0)
job = next(j for j in jobs if j["task_id"] == task["id"])
with make_api_client(admin_user) as api_client:
api_client.jobs_api.partial_update(
job["id"],
patched_job_write_request=models.PatchedJobWriteRequest(
state="completed", stage="acceptance"
),
)
(server_task, _) = api_client.tasks_api.retrieve(task["id"])
assert server_task.jobs.completed == 1
def test_can_remove_owner_and_fetch_with_sdk(self, admin_user, tasks):
# test for API schema regressions
source_task = next(
t for t in tasks if t.get("owner") and t["owner"]["username"] != admin_user
).copy()
with make_api_client(admin_user) as api_client:
api_client.users_api.destroy(source_task["owner"]["id"])
(_, response) = api_client.tasks_api.retrieve(source_task["id"])
fetched_task = json.loads(response.data)
source_task["owner"] = None
assert DeepDiff(source_task, fetched_task, ignore_order=True) == {}
class TestListTasksFilters(CollectionSimpleFilterTestBase):
field_lookups = {
"owner": ["owner", "username"],
"assignee": ["assignee", "username"],
"tracker_link": ["bug_tracker"],
}
@pytest.fixture(autouse=True)
def setup(self, restore_db_per_class, admin_user, tasks):
self.user = admin_user
self.samples = tasks
def _get_endpoint(self, api_client: ApiClient) -> Endpoint:
return api_client.tasks_api.list_endpoint
@pytest.mark.parametrize(
"field",
(
"name",
"owner",
"status",
"assignee",
"subset",
"mode",
"dimension",
"project_id",
"tracker_link",
),
)
def test_can_use_simple_filter_for_object_list(self, field):
return super().test_can_use_simple_filter_for_object_list(field)
@pytest.mark.usefixtures("restore_db_per_function")
class TestPostTasks:
def _test_create_task_201(self, user, spec, **kwargs):
with make_api_client(user) as api_client:
(_, response) = api_client.tasks_api.create(spec, **kwargs)
assert response.status == HTTPStatus.CREATED
return response
def _test_create_task_403(self, user, spec, **kwargs):
with make_api_client(user) as api_client:
(_, response) = api_client.tasks_api.create(
spec, **kwargs, _parse_response=False, _check_status=False
)
assert response.status == HTTPStatus.FORBIDDEN
return response
def _test_users_to_create_task_in_project(
self, project_id, users, is_staff, is_allow, is_project_staff, **kwargs
):
if is_staff:
users = [user for user in users if is_project_staff(user["id"], project_id)]
else:
users = [user for user in users if not is_project_staff(user["id"], project_id)]
assert len(users)
for user in users:
username = user["username"]
spec = {
"name": f"test {username} to create a task within a project",
"project_id": project_id,
}
if is_allow:
self._test_create_task_201(username, spec, **kwargs)
else:
self._test_create_task_403(username, spec, **kwargs)
@pytest.mark.parametrize("project_id", [1])
@pytest.mark.parametrize(
"groups, is_staff, is_allow",
[
("admin", False, True),
("business", False, False),
("user", True, True),
],
)
def test_users_to_create_task_in_project(
self, project_id, groups, is_staff, is_allow, is_project_staff, find_users
):
users = find_users(privilege=groups)
self._test_users_to_create_task_in_project(
project_id, users, is_staff, is_allow, is_project_staff
)
@pytest.mark.parametrize("org, project_id", [({"id": 2, "slug": "org2"}, 2)])
@pytest.mark.parametrize(
"role, is_staff, is_allow",
[
("worker", False, False),
],
)
def test_worker_cannot_create_task_in_project_without_ownership(
self, org, project_id, role, is_staff, is_allow, is_project_staff, find_users
):
users = find_users(org=org["id"], role=role)
self._test_users_to_create_task_in_project(
project_id, users, is_staff, is_allow, is_project_staff, org=org["slug"]
)
def test_create_response_matches_get(self, admin_user):
username = admin_user
spec = {"name": "test create task", "labels": [{"name": "a"}]}
response = self._test_create_task_201(username, spec)
task = json.loads(response.data)
with make_api_client(username) as api_client:
(_, response) = api_client.tasks_api.retrieve(task["id"])
assert DeepDiff(task, json.loads(response.data), ignore_order=True) == {}
def test_can_create_task_with_skeleton(self, admin_user):
username = admin_user
spec = {
"name": f"test admin1 to create a task with skeleton",
"labels": [
{
"name": "s1",
"color": "#5c5eba",
"attributes": [
{
"name": "color",
"mutable": False,
"input_type": "select",
"default_value": "white",
"values": ["white", "black"],
}
],
"type": "skeleton",
"sublabels": [
{
"name": "1",
"color": "#d53957",
"attributes": [
{
"id": 23,
"name": "attr",
"mutable": False,
"input_type": "select",
"default_value": "val1",
"values": ["val1", "val2"],
}
],
"type": "points",
},
{"name": "2", "color": "#4925ec", "attributes": [], "type": "points"},
{"name": "3", "color": "#59a8fe", "attributes": [], "type": "points"},
],
"svg": '<line x1="36.329429626464844" y1="45.98662185668945" x2="59.07190704345703" y2="23.076923370361328" '
'stroke="black" data-type="edge" data-node-from="2" stroke-width="0.5" data-node-to="3"></line>'
'<line x1="22.61705780029297" y1="25.75250816345215" x2="36.329429626464844" y2="45.98662185668945" '
'stroke="black" data-type="edge" data-node-from="1" stroke-width="0.5" data-node-to="2"></line>'
'<circle r="1.5" stroke="black" fill="#b3b3b3" cx="22.61705780029297" cy="25.75250816345215" '
'stroke-width="0.1" data-type="element node" data-element-id="1" data-node-id="1" data-label-name="1">'
'</circle><circle r="1.5" stroke="black" fill="#b3b3b3" cx="36.329429626464844" cy="45.98662185668945" '
'stroke-width="0.1" data-type="element node" data-element-id="2" data-node-id="2" data-label-name="2"></circle>'
'<circle r="1.5" stroke="black" fill="#b3b3b3" cx="59.07190704345703" cy="23.076923370361328" '
'stroke-width="0.1" data-type="element node" data-element-id="3" data-node-id="3" data-label-name="3"></circle>',
}
],
}
self._test_create_task_201(username, spec)
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetData:
_USERNAME = "user1"
@pytest.mark.parametrize(
"content_type, task_id",
[
("image/png", 8),
("image/png", 5),
("image/x.point-cloud-data", 6),
],
)
def test_frame_content_type(self, content_type, task_id):
with make_api_client(self._USERNAME) as api_client:
(_, response) = api_client.tasks_api.retrieve_data(
task_id, type="frame", quality="original", number=0
)
assert response.status == HTTPStatus.OK
assert response.headers["Content-Type"] == content_type
@pytest.mark.usefixtures("restore_db_per_function")
class TestPatchTaskAnnotations:
def _test_check_response(self, is_allow, response, data=None):
if is_allow:
assert response.status == HTTPStatus.OK
assert compare_annotations(data, json.loads(response.data)) == {}
else:
assert response.status == HTTPStatus.FORBIDDEN
@pytest.fixture(scope="class")
def request_data(self, annotations):
def get_data(tid):
data = deepcopy(annotations["task"][str(tid)])
if data["shapes"][0]["type"] == "skeleton":
data["shapes"][0]["elements"][0].update({"points": [2.0, 3.0, 4.0, 5.0]})
else:
data["shapes"][0].update({"points": [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]})
data["version"] += 1
return data
return get_data
@pytest.mark.parametrize("org", [""])
@pytest.mark.parametrize(
"privilege, task_staff, is_allow",
[
("admin", True, True),
("admin", False, True),
("business", True, True),
("business", False, False),
("worker", True, True),
("worker", False, False),
("user", True, True),
("user", False, False),
],
)
def test_user_update_task_annotations(
self,
org,
privilege,
task_staff,
is_allow,
find_task_staff_user,
find_users,
request_data,
tasks_by_org,
filter_tasks_with_shapes,
):
users = find_users(privilege=privilege)
tasks = tasks_by_org[org]
filtered_tasks = filter_tasks_with_shapes(tasks)
username, tid = find_task_staff_user(filtered_tasks, users, task_staff, [21])
data = request_data(tid)
with make_api_client(username) as api_client:
(_, response) = api_client.tasks_api.partial_update_annotations(
id=tid,
action="update",
patched_labeled_data_request=deepcopy(data),
_parse_response=False,
_check_status=False,
)
self._test_check_response(is_allow, response, data)
@pytest.mark.parametrize("org", [2])
@pytest.mark.parametrize(
"role, task_staff, is_allow",
[
("maintainer", False, True),
("owner", False, True),
("supervisor", False, False),
("worker", False, False),
("maintainer", True, True),
("owner", True, True),
("supervisor", True, True),
("worker", True, True),
],
)
def test_member_update_task_annotation(
self,
org,
role,
task_staff,
is_allow,
find_task_staff_user,
find_users,
tasks_by_org,
request_data,
):
users = find_users(role=role, org=org)
tasks = tasks_by_org[org]
username, tid = find_task_staff_user(tasks, users, task_staff)
data = request_data(tid)
with make_api_client(username) as api_client:
(_, response) = api_client.tasks_api.partial_update_annotations(
id=tid,
action="update",
patched_labeled_data_request=deepcopy(data),
_parse_response=False,
_check_status=False,
)
self._test_check_response(is_allow, response, data)
def test_remove_first_keyframe(self):
endpoint = "tasks/8/annotations"
shapes0 = [
{"type": "rectangle", "frame": 1, "points": [1, 2, 3, 4]},
{"type": "rectangle", "frame": 4, "points": [5, 6, 7, 8]},
]
annotations = {"tracks": [{"label_id": 13, "frame": 0, "shapes": shapes0}]}
response = patch_method("admin1", endpoint, annotations, action="create")
assert response.status_code == HTTPStatus.OK, response.content
annotations["tracks"][0]["shapes"] = shapes0[1:]
response = patch_method("admin1", endpoint, annotations, action="update")
assert response.status_code == HTTPStatus.OK
def test_can_split_skeleton_tracks_on_jobs(self, jobs):
# https://github.com/opencv/cvat/pull/6968
task_id = 21
task_jobs = [job for job in jobs if job["task_id"] == task_id]
frame_ranges = {}
for job in task_jobs:
frame_ranges[job["id"]] = set(range(job["start_frame"], job["stop_frame"] + 1))
# skeleton track that covers few jobs
annotations = {
"tracks": [
{
"frame": 0,
"label_id": 58,
"shapes": [{"type": "skeleton", "frame": 0, "points": []}],
"elements": [
{
"label_id": 59,
"frame": 0,
"shapes": [
{"type": "points", "frame": 0, "points": [1.0, 2.0]},
{"type": "points", "frame": 2, "points": [1.0, 2.0]},
{"type": "points", "frame": 7, "points": [1.0, 2.0]},
],
},
],
}
]
}
# clear task annotations
response = delete_method("admin1", f"tasks/{task_id}/annotations")
assert response.status_code == 204, f"Cannot delete task's annotations: {response.content}"
# create skeleton track that covers few jobs
response = patch_method(
"admin1", f"tasks/{task_id}/annotations", annotations, action="create"
)
assert response.status_code == 200, f"Cannot update task's annotations: {response.content}"
# check that server splitted skeleton track's elements on jobs correctly
for job_id, job_frame_range in frame_ranges.items():
response = get_method("admin1", f"jobs/{job_id}/annotations")
assert response.status_code == 200, f"Cannot get job's annotations: {response.content}"
job_annotations = response.json()
assert len(job_annotations["tracks"]) == 1, "Expected to see only one track"
track = job_annotations["tracks"][0]
assert track.get("elements", []), "Expected to see track with elements"
for element in track["elements"]:
element_frames = set(shape["frame"] for shape in element["shapes"])
assert element_frames <= job_frame_range, "Track shapes get out of job frame range"
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetTaskDataset:
def _test_export_task(self, username: str, tid: int, **kwargs):
with make_api_client(username) as api_client:
return export_dataset(api_client.tasks_api.retrieve_dataset_endpoint, id=tid, **kwargs)
def test_can_export_task_dataset(self, admin_user, tasks_with_shapes):
task = tasks_with_shapes[0]
response = self._test_export_task(admin_user, task["id"])
assert response.data
@pytest.mark.parametrize("tid", [21])
@pytest.mark.parametrize(
"format_name", ["CVAT for images 1.1", "CVAT for video 1.1", "COCO Keypoints 1.0"]
)
def test_can_export_task_with_several_jobs(self, admin_user, tid, format_name):
response = self._test_export_task(admin_user, tid, format=format_name)
assert response.data
@pytest.mark.parametrize("tid", [8])
def test_can_export_task_to_coco_format(self, admin_user, tid):
# these annotations contains incorrect frame numbers
# in order to check that server handle such cases
annotations = {
"version": 0,
"tags": [],
"shapes": [],
"tracks": [
{
"label_id": 63,
"frame": 1,
"group": 0,
"source": "manual",
"shapes": [
{
"type": "skeleton",
"frame": 1,
"occluded": False,
"outside": False,
"z_order": 0,
"rotation": 0,
"points": [],
"attributes": [],
}
],
"attributes": [],
"elements": [
{
"label_id": 64,
"frame": 0,
"group": 0,
"source": "manual",
"shapes": [
{
"type": "points",
"frame": 1,
"occluded": False,
"outside": True,
"z_order": 0,
"rotation": 0,
"points": [74.14935096036425, 79.09960455479086],
"attributes": [],
},
{
"type": "points",
"frame": 7,
"occluded": False,
"outside": False,
"z_order": 0,
"rotation": 0,
"points": [74.14935096036425, 79.09960455479086],
"attributes": [],
},
],
"attributes": [],
},
{
"label_id": 65,
"frame": 0,
"group": 0,
"source": "manual",
"shapes": [
{
"type": "points",
"frame": 0,
"occluded": False,
"outside": False,
"z_order": 0,
"rotation": 0,
"points": [285.07319976630424, 353.51583641966175],
"attributes": [],
}
],
"attributes": [],
},
],
}
],
}
response = patch_method(
admin_user, f"tasks/{tid}/annotations", annotations, action="update"
)
assert response.status_code == HTTPStatus.OK
# check that we can export task
response = self._test_export_task(admin_user, tid, format="COCO Keypoints 1.0")
assert response.status == HTTPStatus.OK
# check that server saved track annotations correctly
response = get_method(admin_user, f"tasks/{tid}/annotations")
assert response.status_code == HTTPStatus.OK
annotations = response.json()
assert annotations["tracks"][0]["frame"] == 0
assert annotations["tracks"][0]["shapes"][0]["frame"] == 0
assert annotations["tracks"][0]["elements"][0]["shapes"][0]["frame"] == 0
@pytest.mark.usefixtures("restore_db_per_function")
def test_can_download_task_with_special_chars_in_name(self, admin_user):
# Control characters in filenames may conflict with the Content-Disposition header
# value restrictions, as it needs to include the downloaded file name.
task_spec = {
"name": "test_special_chars_{}_in_name".format("".join(chr(c) for c in range(1, 127))),
"labels": [{"name": "cat"}],
}
task_data = {
"image_quality": 75,
"client_files": generate_image_files(1),
}
task_id, _ = create_task(admin_user, task_spec, task_data)
response = self._test_export_task(admin_user, task_id)
assert response.status == HTTPStatus.OK
assert zipfile.is_zipfile(io.BytesIO(response.data))
def test_export_dataset_after_deleting_related_cloud_storage(self, admin_user, tasks):
related_field = "target_storage"
task = next(
t for t in tasks if t[related_field] and t[related_field]["location"] == "cloud_storage"
)
task_id = task["id"]
cloud_storage_id = task[related_field]["cloud_storage_id"]
with make_api_client(admin_user) as api_client:
_, response = api_client.cloudstorages_api.destroy(cloud_storage_id)
assert response.status == HTTPStatus.NO_CONTENT
result, response = api_client.tasks_api.retrieve(task_id)
assert not result[related_field]
response = export_dataset(api_client.tasks_api.retrieve_dataset_endpoint, id=task["id"])
assert response.data
@pytest.mark.usefixtures("restore_db_per_function")
@pytest.mark.usefixtures("restore_cvat_data")
@pytest.mark.usefixtures("restore_redis_ondisk_per_function")
class TestPostTaskData:
_USERNAME = "admin1"
def _test_cannot_create_task(self, username, spec, data, **kwargs):
with make_api_client(username) as api_client:
(task, response) = api_client.tasks_api.create(spec, **kwargs)
assert response.status == HTTPStatus.CREATED
(_, response) = api_client.tasks_api.create_data(
task.id, data_request=deepcopy(data), _content_type="application/json", **kwargs
)
assert response.status == HTTPStatus.ACCEPTED
status = wait_until_task_is_created(api_client.tasks_api, task.id)
assert status.state.value == "Failed"
return status
def test_can_create_task_with_defined_start_and_stop_frames(self):
task_spec = {
"name": f"test {self._USERNAME} to create a task with defined start and stop frames",
"labels": [
{
"name": "car",
"color": "#ff00ff",
"attributes": [
{
"name": "a",
"mutable": True,
"input_type": "number",
"default_value": "5",
"values": ["4", "5", "6"],
}
],
}
],
}
task_data = {
"image_quality": 75,
"start_frame": 2,
"stop_frame": 5,
"client_files": generate_image_files(7),
}
task_id, _ = create_task(self._USERNAME, task_spec, task_data)
# check task size
with make_api_client(self._USERNAME) as api_client:
(task, _) = api_client.tasks_api.retrieve(task_id)
assert task.size == 4
def test_can_create_task_with_exif_rotated_images(self):
task_spec = {
"name": f"test {self._USERNAME} to create a task with exif rotated images",
"labels": [
{
"name": "car",
}
],
}
image_files = ["images/exif_rotated/left.jpg", "images/exif_rotated/right.jpg"]
task_data = {
"server_files": image_files,
"image_quality": 70,
"segment_size": 500,
"use_cache": True,
"sorting_method": "natural",
}
task_id, _ = create_task(self._USERNAME, task_spec, task_data)
# check that the frames have correct width and height
for chunk_quality in ["original", "compressed"]:
with make_api_client(self._USERNAME) as api_client:
_, response = api_client.tasks_api.retrieve_data(
task_id, number=0, type="chunk", quality=chunk_quality
)
data_meta, _ = api_client.tasks_api.retrieve_data_meta(task_id)
with zipfile.ZipFile(io.BytesIO(response.data)) as zip_file:
for name, frame_meta in zip(zip_file.namelist(), data_meta.frames):
with zip_file.open(name) as zipped_img:
im = Image.open(zipped_img)
# original is 480x640 with 90/-90 degrees rotation
assert frame_meta.height == 640 and frame_meta.width == 480
assert im.height == 640 and im.width == 480
assert im.getexif().get(274, 1) == 1
def test_can_create_task_with_big_images(self):
# Checks for regressions about the issue
# https://github.com/opencv/cvat/issues/6878
# In the case of big files (>2.5 MB by default),
# uploaded files could be write-appended twice,
# leading to bigger raw file sizes than expected.
task_spec = {
"name": f"test {self._USERNAME} to create a task with big images",
"labels": [
{
"name": "car",
}
],
}
# We need a big file to reproduce the problem
image_file = generate_image_file("big_image.bmp", size=(4000, 4000), color=(100, 200, 30))
image_bytes = image_file.getvalue()
file_size = len(image_bytes)
assert 10 * 2**20 < file_size
task_data = {
"client_files": [image_file],
"image_quality": 70,
"use_cache": False,
"use_zip_chunks": True,
}
task_id, _ = create_task(self._USERNAME, task_spec, task_data)
# check that the original chunk image have the original size
# this is less accurate than checking the uploaded file directly, but faster
with make_api_client(self._USERNAME) as api_client:
_, response = api_client.tasks_api.retrieve_data(
task_id, number=0, quality="original", type="chunk", _parse_response=False
)
chunk_file = io.BytesIO(response.data)
with zipfile.ZipFile(chunk_file) as chunk_zip:
infos = chunk_zip.infolist()
assert len(infos) == 1
assert infos[0].file_size == file_size
chunk_image = chunk_zip.read(infos[0])
assert chunk_image == image_bytes
def test_can_create_task_with_exif_rotated_tif_image(self):
task_spec = {
"name": f"test {self._USERNAME} to create a task with exif rotated tif image",
"labels": [
{
"name": "car",
}
],
}
image_files = ["images/exif_rotated/tif_left.tif"]
task_data = {
"server_files": image_files,
"image_quality": 70,
"segment_size": 500,
"use_cache": False,
"sorting_method": "natural",
}
task_id, _ = create_task(self._USERNAME, task_spec, task_data)
for chunk_quality in ["original", "compressed"]:
# check that the frame has correct width and height
with make_api_client(self._USERNAME) as api_client:
_, response = api_client.tasks_api.retrieve_data(
task_id, number=0, type="chunk", quality=chunk_quality
)
with zipfile.ZipFile(io.BytesIO(response.data)) as zip_file:
assert len(zip_file.namelist()) == 1
name = zip_file.namelist()[0]
assert name == "000000.tif" if chunk_quality == "original" else "000000.jpeg"
with zip_file.open(name) as zipped_img:
im = Image.open(zipped_img)
# raw image is horizontal 100x150 with -90 degrees rotation
assert im.height == 150 and im.width == 100
assert im.getexif().get(274, 1) == 1
def test_can_create_task_with_sorting_method_natural(self):
task_spec = {
"name": f"test {self._USERNAME} to create a task with a custom sorting method",
"labels": [
{
"name": "car",
}
],
}
image_files = generate_image_files(15)
task_data = {
"client_files": image_files[5:] + image_files[:5], # perturb the order
"image_quality": 70,
"sorting_method": "natural",
}
task_id, _ = create_task(self._USERNAME, task_spec, task_data)
# check that the frames were sorted again
with make_api_client(self._USERNAME) as api_client:
data_meta, _ = api_client.tasks_api.retrieve_data_meta(task_id)
# generate_image_files produces files that are already naturally sorted
for image_file, frame in zip(image_files, data_meta.frames):
assert image_file.name == frame.name
@pytest.mark.parametrize("data_source", ["client_files", "server_files"])
def test_can_create_task_with_sorting_method_predefined(self, data_source):
task_spec = {
"name": f"test {self._USERNAME} to create a task with a custom sorting method",
"labels": [
{
"name": "car",
}
],
}
if data_source == "client_files":
image_files = generate_image_files(15)
# shuffle to check for occasional sorting, e.g. in the DB
image_files = image_files[7:] + image_files[5:7] + image_files[:5]
elif data_source == "server_files":
# Files from the test file share
image_files = ["images/image_3.jpg", "images/image_1.jpg", "images/image_2.jpg"]
else:
assert False
task_data = {
data_source: image_files,
"image_quality": 70,
"sorting_method": "predefined",
}
(task_id, _) = create_task(self._USERNAME, task_spec, task_data)
# check that the frames were sorted again
with make_api_client(self._USERNAME) as api_client:
(data_meta, _) = api_client.tasks_api.retrieve_data_meta(task_id)
for image_file, frame in zip(image_files, data_meta.frames):
if isinstance(image_file, str):
image_name = image_file
else:
image_name = image_file.name
assert image_name == frame.name
def test_can_get_annotations_from_new_task_with_skeletons(self):
spec = {
"name": f"test admin1 to create a task with skeleton",
"labels": [