4646from lib .core .usecases .base import BaseReportableUseCase
4747from lib .core .usecases .base import BaseUseCase
4848from 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
5053logger = logging .getLogger ("sa" )
5154
@@ -1381,8 +1384,8 @@ def execute(self) -> Response:
13811384class 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
0 commit comments