Skip to content

Commit 5796904

Browse files
committed
public urls with image names list
1 parent 4415f03 commit 5796904

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

superannotate/db/projects.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ def upload_images_to_project(
821821
def upload_images_from_public_urls_to_project(
822822
project,
823823
img_urls,
824+
img_names=None,
824825
annotation_status='NotStarted',
825826
image_quality_in_editor=None
826827
):
@@ -831,6 +832,8 @@ def upload_images_from_public_urls_to_project(
831832
:type project: str or dict
832833
:param img_urls: list of str objects to upload
833834
:type img_urls: list
835+
:param img_names: list of str names for each urls in img_url list
836+
:type img_names: list
834837
:param annotation_status: value to set the annotation statuses of the uploaded images NotStarted InProgress QualityCheck Returned Completed Skipped
835838
:type annotation_status: str
836839
:param image_quality_in_editor: image quality be seen in SuperAnnotate web annotation editor.
@@ -840,13 +843,17 @@ def upload_images_from_public_urls_to_project(
840843
:return: uploaded images' urls, uploaded images' filenames, duplicate images' filenames and not-uploaded images' urls
841844
:rtype: tuple of list of strs
842845
"""
846+
847+
if img_names is not None and len(img_names) != len(img_urls):
848+
raise SABaseException(0, "Not all image URLs have corresponding names.")
849+
843850
images_not_uploaded = []
844851
images_to_upload = []
845852
duplicate_images_filenames = []
846853
path_to_url = {}
847854
with tempfile.TemporaryDirectory() as save_dir_name:
848855
save_dir = Path(save_dir_name)
849-
for img_url in img_urls:
856+
for i, img_url in enumerate(img_urls):
850857
try:
851858
response = requests.get(img_url)
852859
response.raise_for_status()
@@ -856,12 +863,15 @@ def upload_images_from_public_urls_to_project(
856863
)
857864
images_not_uploaded.append(img_url)
858865
else:
859-
if response.headers.get('Content-Disposition') is not None:
860-
img_path = save_dir / cgi.parse_header(
861-
response.headers['Content-Disposition']
862-
)[1]['filename']
866+
if not img_names:
867+
if response.headers.get('Content-Disposition') is not None:
868+
img_path = save_dir / cgi.parse_header(
869+
response.headers['Content-Disposition']
870+
)[1]['filename']
871+
else:
872+
img_path = save_dir / basename(urlparse(img_url).path)
863873
else:
864-
img_path = save_dir / basename(urlparse(img_url).path)
874+
img_path = save_dir / img_names[i]
865875

866876
if str(img_path) in path_to_url.keys():
867877
duplicate_images_filenames.append(basename(img_path))

tests/test_upload_images_from_public_urls.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
test_root = Path().resolve() / 'tests'
77

8-
PROJECT_NAME = 'test_public_links_upload'
9-
108

119
def test_upload_images_from_public_urls_to_project():
10+
PROJECT_NAME = 'test_public_links_upload'
11+
1212
test_img_list = [
1313
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
1414
'https://www.pexels.com/photo/5450829/download/',
@@ -37,3 +37,37 @@ def test_upload_images_from_public_urls_to_project():
3737

3838
for image in images_in_project:
3939
assert image in uploaded_filenames
40+
41+
42+
def test_upload_images_from_public_to_project_with_image_name():
43+
PROJECT_NAME = 'test_public_links_upload2'
44+
45+
test_img_list = [
46+
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
47+
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
48+
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
49+
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940',
50+
'https://images.pexels.com/photos/3702354/pexels-photo-3702354.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'
51+
]
52+
53+
img_name_list = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg']
54+
55+
if sa.search_projects(PROJECT_NAME) != []:
56+
sa.delete_project(PROJECT_NAME)
57+
58+
proj_data = sa.create_project(PROJECT_NAME, "test", "Vector")
59+
uploaded_urls, uploaded_filenames, duplicate_filenames, not_uploaded_urls = sa.upload_images_from_public_urls_to_project(
60+
proj_data,
61+
test_img_list,
62+
img_name_list,
63+
annotation_status='InProgress',
64+
image_quality_in_editor="original"
65+
)
66+
images_in_project = sa.search_images(
67+
proj_data, annotation_status='InProgress'
68+
)
69+
# check how many images were uploaded and how many were not
70+
assert len(uploaded_urls) == 5
71+
assert len(duplicate_filenames) == 0
72+
assert len(uploaded_filenames) == 5
73+
assert len(not_uploaded_urls) == 0

0 commit comments

Comments
 (0)