Skip to content

Commit

Permalink
Add functionality to filter images by object tags (#89)
Browse files Browse the repository at this point in the history
* Add option to filter by object tags to FilterImageByTag layer

* remove space

* update sdk version
  • Loading branch information
cxnt authored Oct 7, 2024
1 parent 575d0f7 commit 83d658b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "[Beta] Drag and drop interface for building custom DataOps pipelines",
"entrypoint": "python -m uvicorn src.main:app --host 0.0.0.0 --port 8000",
"headless": false,
"docker_image": "supervisely/data-nodes:1.0.21",
"docker_image": "supervisely/data-nodes:1.0.22",
"modal_template": "src/modal.html",
"modal_template_state": {
"modalityType": "images"
Expand Down
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
git+https://github.com/supervisely/supervisely.git@optimize-index

# supervisely==6.73.165
# supervisely==6.73.203
jsonschema==4.19.2
networkx==3.1
scikit-image==0.21.0
Expand Down
43 changes: 35 additions & 8 deletions src/compute/layers/processing/FilterImageByTagLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class FilterImageByTagLayer(Layer):
},
},
},
"condition": {"type": "string", "enum": ["with", "without"]},
"condition": {
"type": "string",
"enum": ["with_img", "without_img", "with_obj", "without_obj"],
},
},
}
},
Expand All @@ -46,20 +49,44 @@ def process(self, data_el: Tuple[ImageDescriptor, Annotation]):
img_desc, ann = data_el
condition = self.settings["condition"]

def has_tag(img_tags: Union[List[Tag], TagCollection], filter_tag: dict):
def has_tag_img(img_tags: Union[List[Tag], TagCollection], filter_tag: dict):
for img_tag in img_tags:
if img_tag.name == filter_tag["name"] and img_tag.value == filter_tag["value"]:
return True
return False

if condition == "with":
satisfies_cond = all(has_tag(ann.img_tags, f_tag) for f_tag in self.settings["tags"])
elif condition == "without":
satisfies_cond = all(
not has_tag(ann.img_tags, f_tag) for f_tag in self.settings["tags"]
)
def has_tag_obj(obj_tags: Union[List[Tag], TagCollection], filter_tag: dict):
for obj_tag in obj_tags:
if obj_tag.name == filter_tag["name"] and obj_tag.value == filter_tag["value"]:
return True
return False

if condition in ["with_img", "without_img"]:
if condition == "with_img":
satisfies_cond = all(
has_tag_img(ann.img_tags, f_tag) for f_tag in self.settings["tags"]
)
elif condition == "without_img":
satisfies_cond = all(
not has_tag_img(ann.img_tags, f_tag) for f_tag in self.settings["tags"]
)

elif condition in ["with_obj", "without_obj"]:
obj_tags = []
for label in ann.labels:
obj_tags.extend(label.tags)

if condition == "with_obj":
satisfies_cond = all(
has_tag_obj(obj_tags, f_tag) for f_tag in self.settings["tags"]
)
elif condition == "without_obj":
satisfies_cond = all(
not has_tag_obj(obj_tags, f_tag) for f_tag in self.settings["tags"]
)
else:
raise NotImplementedError()

if satisfies_cond:
yield data_el + tuple([0]) # branch 0
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Filter Images by Tags

`Filter Images by Tags` layer is used to route images down either the "True" or "False" branch, depending on specific tag inclusion and exclusion criteria. Users can define tag and filtering condition either "with tag" or "without tag".
`Filter Images by Tags` layer is used to route images down either the "True" or "False" branch, depending on specific tag inclusion and exclusion criteria. Users can define tag and filtering condition either "with tag" or "without tag".

# Settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def create_new_layer(cls, layer_id: Optional[str] = None):
condition_text = Text("Condition", status="text", font_size=get_text_font_size())
condition_selector = Select(
[
Select.Item("with", "Image has tag"),
Select.Item("without", "Image doesn't have tag"),
Select.Item("with_img", "Image has tag"),
Select.Item("without_img", "Image doesn't have tag"),
Select.Item("with_obj", "Object has tag"),
Select.Item("without_obj", "Object doesn't have tag"),
],
size="small",
)
Expand Down

0 comments on commit 83d658b

Please sign in to comment.