Skip to content

Commit 1bf8e00

Browse files
feature: upload tags
1 parent 6f843ca commit 1bf8e00

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

next_cvat/client/job_annotations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ def add_polyline_(
5151

5252
return self
5353

54+
def add_tag_(
55+
self,
56+
tag: next_cvat.Tag,
57+
image_name: str,
58+
group: int = 0,
59+
) -> JobAnnotations:
60+
label = self.job.task.project.label(name=tag.label)
61+
62+
frame = self.job.task.frame(image_name=image_name)
63+
64+
self.annotations["tags"].append(
65+
tag.request(frame=frame.id, label_id=label.id, group=group)
66+
)
67+
68+
return self
69+
5470
def request(self) -> models.LabeledDataRequest:
5571
request = models.LabeledDataRequest()
5672
request.version = self.annotations["version"]

next_cvat/types/tag.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import List
44

5+
from cvat_sdk.api_client import models
56
from pydantic import BaseModel
67

78
from .attribute import Attribute
@@ -11,3 +12,14 @@ class Tag(BaseModel):
1112
label: str
1213
source: str
1314
attributes: List[Attribute]
15+
16+
def request(
17+
self, frame: int, label_id: int, group: int = 0
18+
) -> models.LabeledImageRequest:
19+
return models.LabeledImageRequest(
20+
frame=frame,
21+
label_id=label_id,
22+
group=group,
23+
source=self.source,
24+
attributes=[attr.model_dump() for attr in self.attributes],
25+
)

tests/test_add_tag.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
import next_cvat
6+
7+
8+
def test_add_tag():
9+
if not Path(".env.cvat.secrets").exists():
10+
pytest.skip("No secrets file found")
11+
12+
client = next_cvat.Client.from_env_file(".env.cvat.secrets")
13+
14+
project_id = 198488 # Using the same test project as in other tests
15+
job_id = 1442235
16+
task_id = 999670
17+
deformation_attribute_id = 1389238
18+
19+
job = client.project(project_id).task(task_id).job(job_id)
20+
21+
annotations = job.annotations()
22+
23+
annotations.add_tag_(
24+
next_cvat.Tag(
25+
label="Deformation",
26+
source="manual",
27+
attributes=[
28+
next_cvat.Attribute(
29+
name="Severity",
30+
value="High",
31+
spec_id=deformation_attribute_id,
32+
)
33+
],
34+
),
35+
image_name="20240916_000854_2011T_437.bmp",
36+
)
37+
38+
job.update_annotations_(annotations)
39+
40+
print("Successfully added tag annotation")

0 commit comments

Comments
 (0)