Commit d369931a authored by Sam Tsai's avatar Sam Tsai Committed by Facebook GitHub Bot
Browse files

add option to filter empty annotations

Summary: Add option for controlling empty annotation filtering.

Reviewed By: zhanghang1989

Differential Revision: D34365265

fbshipit-source-id: 261c6879636f19138de781098f47dee4909de9e7
parent cb41f780
......@@ -249,6 +249,7 @@ def convert_to_dict_list(
anns: List[Dict],
dataset_name: Optional[str] = None,
image_direct_copy_keys: Optional[List[str]] = None,
filter_empty_annotations: Optional[bool] = True,
) -> List[Dict]:
ann_error_report = {
......@@ -263,7 +264,10 @@ def convert_to_dict_list(
name: ErrorEntry(name, msg, 0)
for name, msg in [
("ignore_image_root", f"Image root ignored {image_root}"),
("no_annotations", "Image filtered"),
(
"no_annotations",
"Image filtered" if filter_empty_annotations else "Warning",
),
]
}
ex_warning_fn = None
......@@ -299,7 +303,8 @@ def convert_to_dict_list(
)
if len(converted_anns) == 0:
image_error_report["no_annotations"].cnt += 1
continue
if filter_empty_annotations:
continue
record["annotations"] = converted_anns
# Copy keys if additionally asked
......@@ -352,6 +357,7 @@ def extended_coco_load(
dataset_name: Optional[str] = None,
loaded_json: Optional[str] = None,
image_direct_copy_keys: List[str] = None,
filter_empty_annotations: Optional[bool] = True,
) -> List[Dict]:
"""
Load a json file with COCO's annotation format.
......@@ -419,6 +425,7 @@ def extended_coco_load(
anns,
dataset_name,
image_direct_copy_keys=image_direct_copy_keys,
filter_empty_annotations=filter_empty_annotations,
)
......
......@@ -86,6 +86,7 @@ class TestD2GoDatasets(unittest.TestCase):
img_list = [
{"id": 0, "width": 50, "height": 50, "file_name": "a.png"},
{"id": 1, "width": 50, "height": 50, "file_name": "b.png"},
{"id": 2, "width": 50, "height": 50, "file_name": "b.png"},
]
ann_list = [
[
......@@ -140,17 +141,18 @@ class TestD2GoDatasets(unittest.TestCase):
"bbox": [0, 0, 0, 0],
},
],
[],
]
out_dict_list = extended_coco.convert_to_dict_list(
"",
[0],
img_list,
ann_list,
)
out_dict_list = extended_coco.convert_to_dict_list("", [0], img_list, ann_list)
self.assertEqual(len(out_dict_list), 1)
self.assertEqual(len(out_dict_list[0]["annotations"]), 1)
out_dict_list = extended_coco.convert_to_dict_list(
"", [0], img_list, ann_list, filter_empty_annotations=False
)
self.assertEqual(len(out_dict_list), 3)
@tempdir
def test_coco_injection(self, tmp_dir):
image_dir, json_file = create_test_images_and_dataset_json(tmp_dir)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment