Skip to content

Commit

Permalink
fix unused coco merge util (#353)
Browse files Browse the repository at this point in the history
* fix unused coco merge util

* add +1 to indexes

* fix coco util tests

* fix tests
  • Loading branch information
fcakyon authored Jan 13, 2022
1 parent 32f507a commit 03f2b88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions sahi/utils/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -1684,18 +1684,18 @@ def merge(coco_dict1: dict, coco_dict2: dict, desired_name2id: dict = None) -> d
temp_coco_dict2 = update_categories(desired_name2id, temp_coco_dict2)

# calculate first image and annotation index of the second coco file
last_image_id = coco_dict1["images"][-1]["id"]
last_annotation_id = coco_dict1["annotations"][-1]["id"]
max_image_id = np.array([image["id"] for image in coco_dict1["images"]]).max()
max_annotation_id = np.array([annotation["id"] for annotation in coco_dict1["annotations"]]).max()

merged_coco_dict = temp_coco_dict1

for image in temp_coco_dict2["images"]:
image["id"] += last_image_id
image["id"] += max_image_id + 1
merged_coco_dict["images"].append(image)

for annotation in temp_coco_dict2["annotations"]:
annotation["image_id"] += last_image_id
annotation["id"] += last_annotation_id
annotation["image_id"] += max_image_id
annotation["id"] += max_annotation_id + 1
merged_coco_dict["annotations"].append(annotation)

return merged_coco_dict
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cocoutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def test_merge(self):
self.assertEqual(merged_coco_dict["annotations"][6]["id"], 7)
self.assertEqual(merged_coco_dict["annotations"][7]["category_id"], 2)
self.assertEqual(merged_coco_dict["annotations"][7]["image_id"], 2)
self.assertEqual(merged_coco_dict["annotations"][7]["id"], 8)
self.assertEqual(merged_coco_dict["annotations"][7]["id"], 9)

def test_merge_from_list(self):
from sahi.utils.coco import merge_from_list
Expand Down Expand Up @@ -496,11 +496,11 @@ def test_merge_from_list(self):
)
self.assertEqual(
merged_coco_dict["annotations"][12]["id"],
13,
15,
)
self.assertEqual(
merged_coco_dict["annotations"][12]["image_id"],
3,
4,
)
self.assertEqual(
merged_coco_dict["annotations"][9]["category_id"],
Expand Down

0 comments on commit 03f2b88

Please sign in to comment.