Skip to content

Commit a60fc4e

Browse files
committed
Many fixes to UUID
1 parent ea8f0a7 commit a60fc4e

File tree

4 files changed

+95
-81
lines changed

4 files changed

+95
-81
lines changed

superannotate/common.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ def model_training_status_str_to_int(project_status):
6565

6666
def image_path_to_annotation_paths(image_path, project_type):
6767
image_path = Path(image_path)
68-
postfix_json = '___objects.json' if project_type == "Vector" else '___pixel.json'
69-
postfix_mask = '___save.png'
7068
if project_type == "Vector":
71-
return (image_path.parent / (image_path.name + postfix_json), )
69+
return (
70+
image_path.parent /
71+
get_annotation_json_name(image_path.name, project_type),
72+
)
7273
return (
73-
image_path.parent / (image_path.name + postfix_json),
74-
image_path.parent / (image_path.name + postfix_mask)
74+
image_path.parent /
75+
get_annotation_json_name(image_path.name, project_type),
76+
image_path.parent / get_annotation_png_name(image_path.name)
7577
)
7678

7779

@@ -316,3 +318,14 @@ def tqdm_converter(
316318
else:
317319
pbar.update(total_num - pbar.n)
318320
break
321+
322+
323+
def get_annotation_json_name(image_name, project_type):
324+
if project_type == "Vector":
325+
return image_name + "___objects.json"
326+
else:
327+
return image_name + "___pixel.json"
328+
329+
330+
def get_annotation_png_name(image_name):
331+
return image_name + "___save.png"

superannotate/db/images.py

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
add_annotation_polyline_to_json, add_annotation_template_to_json
1818
)
1919
from ..api import API
20+
from ..common import process_api_response
2021
from ..exceptions import SABaseException
22+
from ..parameter_decorators import project_metadata
2123
from .annotation_classes import (
2224
fill_class_and_attribute_ids, fill_class_and_attribute_names,
2325
get_annotation_classes_id_to_name, get_annotation_classes_name_to_id,
2426
search_annotation_classes
2527
)
2628
from .project_api import get_project_metadata_bare
27-
from ..common import process_api_response
28-
from ..parameter_decorators import project_metadata
2929

3030
logger = logging.getLogger("superannotate-python-sdk")
3131

@@ -632,7 +632,14 @@ def get_image_bytes(project, image_name, variant='original'):
632632
res = response.json()
633633
url = res[variant]["url"]
634634
headers = res[variant]["headers"]
635+
print(params)
636+
print(url)
637+
print(headers)
635638
response = requests.get(url=url, headers=headers)
639+
if not response.ok:
640+
raise SABaseException(
641+
response.status_code, "Couldn't download image" + response.text
642+
)
636643
img = io.BytesIO(response.content)
637644
return img
638645

@@ -762,61 +769,50 @@ def get_image_annotations(project, image_name, project_type=None):
762769
params=params
763770
)
764771
if not response.ok:
765-
raise SABaseException(response.status_code, response.text)
772+
raise SABaseException(
773+
response.status_code,
774+
"Couldn't get annotation download token" + response.text
775+
)
766776
res = response.json()
777+
# print(json.dumps(res, indent=2))
767778

