Skip to content

Commit 86f4636

Browse files
committed
Fix returning deleted images
1 parent 0c09c5c commit 86f4636

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

superannotate/db/project_images.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import re
44
import time
5+
import uuid
56
from pathlib import Path
67

78
import boto3
@@ -15,8 +16,8 @@
1516
)
1617
from .project_api import get_project_metadata_bare
1718
from .projects import (
18-
__create_image, get_project_default_image_quality_in_editor,
19-
get_image_array_to_upload, upload_image_array_to_s3
19+
__create_image, get_image_array_to_upload,
20+
get_project_default_image_quality_in_editor, upload_image_array_to_s3
2021
)
2122

2223
logger = logging.getLogger("superannotate-python-sdk")
@@ -76,7 +77,7 @@ def upload_image_to_project(
7677
with open(img, "rb") as f:
7778
img = io.BytesIO(f.read())
7879
elif img.getbuffer().nbytes > common.MAX_IMAGE_SIZE:
79-
raise SAImageSizeTooLarge(file_size)
80+
raise SAImageSizeTooLarge(img.getbuffer().nbytes)
8081

8182
if image_name is not None:
8283
img_name = image_name
@@ -109,16 +110,18 @@ def upload_image_to_project(
109110
)
110111
s3_resource = s3_session.resource('s3')
111112
bucket = s3_resource.Bucket(res["bucket"])
112-
key = prefix + f'{img_name}'
113113
try:
114-
images_array = get_image_array_to_upload(
115-
img, image_quality_in_editor, project["type"]
114+
images_info_and_array = get_image_array_to_upload(
115+
img_name, img, image_quality_in_editor, project["type"]
116116
)
117-
upload_image_array_to_s3(bucket, *images_array, key, project["type"])
117+
key = upload_image_array_to_s3(bucket, *images_info_and_array, prefix)
118118
except Exception as e:
119119
raise SABaseException(0, "Couldn't upload to data server. " + e)
120120

121-
__create_image([img_name], project, annotation_status, prefix)
121+
__create_image(
122+
[img_name], [key], project, annotation_status, prefix,
123+
images_info_and_array[2]
124+
)
122125

123126
while True:
124127
try:

superannotate/db/projects.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from os.path import basename
1313
from pathlib import Path
1414
from urllib.parse import urlparse
15+
import uuid
1516

1617
import boto3
1718
import cv2
@@ -553,34 +554,30 @@ def create_empty_annotation(size, image_name):
553554

554555

555556
def upload_image_array_to_s3(
556-
bucket, size, orig_image, lores_image, huge_image, thumbnail_image, key,
557-
project_type
557+
bucket, img_name, img_name_hash, size, orig_image, lores_image, huge_image,
558+
thumbnail_image, prefix
558559
):
560+
key = prefix + img_name_hash
559561
bucket.put_object(Body=orig_image, Key=key)
560-
#TODO: uuid__lores.jpg
561-
bucket.put_object(Body=lores_image, Key=key + '___lores.jpg')
562-
562+
bucket.put_object(Body=lores_image, Key=key + '___lores.jpg')
563563
bucket.put_object(
564564
Body=huge_image,
565-
#TODO: uuid__huge.jpg
566565
Key=key + '___huge.jpg',
567566
Metadata={
568567
'height': str(size[1]),
569568
'width': str(size[0])
570569
}
571570
)
572571
bucket.put_object(Body=thumbnail_image, Key=key + '___thumb.jpg')
573-
# TODO: remove suffix objects, pixel: save uuid.json
574-
postfix_json = '___objects.json' if project_type == "Vector" else '___pixel.json'
575572
bucket.put_object(
576-
Body=json.dumps(create_empty_annotation(size,
577-
Path(key).name)),
578-
Key=key + postfix_json
573+
Body=json.dumps(create_empty_annotation(size, img_name)),
574+
Key=key + ".json"
579575
)
576+
return key
580577

581578

582579
def get_image_array_to_upload(
583-
byte_io_orig, image_quality_in_editor, project_type
580+
img_name, byte_io_orig, image_quality_in_editor, project_type
584581
):
585582
if image_quality_in_editor not in ["original", "compressed"]:
586583
raise SABaseException(0, "NA ImageQuality in get_image_array_to_upload")
@@ -634,7 +631,8 @@ def get_image_array_to_upload(
634631
byte_io_huge.seek(0)
635632
byte_io_orig.seek(0)
636633

637-
return (
634+
img_name_hash = str(uuid.uuid4()) + Path(img_name).suffix
635+
return img_name, img_name_hash, (
638636
width, height
639637
), byte_io_orig, byte_io_lores, byte_io_huge, byte_io_thumbs
640638

@@ -700,23 +698,32 @@ def __upload_images_to_aws_thread(
700698
__create_image(uploaded_imgs, project, annotation_status, prefix)
701699

702700

703-
def __create_image(img_paths, project, annotation_status, remote_dir):
704-
# print("Creating images ", len(img_paths))
701+
def __create_image(
702+
img_names, img_paths, project, annotation_status, remote_dir, size
703+
):
705704
if len(img_paths) == 0:
706705
return
707706
team_id, project_id = project["team_id"], project["id"]
708707
data = {
709708
"project_id": str(project_id),
710-
"team_id": str(team_id)
711-
"images": [],
712-
"annotation_status": annotation_status
713-
#TODO: {image_name: {heigh, width, annotation_path})
714-
# "meta": {"a.jpg": {"height":78, "width":56, "annotation_json_path": "remote_path/a_uuid.json" }, "b.jpg": {"height":78, "width":56, "annotation_json_path": "remote_path/b_uuid.json" } }
709+
"team_id": str(team_id),
710+
"images": [],
711+
"annotation_status": annotation_status,
712+
"team_id": str(team_id),
713+
"images": [],
714+
"annotation_status": annotation_status,
715+
"meta": {}
715716
}
716-
for img_path in img_paths:
717-
img_name = Path(img_path).name
718-
remote_path = remote_dir + f"{img_name}"
717+
for img_name, img_path in zip(img_names, img_paths):
718+
img_name_uuid = Path(img_path).name
719+
remote_path = remote_dir + f"{img_name_uuid}"
719720
data["images"].append({"name": img_name, "path": remote_path})
721+
data["meta"][img_name] = {
722+
"width": size[0],
723+
"height": size[1],
724+
"annotation_json_path": remote_path + ".json",
725+
"annotation_bluemap_path": remote_path + ".png"
726+
}
720727

721728
response = _api.send_request(
722729
req_type='POST', path='/image/ext-create', json_req=data
@@ -727,7 +734,7 @@ def __create_image(img_paths, project, annotation_status, remote_dir):
727734
)
728735

729736

730-
o_project(
737+
def upload_images_to_project(
731738
project,
732739
img_paths,
733740
annotation_status="NotStarted",
@@ -1655,7 +1662,7 @@ def _upload_preannotations_from_folder_to_project(
16551662
annotation_classes_dict = get_annotation_classes_name_to_id(
16561663
annotation_classes
16571664
)
1658-
1665+
16591666
response = _api.send_request(
16601667
req_type='GET',
16611668
path=f'/project/{project_id}/preannotation',

0 commit comments

Comments
 (0)