Skip to content

Commit 3633bdc

Browse files
committed
Copy annotation states with images
1 parent 9f5036e commit 3633bdc

File tree

2 files changed

+101
-7
lines changed

2 files changed

+101
-7
lines changed

superannotate/db/project_images.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def upload_image_to_project(
133133
break
134134

135135

136-
def _copy_images(source_project, destination_project, image_names=None):
136+
def _copy_images(
137+
source_project, destination_project, image_names, include_annotations,
138+
copy_annotation_status, copy_pin
139+
):
137140
source_project, source_project_folder = source_project
138141
destination_project, destination_project_folder = destination_project
139142
if source_project["id"] != destination_project["id"]:
@@ -158,10 +161,48 @@ def _copy_images(source_project, destination_project, image_names=None):
158161
response.status_code, "Couldn't copy images " + response.text
159162
)
160163

164+
for image_name in image_names:
165+
if include_annotations:
166+
annotations = get_image_annotations(
167+
(source_project, source_project_folder), image_name
168+
)
169+
if annotations["annotation_json"] is not None:
170+
if "annotation_mask" in annotations:
171+
upload_image_annotations(
172+
(destination_project, destination_project_folder),
173+
image_name, annotations["annotation_json"],
174+
annotations["annotation_mask"]
175+
)
176+
else:
177+
upload_image_annotations(
178+
(destination_project, destination_project_folder),
179+
image_name, annotations["annotation_json"]
180+
)
181+
if copy_annotation_status or copy_pin:
182+
img_metadata = get_image_metadata(
183+
(source_project, source_project_folder), image_name
184+
)
185+
if copy_annotation_status:
186+
set_image_annotation_status(
187+
(destination_project, destination_project_folder),
188+
image_name, img_metadata["annotation_status"]
189+
)
190+
if copy_pin:
191+
pin_image(
192+
(destination_project, destination_project_folder),
193+
image_name, img_metadata["is_pinned"]
194+
)
161195
return response.json()
162196

163197

164-
def copy_images(source_project, destination_project, image_names=None):
198+
def copy_images(
199+
source_project,
200+
destination_project,
201+
image_names=None,
202+
include_annotations=True,
203+
copy_annotation_status=True,
204+
copy_pin=True
205+
):
165206
source_project, source_project_folder = get_project_and_folder_metadata(
166207
source_project
167208
)
@@ -172,7 +213,8 @@ def copy_images(source_project, destination_project, image_names=None):
172213
image_names = search_images((source_project, source_project_folder))
173214
res = _copy_images(
174215
(source_project, source_project_folder),
175-
(destination_project, destination_project_folder), image_names
216+
(destination_project, destination_project_folder), image_names,
217+
include_annotations, copy_annotation_status, copy_pin
176218
)
177219
logger.info(
178220
"Copied images %s from %s to %s. Number of skipped images %s",
@@ -215,7 +257,14 @@ def delete_images(project, image_names):
215257
logger.info("Images %s deleted in project %s", image_names, project["name"])
216258

217259

218-
def move_images(source_project, destination_project, image_names=None):
260+
def move_images(
261+
source_project,
262+
destination_project,
263+
image_names=None,
264+
include_annotations=True,
265+
copy_annotation_status=True,
266+
copy_pin=True,
267+
):
219268
source_project, source_project_folder = get_project_and_folder_metadata(
220269
source_project
221270
)
@@ -226,7 +275,8 @@ def move_images(source_project, destination_project, image_names=None):
226275
image_names = search_images((source_project, source_project_folder))
227276
_copy_images(
228277
(source_project, source_project_folder),
229-
(destination_project, destination_project_folder), image_names
278+
(destination_project, destination_project_folder), image_names,
279+
include_annotations, copy_annotation_status, copy_pin
230280
)
231281
delete_images((source_project, source_project_folder), image_names)
232282
logger.info(
@@ -308,13 +358,13 @@ def copy_image(
308358
if annotations["annotation_json"] is not None:
309359
if "annotation_mask" in annotations:
310360
upload_image_annotations(
311-
destination_project, new_name,
361+
(destination_project, destination_project_folder), new_name,
312362
annotations["annotation_json"],
313363
annotations["annotation_mask"]
314364
)
315365
else:
316366
upload_image_annotations(
317-
destination_project, new_name,
367+
(destination_project, destination_project_folder), new_name,
318368
annotations["annotation_json"]
319369
)
320370
if copy_annotation_status:

tests/test_folders.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,47 @@ def test_move_images2(tmpdir):
331331

332332
num_images = sa.get_project_image_count(project)
333333
assert num_images == 0
334+
335+
336+
def test_copy_images2(tmpdir):
337+
PROJECT_NAME = "test copy folder annotation images"
338+
tmpdir = Path(tmpdir)
339+
340+
projects_found = sa.search_projects(PROJECT_NAME, return_metadata=True)
341+
for pr in projects_found:
342+
sa.delete_project(pr)
343+
344+
project = sa.create_project(PROJECT_NAME, 'test', 'Vector')
345+
sa.create_annotation_classes_from_classes_json(
346+
project, FROM_FOLDER / "classes" / "classes.json"
347+
)
348+
sa.create_folder(project, "folder1")
349+
project = PROJECT_NAME + "/folder1"
350+
sa.upload_images_from_folder_to_project(
351+
project, FROM_FOLDER, annotation_status="InProgress"
352+
)
353+
354+
sa.upload_annotations_from_folder_to_project(project, FROM_FOLDER)
355+
num_images = sa.get_project_image_count(project)
356+
assert num_images == 4
357+
358+
sa.create_folder(PROJECT_NAME, "folder2")
359+
project2 = PROJECT_NAME + "/folder2"
360+
num_images = sa.get_project_image_count(project2)
361+
assert num_images == 0
362+
363+
sa.copy_images(
364+
project,
365+
project2, ["example_image_2.jpg", "example_image_3.jpg"],
366+
include_annotations=False,
367+
copy_annotation_status=False,
368+
copy_pin=False
369+
)
370+
371+
num_images = sa.get_project_image_count(project2)
372+
assert num_images == 2
373+
374+
# ann1 = sa.get_image_annotations(project, "example_image_2.jpg")
375+
# ann2 = sa.get_image_annotations(project2, "example_image_2.jpg")
376+
377+
# assert ann1 == ann2

0 commit comments

Comments
 (0)