Skip to content

Commit c4e142c

Browse files
committed
Disabled some SDK functions for projects with URL attached images
1 parent 2c6df3d commit c4e142c

File tree

5 files changed

+101
-10
lines changed

5 files changed

+101
-10
lines changed

superannotate/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"Skipped": 6
3131
}
3232

33-
_UPLOAD_STATES = {"Initial": 1, "Basic": 2, "External": 3}
33+
_UPLOAD_STATES_STR_TO_CODES = {"Initial": 1, "Basic": 2, "External": 3}
34+
_UPLOAD_STATES_CODES_TO_STR = {1: "Initial", 2: "Basic", 3: "External"}
3435

3536
_USER_ROLES = {"Admin": 2, "Annotator": 3, "QA": 4, "Customer": 5, "Viewer": 6}
3637
_AVAILABLE_SEGMENTATION_MODELS = ['autonomous', 'generic']
@@ -122,7 +123,11 @@ def annotation_status_str_to_int(annotation_status):
122123

123124

124125
def upload_state_str_to_int(upload_state):
125-
return _UPLOAD_STATES[upload_state]
126+
return _UPLOAD_STATES_STR_TO_CODES[upload_state]
127+
128+
129+
def upload_state_int_to_str(upload_state):
130+
return _UPLOAD_STATES_CODES_TO_STR[upload_state]
126131

127132

128133
def annotation_status_int_to_str(annotation_status):

superannotate/db/images.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,12 @@ def download_image(
631631
)
632632

633633
project, project_folder = get_project_and_folder_metadata(project)
634+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
635+
if upload_state == "External":
636+
raise SABaseException(
637+
0,
638+
"The function does not support projects containing images attached with URLs"
639+
)
634640
img = get_image_bytes(
635641
(project, project_folder), image_name, variant=variant
636642
)
@@ -698,6 +704,13 @@ def get_image_bytes(project, image_name, variant='original'):
698704
:return: io.BytesIO() of the image
699705
:rtype: io.BytesIO()
700706
"""
707+
project, project_folder = get_project_and_folder_metadata(project)
708+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
709+
if upload_state == "External":
710+
raise SABaseException(
711+
0,
712+
"The function does not support projects containing images attached with URLs"
713+
)
701714
if variant not in ["original", "lores"]:
702715
raise SABaseException(
703716
0, "Image download variant should be either original or lores"

superannotate/db/project_images.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def upload_image_to_project(
5151
:type image_quality_in_editor: str
5252
"""
5353
project, project_folder = get_project_and_folder_metadata(project)
54+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
55+
if upload_state == "External":
56+
raise SABaseException(
57+
0,
58+
"The function does not support projects containing images attached with URLs"
59+
)
5460
annotation_status = common.annotation_status_str_to_int(annotation_status)
5561
if image_quality_in_editor is None:
5662
image_quality_in_editor = get_project_default_image_quality_in_editor(
@@ -121,8 +127,12 @@ def upload_image_to_project(
121127
else:
122128
project_folder_id = None
123129
__create_image(
124-
[img_name], [key], project, annotation_status, prefix,
125-
[images_info_and_array[2]], project_folder_id
130+
[img_name], [key],
131+
project,
132+
annotation_status,
133+
prefix, [images_info_and_array[2]],
134+
project_folder_id,
135+
upload_state="Basic"
126136
)
127137

128138
while True:

superannotate/db/projects.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ def upload_video_to_project(
237237
:return: filenames of uploaded images
238238
:rtype: list of strs
239239
"""
240+
project, project_folder = get_project_and_folder_metadata(project)
241+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
242+
if upload_state == "External":
243+
raise SABaseException(
244+
0,
245+
"The function does not support projects containing images attached with URLs"
246+
)
240247
logger.info("Uploading from video %s.", str(video_path))
241248
rotate_code = None
242249
try:
@@ -390,6 +397,12 @@ def upload_videos_from_folder_to_project(
390397
:rtype: tuple of list of strs
391398
"""
392399
project, project_folder = get_project_and_folder_metadata(project)
400+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
401+
if upload_state == "External":
402+
raise SABaseException(
403+
0,
404+
"The function does not support projects containing images attached with URLs"
405+
)
393406
if recursive_subfolders:
394407
logger.warning(
395408
"When using recursive subfolder parsing same name videos in different subfolders will overwrite each other."
@@ -480,6 +493,12 @@ def upload_images_from_folder_to_project(
480493
project_folder_name = project["name"] + (
481494
f'/{project_folder["name"]}' if project_folder else ""
482495
)
496+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
497+
if upload_state == "External":
498+
raise SABaseException(
499+
0,
500+
"The function does not support projects containing images attached with URLs"
501+
)
483502
if recursive_subfolders:
484503
logger.info(
485504
"When using recursive subfolder parsing same name images in different subfolders will overwrite each other."
@@ -812,6 +831,12 @@ def upload_images_to_project(
812831
project_folder_name = project["name"] + (
813832
f'/{project_folder["name"]}' if project_folder else ""
814833
)
834+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
835+
if upload_state == "External":
836+
raise SABaseException(
837+
0,
838+
"The function does not support projects containing images attached with URLs"
839+
)
815840
if not isinstance(img_paths, list):
816841
raise SABaseException(
817842
0, "img_paths argument to upload_images_to_project should be a list"
@@ -939,13 +964,12 @@ def attach_image_urls_to_project(
939964
project_folder_name = project["name"] + (
940965
f'/{project_folder["name"]}' if project_folder else ""
941966
)
942-
upload_state = project.get("upload_state")
943-
if upload_state == "basic":
967+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
968+
if upload_state == "Basic":
944969
raise SABaseException(
945970
0,
946971
"You cannot attach URLs in this type of project. Please attach it in an external storage project"
947972
)
948-
upload_state = "external"
949973
annotation_status = common.annotation_status_str_to_int(annotation_status)
950974
team_id, project_id = project["team_id"], project["id"]
951975
image_data = pd.read_csv(attachments)
@@ -1144,7 +1168,13 @@ def upload_images_from_public_urls_to_project(
11441168
images_to_upload = []
11451169
duplicate_images_filenames = []
11461170
path_to_url = {}
1147-
1171+
project, project_folder = get_project_and_folder_metadata(project)
1172+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
1173+
if upload_state == "External":
1174+
raise SABaseException(
1175+
0,
1176+
"The function does not support projects containing images attached with URLs"
1177+
)
11481178
finish_event = threading.Event()
11491179
tqdm_thread = threading.Thread(
11501180
target=_tqdm_download,
@@ -1246,6 +1276,13 @@ def upload_images_from_google_cloud_to_project(
12461276
images_to_upload = []
12471277
duplicate_images_filenames = []
12481278
path_to_url = {}
1279+
project, project_folder = get_project_and_folder_metadata(project)
1280+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
1281+
if upload_state == "External":
1282+
raise SABaseException(
1283+
0,
1284+
"The function does not support projects containing images attached with URLs"
1285+
)
12491286
cloud_client = storage.Client(project=google_project)
12501287
bucket = cloud_client.get_bucket(bucket_name)
12511288
image_blobs = bucket.list_blobs(prefix=folder_path)
@@ -1322,6 +1359,13 @@ def upload_images_from_azure_blob_to_project(
13221359
images_to_upload = []
13231360
duplicate_images_filenames = []
13241361
path_to_url = {}
1362+
project, project_folder = get_project_and_folder_metadata(project)
1363+
upload_state = common.upload_state_int_to_str(project.get("upload_state"))
1364+
if upload_state == "External":
1365+
raise SABaseException(
1366+
0,
1367+
"The function does not support projects containing images attached with URLs"
1368+
)
13251369
connect_key = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
13261370
blob_service_client = BlobServiceClient.from_connection_string(connect_key)
13271371
container_client = blob_service_client.get_container_client(container_name)

superannotate/ml/ml_funcs.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ..api import API
1818
from ..common import (
1919
_AVAILABLE_SEGMENTATION_MODELS, model_training_status_int_to_str,
20-
project_type_str_to_int
20+
project_type_str_to_int, upload_state_int_to_str
2121
)
2222
from ..db.images import get_image_metadata, search_images
2323
from ..exceptions import SABaseException
@@ -56,7 +56,12 @@ def run_prediction(project, images_list, model):
5656
f"Specified project has type {project['type']}, and does not correspond to the type of provided model"
5757
)
5858
project_id = project["id"]
59-
59+
upload_state = upload_state_int_to_str(project.get("upload_state"))
60+
if upload_state == "External":
61+
raise SABaseException(
62+
0,
63+
"The function does not support projects containing images attached with URLs"
64+
)
6065
images_metadata = get_image_metadata(project, images_list)
6166
if isinstance(images_metadata, dict):
6267
images_metadata = [images_metadata]
@@ -126,6 +131,13 @@ def run_segmentation(project, images_list, model):
126131
)
127132
raise SABaseException(0, "Model Does not exist")
128133

134+
upload_state = upload_state_int_to_str(project.get("upload_state"))
135+
if upload_state == "External":
136+
raise SABaseException(
137+
0,
138+
"The function does not support projects containing images attached with URLs"
139+
)
140+
129141
images_metadata = get_image_metadata(project, images_list)
130142
images_metadata.sort(key=lambda x: x["name"])
131143

@@ -216,6 +228,13 @@ def run_training(
216228
raise SABaseException(0, "Invalid project types")
217229
project_type = types.pop()
218230

231+
upload_state = upload_state_int_to_str(project.get("upload_state"))
232+
if upload_state == "External":
233+
raise SABaseException(
234+
0,
235+
"The function does not support projects containing images attached with URLs"
236+
)
237+
219238
base_model = base_model.get(project_type, None)
220239
if not base_model:
221240
logger.error(

0 commit comments

Comments
 (0)