Skip to content

Commit 488bcd1

Browse files
committed
changed download_image_annotations to sdk_core
1 parent 545dcbc commit 488bcd1

File tree

8 files changed

+42
-68
lines changed

8 files changed

+42
-68
lines changed

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,12 +1097,15 @@ def download_image_annotations(
10971097
:rtype: tuple
10981098
"""
10991099

1100-
project, folder = self.controller.get_project_folder_by_path(project)
1100+
project_name, folder_name = extract_project_folder(project)
1101+
project = self.controller.get_project(project_name)
1102+
folder = project.get_folder(folder_name)
1103+
download_path = self.controller.setup_destination_dir(local_dir_path)
11011104
res = self.controller.annotations.download_image_annotations(
11021105
project=project,
11031106
folder=folder,
11041107
image_name=image_name,
1105-
destination=local_dir_path,
1108+
destination=download_path,
11061109
)
11071110
if res.errors:
11081111
raise AppException(res.errors)

src/superannotate/lib/core/serviceproviders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ def get_limitations(
479479
@abstractmethod
480480
def get_download_token(
481481
self,
482-
project: entities.ProjectEntity,
483-
folder: entities.FolderEntity,
482+
project_id: int,
483+
folder_id: int,
484484
image_id: int,
485485
include_original: int = 1,
486486
) -> ServiceResponse:

src/superannotate/lib/core/usecases/images.py

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
from lib.core.usecases.base import BaseReportableUseCase
4747
from lib.core.usecases.base import BaseUseCase
4848
from PIL import UnidentifiedImageError
49+
from superannotate_core.app import Folder
50+
from superannotate_core.app import Item
51+
from superannotate_core.app import Project
4952

5053
logger = logging.getLogger("sa")
5154

@@ -1381,8 +1384,8 @@ def execute(self) -> Response:
13811384
class DownloadImageAnnotationsUseCase(BaseUseCase):
13821385
def __init__(
13831386
self,
1384-
project: ProjectEntity,
1385-
folder: FolderEntity,
1387+
project: Project,
1388+
folder: Folder,
13861389
image_name: str,
13871390
service_provider: BaseServiceProvider,
13881391
destination: str,
@@ -1394,15 +1397,6 @@ def __init__(
13941397
self._service_provider = service_provider
13951398
self._destination = destination
13961399

1397-
@property
1398-
def image_use_case(self):
1399-
return GetImageUseCase(
1400-
service_provider=self._service_provider,
1401-
project=self._project,
1402-
folder=self._folder,
1403-
image_name=self._image_name,
1404-
)
1405-
14061400
def validate_project_type(self):
14071401
if self._project.type in constances.LIMITED_FUNCTIONS:
14081402
raise AppValidationException(
@@ -1476,56 +1470,32 @@ def fill_classes_data(self, annotations: dict):
14761470

14771471
def execute(self):
14781472
if self.is_valid():
1479-
data = {
1480-
"annotation_json": None,
1481-
"annotation_json_filename": None,
1482-
"annotation_mask": None,
1483-
"annotation_mask_filename": None,
1484-
}
1485-
image_response = self.image_use_case.execute()
1486-
token = self._service_provider.get_download_token(
1487-
project=self._project,
1488-
folder=self._folder,
1489-
image_id=image_response.data.id,
1490-
).data
1491-
credentials = token["annotations"]["MAIN"][0]
1492-
1493-
annotation_json_creds = credentials["annotation_json_path"]
1494-
1495-
response = requests.get(
1496-
url=annotation_json_creds["url"],
1497-
headers=annotation_json_creds["headers"],
1498-
)
1499-
if not response.ok:
1500-
# TODO remove
1501-
logger.warning("Couldn't load annotations.")
1502-
self._response.data = (None, None)
1503-
return self._response
1504-
data["annotation_json"] = response.json()
1505-
data["annotation_json_filename"] = f"{self._image_name}.json"
15061473
mask_path = None
1507-
if self._project.type == constances.ProjectType.PIXEL.value:
1508-
annotation_blue_map_creds = credentials["annotation_bluemap_path"]
1474+
if self._project.type.value == constances.ProjectType.PIXEL.value:
1475+
image: Item = self._folder.list_items(item_names=[self._image_name])[0]
1476+
token = self._service_provider.get_download_token(
1477+
project_id=self._project.id,
1478+
folder_id=self._folder.id,
1479+
image_id=image.id,
1480+
).data
1481+
annotation_blue_map_creds = token["annotations"]["MAIN"][0][
1482+
"annotation_bluemap_path"
1483+
]
15091484
response = requests.get(
15101485
url=annotation_blue_map_creds["url"],
15111486
headers=annotation_blue_map_creds["headers"],
15121487
)
1513-
data["annotation_mask_filename"] = f"{self._image_name}___save.png"
1488+
annotation_mask_filename = f"{self._image_name}___save.png"
15141489
if response.ok:
1515-
data["annotation_mask"] = io.BytesIO(response.content).getbuffer()
1516-
mask_path = (
1517-
Path(self._destination) / data["annotation_mask_filename"]
1518-
)
1490+
mask_path = Path(self._destination) / annotation_mask_filename
15191491
with open(mask_path, "wb") as f:
1520-
f.write(data["annotation_mask"])
1492+
f.write(io.BytesIO(response.content).getbuffer())
15211493
else:
15221494
logger.info("There is no blue-map for the image.")
1523-
1524-
json_path = Path(self._destination) / data["annotation_json_filename"]
1525-
self.fill_classes_data(data["annotation_json"])
1526-
with open(json_path, "w") as f:
1527-
json.dump(data["annotation_json"], f, indent=4)
1528-
1495+
self._folder.download_annotations(
1496+
download_path=self._destination, item_names=[self._image_name]
1497+
)
1498+
json_path = f"{self._destination}/{self._image_name}"
15291499
self._response.data = (str(json_path), str(mask_path))
15301500
return self._response
15311501

src/superannotate/lib/infrastructure/controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ def download(
490490

491491
def download_image_annotations(
492492
self,
493-
project: ProjectEntity,
494-
folder: FolderEntity,
493+
project: Project,
494+
folder: Folder,
495495
image_name: str,
496496
destination: str,
497497
):

src/superannotate/lib/infrastructure/serviceprovider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def get_limitations(
7979

8080
def get_download_token(
8181
self,
82-
project: entities.ProjectEntity,
83-
folder: entities.FolderEntity,
82+
project_id: int,
83+
folder_id: int,
8484
image_id: int,
8585
include_original: int = 1,
8686
):
@@ -92,8 +92,8 @@ def get_download_token(
9292
download_token_url,
9393
"get",
9494
params={
95-
"project_id": project.id,
96-
"folder_id": folder.id,
95+
"project_id": project_id,
96+
"folder_id": folder_id,
9797
"include_original": include_original,
9898
},
9999
)

tests/integration/annotations/test_download_annotations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def test_download_annotations_from_folders_mul(self):
121121
)
122122
assert count == 31 + 5 # folder names and classes
123123

124+
# TODO failed after SDK_core integration (check logging in future)
124125
def test_download_annotations_duplicated_names(self):
125126
self._attach_items(count=4)
126127
with tempfile.TemporaryDirectory() as temp_dir:

tests/integration/annotations/test_get_annotations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
import pytest
7+
from requests.exceptions import HTTPError
78
from src.superannotate import SAClient
89
from tests.integration.base import BaseTestCase
910

@@ -58,6 +59,7 @@ def test_get_annotations_by_ids(self):
5859

5960
self.assertEqual(len(annotations), 4)
6061

62+
# TODO check the behavior of get_annotations in case of item_ids in the future
6163
def test_get_annotations_by_ids_with_duplicate_names(self):
6264
sa.create_folder(self.PROJECT_NAME, self.FOLDER_NAME_2)
6365
self._attach_items(count=4, folder=self.FOLDER_NAME_2) # noqa
@@ -87,12 +89,9 @@ def test_get_annotations_by_wrong_item_ids(self):
8789

8890
self.assertEqual(len(annotations), 0)
8991

90-
# todo update the implementation
92+
# TODO update the implementation
9193
def test_get_annotations_by_wrong_project_ids(self):
92-
try:
93-
sa.get_annotations(1, [1, 2, 3])
94-
except Exception as e:
95-
self.assertEqual(str(e), "Project not found.")
94+
self.assertRaises(HTTPError, sa.get_annotations, 1, [1, 2, 3])
9695

9796
@pytest.mark.flaky(reruns=3)
9897
def test_get_annotations_order(self):
@@ -177,6 +176,7 @@ def test_get_annotations10000(self):
177176
a = sa.get_annotations(self.PROJECT_NAME)
178177
assert len(a) == count
179178

179+
# TODO failed after SDK_core integration (check logging in future)
180180
def test_get_annotations_logs(self):
181181
self._attach_items(count=4)
182182
items_names = [self.IMAGE_NAME] * 4

tests/integration/annotations/test_preannotation_upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def folder_path(self):
1919
return os.path.join(Path(__file__).parent.parent.parent, self.TEST_FOLDER_PATH)
2020

2121
def test_pre_annotation_folder_upload_download(self):
22-
self._attach_items()
22+
self._attach_items(count=4)
2323
sa.create_annotation_classes_from_classes_json(
2424
self.PROJECT_NAME, f"{self.folder_path}/classes/classes.json"
2525
)

0 commit comments

Comments
 (0)