768779
annotation_classes = search_annotation_classes(project)
769780
annotation_classes_dict = get_annotation_classes_id_to_name(
770781
annotation_classes
771782
)
772-
if project_type == "Vector":
773-
url = res["objects"]["url"]
774-
annotation_json_filename = url.rsplit('/', 1)[-1]
775-
headers = res["objects"]["headers"]
776-
response = requests.get(url=url, headers=headers)
777-
if response.ok:
778-
res_json = response.json()
779-
fill_class_and_attribute_names(res_json, annotation_classes_dict)
780-
return {
781-
"annotation_json_filename": annotation_json_filename,
782-
"annotation_json": res_json
783-
}
784-
if not response.ok and response.status_code == 403:
785-
return {"annotation_json": None, "annotation_json_filename": None}
786-
raise SABaseException(response.status_code, response.text)
787-
else: # pixel
788-
url = res["pixelObjects"]["url"]
789-
annotation_json_filename = url.rsplit('/', 1)[-1]
790-
headers = res["pixelObjects"]["headers"]
791-
response = requests.get(url=url, headers=headers)
792-
if not response.ok and response.status_code == 403:
793-
return {
794-
"annotation_json": None,
795-
"annotation_json_filename": None,
796-
"annotation_mask": None,
797-
"annotation_mask_filename": None
798-
}
799-
elif not response.ok:
800-
raise SABaseException(response.status_code, response.text)
801-
res_json = response.json()
802-
fill_class_and_attribute_names(res_json, annotation_classes_dict)
803-
if len(res_json["instances"]) != 0:
804-
url = res["pixelSave"]["url"]
805-
annotation_mask_filename = url.rsplit('/', 1)[-1]
806-
headers = res["pixelSave"]["headers"]
807-
response = requests.get(url=url, headers=headers)
808-
if not response.ok:
809-
raise SABaseException(response.status_code, response.text)
810-
mask = io.BytesIO(response.content)
811-
else:
812-
mask = None
813-
annotation_mask_filename = None
814-
return {
815-
"annotation_json": res_json,
816-
"annotation_json_filename": annotation_json_filename,
817-
"annotation_mask": mask,
818-
"annotation_mask_filename": annotation_mask_filename
819-
}
783+
main_annotations = res["annotations"]["MAIN"][0]
784+
response = requests.get(
785+
url=main_annotations["annotation_json_path"]["url"],
786+
headers=main_annotations["annotation_json_path"]["headers"]
787+
)
788+
if not response.ok:
789+
raise SABaseException(
790+
response.status_code, "Couldn't load annotations" + response.text
791+
)
792+
res_json = response.json()
793+
fill_class_and_attribute_names(res_json, annotation_classes_dict)
794+
result = {
795+
"annotation_json":
796+
response.json(),
797+
"annotation_json_filename":
798+
common.get_annotation_json_name(image_name, project_type)
799+
}
800+
if project_type == "Pixel":
801+
response = requests.get(
802+
url=main_annotations["annotation_bluemap_path"]["url"],
803+
headers=main_annotations["annotation_bluemape_path"]["headers"]
804+
)
805+
if not response.ok:
806+
raise SABaseException(
807+
response.status_code,
808+
"Couldn't load annotations" + response.text
809+
)
810+
mask = io.BytesIO(response.content)
811+
result["annotation_mask"] = mask
812+
result["annotation_mask_filename"] = common.get_annotation_png_name(
813+
image_name
814+
)
815+
return result
820816

821817

822818
def download_image_annotations(project, image_name, local_dir_path):
@@ -956,17 +952,16 @@ def upload_image_annotations(
956952
response.status_code, "Couldn't upload annotation. " + response.text
957953
)
958954
res = response.json()
959-
if project_type == "Vector":
960-
res = res['objects']
961-
s3_session = boto3.Session(
962-
aws_access_key_id=res['accessKeyId'],
963-
aws_secret_access_key=res['secretAccessKey'],
964-
aws_session_token=res['sessionToken']
965-
)
966-
s3_resource = s3_session.resource('s3')
967-
bucket = s3_resource.Bucket(res["bucket"])
968-
bucket.put_object(Key=res['filePath'], Body=json.dumps(annotation_json))
969-
else: # pixel
955+
res = res['annotation_json_path']
956+
s3_session = boto3.Session(
957+
aws_access_key_id=res['accessKeyId'],
958+
aws_secret_access_key=res['secretAccessKey'],
959+
aws_session_token=res['sessionToken']
960+
)
961+
s3_resource = s3_session.resource('s3')
962+
bucket = s3_resource.Bucket(res["bucket"])
963+
bucket.put_object(Key=res['filePath'], Body=json.dumps(annotation_json))
964+
if project_type == "Pixel":
970965
if mask is None:
971966
raise SABaseException(0, "Pixel annotation should have mask.")
972967
if not isinstance(mask, io.BytesIO):

