Skip to content

Commit 873c662

Browse files
committed
add filter by tag
1 parent 66670b7 commit 873c662

File tree

9 files changed

+166
-4794
lines changed

9 files changed

+166
-4794
lines changed

docs/source/superannotate.sdk.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ ________________________
341341
.. _ref_filter_annotation_instances:
342342
.. autofunction:: superannotate.filter_annotation_instances
343343
.. autofunction:: superannotate.filter_images_by_comments
344+
.. autofunction:: superannotate.filter_images_by_tags
344345

345346
----------
346347

superannotate/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
project_type_str_to_int, user_role_str_to_int
2323
)
2424
from .dataframe_filtering import (
25-
filter_annotation_instances, filter_images_by_comments
25+
filter_annotation_instances, filter_images_by_comments,
26+
filter_images_by_tags
2627
)
2728
from .db.annotation_classes import (
2829
create_annotation_class, create_annotation_classes_from_classes_json,

superannotate/analytics/common.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def df_to_annotations(df, output_dir):
1111
"""Converts and saves pandas DataFrame annotation info (see aggregate_annotations_as_df) in output_dir
12-
The DataFrame should have columns: "imageName", "classNmae", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", trackingId", "probability", "pointLabels", "meta", "commentResolved", "classColor", "groupId"
12+
The DataFrame should have columns: "imageName", "className", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", trackingId", "probability", "pointLabels", "meta", "commentResolved", "classColor", "groupId"
1313
1414
Currently only works for Vector projects.
1515
@@ -120,6 +120,7 @@ def aggregate_annotations_as_df(
120120
project_root,
121121
include_classes_wo_annotations=False,
122122
include_comments=False,
123+
include_tags=False,
123124
verbose=True
124125
):
125126
"""Aggregate annotations as pandas dataframe from project root.
@@ -154,7 +155,6 @@ def aggregate_annotations_as_df(
154155
"error": [],
155156
"locked": [],
156157
"visible": [],
157-
"tag": [],
158158
"trackingId": [],
159159
"probability": [],
160160
"pointLabels": [],
@@ -172,6 +172,8 @@ def aggregate_annotations_as_df(
172172

173173
if include_comments:
174174
annotation_data["commentResolved"] = []
175+
if include_tags:
176+
annotation_data["tag"] = []
175177

176178
classes_path = Path(project_root) / "classes" / "classes.json"
177179
if not classes_path.is_file():
@@ -245,13 +247,14 @@ def __get_image_metadata(image_name, annotations):
245247
__append_annotation(annotation_dict)
246248
continue
247249
if annotation_type == "tag":
248-
annotation_tag = annotation["name"]
249-
annotation_dict = {
250-
"type": annotation_type,
251-
"tag": annotation_tag
252-
}
253-
annotation_dict.update(image_metadata)
254-
__append_annotation(annotation_dict)
250+
if include_tags:
251+
annotation_tag = annotation["name"]
252+
annotation_dict = {
253+
"type": annotation_type,
254+
"tag": annotation_tag
255+
}
256+
annotation_dict.update(image_metadata)
257+
__append_annotation(annotation_dict)
255258
continue
256259
annotation_instance_id += 1
257260
annotation_class_name = annotation.get("className")

superannotate/dataframe_filtering.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ def filter_images_by_comments(
4242
return list(images)
4343

4444

45+
def filter_images_by_tags(annotations_df, include=None, exclude=None):
46+
"""Filter images on tags
47+
48+
:param annotations_df: pandas DataFrame of project annotations
49+
:type annotations_df: pandas.DataFrame
50+
:param include: include images with given tags
51+
:type include: list of strs
52+
:param exclude: exclude images with given tags
53+
:type exclude: list of strs
54+
55+
:return: filtered image names
56+
:rtype: list of strs
57+
58+
"""
59+
60+
df = annotations_df[annotations_df["type"] == "tag"]
61+
images = set(df["imageName"].dropna().unique())
62+
63+
if include:
64+
include_images = set(
65+
df[df["tag"].isin(include)]["imageName"].dropna().unique()
66+
)
67+
images = images.intersection(include_images)
68+
69+
if exclude:
70+
exclude_images = set(
71+
df[df["tag"].isin(exclude)]["imageName"].dropna().unique()
72+
)
73+
74+
images = images.difference(exclude_images)
75+
76+
return list(images)
77+
78+
4579
def filter_annotation_instances(annotations_df, include=None, exclude=None):
4680
"""Filter annotation instances from project annotations pandas DataFrame.
4781

0 commit comments

Comments
 (0)