superannotate/db/project_images.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ def upload_image_to_project(
9696
path=f'/project/{project_id}/sdkImageUploadToken',
9797
params=params
9898
)
99-
if response.ok:
100-
res = response.json()
101-
prefix = res['filePath']
102-
else:
99+
if not response.ok:
103100
raise SABaseException(
104101
response.status_code, "Couldn't get upload token " + response.text
105102
)
103+
res = response.json()
104+
prefix = res['filePath']
106105
s3_session = boto3.Session(
107106
aws_access_key_id=res['accessKeyId'],
108107
aws_secret_access_key=res['secretAccessKey'],
@@ -120,7 +119,7 @@ def upload_image_to_project(
120119

121120
__create_image(
122121
[img_name], [key], project, annotation_status, prefix,
123-
images_info_and_array[2]
122+
[images_info_and_array[2]]
124123
)
125124

126125
while True:

superannotate/db/projects.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,11 @@ def __upload_images_to_aws_thread(
658658
bucket = s3_resource.Bucket(res["bucket"])
659659
prefix = res['filePath']
660660
uploaded_imgs = []
661+
uploaded_imgs_info = ([], [], [])
661662
for i in range(start_index, end_index):
662663
if i >= len_img_paths:
663664
break
664665
path = img_paths[i]
665-
key = prefix + f'{Path(path).name}'
666666
try:
667667
if from_s3_bucket is not None:
668668
file = io.BytesIO()
@@ -678,28 +678,34 @@ def __upload_images_to_aws_thread(
678678
with open(path, "rb") as f:
679679
file = io.BytesIO(f.read())
680680
images_array = get_image_array_to_upload(
681-
file, image_quality_in_editor, project["type"]
682-
)
683-
upload_image_array_to_s3(
684-
bucket, *images_array, key, project["type"]
681+
Path(path).name, file, image_quality_in_editor, project["type"]
685682
)
683+
key = upload_image_array_to_s3(bucket, *images_array, prefix)
686684
except Exception as e:
687685
logger.warning("Unable to upload image %s. %s", path, e)
688686
couldnt_upload[thread_id].append(path)
689687
continue
690688
else:
691689
uploaded[thread_id].append(path)
692690
uploaded_imgs.append(path)
691+
uploaded_imgs_info[0].append(Path(path).name)
692+
uploaded_imgs_info[1].append(key)
693+
uploaded_imgs_info[2].append(images_array[2])
693694
if len(uploaded_imgs) >= 100:
694695
__create_image(
695-
uploaded_imgs, project, annotation_status, prefix
696+
uploaded_imgs_info[0], uploaded_imgs_info[1], project,
697+
annotation_status, prefix, uploaded_imgs_info[2]
696698
)
697699
uploaded_imgs = []
698-
__create_image(uploaded_imgs, project, annotation_status, prefix)
700+
uploaded_imgs_info = ([], [], [])
701+
__create_image(
702+
uploaded_imgs_info[0], uploaded_imgs_info[1], project,
703+
annotation_status, prefix, uploaded_imgs_info[2]
704+
)
699705

700706

701707
def __create_image(
702-
img_names, img_paths, project, annotation_status, remote_dir, size
708+
img_names, img_paths, project, annotation_status, remote_dir, sizes
703709
):
704710
if len(img_paths) == 0:
705711
return
@@ -714,7 +720,7 @@ def __create_image(
714720
"annotation_status": annotation_status,
715721
"meta": {}
716722
}
717-
for img_name, img_path in zip(img_names, img_paths):
723+
for img_name, img_path, size in zip(img_names, img_paths, sizes):
718724
img_name_uuid = Path(img_path).name
719725
remote_path = remote_dir + f"{img_name_uuid}"
720726
data["images"].append({"name": img_name, "path": remote_path})
@@ -1572,6 +1578,7 @@ def upload_preannotations_from_folder_to_project(
15721578
)
15731579
#TODO: new endpoint to be delivered: use new endpoint to create annotation json for preannotations
15741580

1581+
15751582
def _upload_preannotations_from_folder_to_project(
15761583
project, folder_path, from_s3_bucket=None, recursive_subfolders=False
15771584
):

0 commit comments

Comments
 (0)