Skip to content

Commit

Permalink
Fixing #3277 (#3279)
Browse files Browse the repository at this point in the history
* fixing filter_keypoints() bug

* adding dynamic doc test
  • Loading branch information
brimoor authored Jul 11, 2023
1 parent a23b864 commit ca552ff
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 18 deletions.
8 changes: 5 additions & 3 deletions fiftyone/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -9787,7 +9787,7 @@ def _make_set_field_pipeline(
self,
field,
expr,
embedded_root,
embedded_root=embedded_root,
allow_missing=allow_missing,
new_field=new_field,
context=context,
Expand Down Expand Up @@ -10367,7 +10367,9 @@ def _parse_field_name(
other_list_fields = sorted(other_list_fields)

def _replace(path):
return ".".join([new_field] + path.split(".")[1:])
n = new_field.count(".") + 1
chunks = path.split(".", n)
return new_field + "." + chunks[-1] if len(chunks) > n else new_field

if new_field:
field_name = _replace(field_name)
Expand Down Expand Up @@ -10458,7 +10460,7 @@ def _make_set_field_pipeline(
sample_collection,
field,
expr,
embedded_root,
embedded_root=False,
allow_missing=False,
new_field=None,
context=None,
Expand Down
21 changes: 6 additions & 15 deletions fiftyone/core/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,6 @@ def to_mongo(self, sample_collection):
_, points_path = sample_collection._get_label_field_path(
self._field, "points"
)
new_field = self._get_new_field(sample_collection)

pipeline = []

Expand Down Expand Up @@ -2808,25 +2807,17 @@ def to_mongo(self, sample_collection):

if self._only_matches:
# Remove Keypoint objects with no points after filtering
has_points = (
F("points").filter(F()[0] != float("nan")).length() > 0
)
if is_list_field:
has_points = (
F("points").filter(F()[0] != float("nan")).length() > 0
)
match_expr = F("keypoints").filter(has_points)
only_expr = F().filter(has_points)
else:
field, _ = sample_collection._handle_frame_field(new_field)
has_points = (
F(field + ".points")
.filter(F()[0] != float("nan"))
.length()
> 0
)
match_expr = has_points.if_else(F(field), None)
only_expr = has_points.if_else(F(), None)

_pipeline, _ = sample_collection._make_set_field_pipeline(
root_path,
match_expr,
embedded_root=True,
only_expr,
allow_missing=True,
new_field=self._new_field,
)
Expand Down
146 changes: 146 additions & 0 deletions tests/unittests/view_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,152 @@ def test_filter_keypoints(self):
view = dataset.filter_keypoints("kps", labels=[])
self.assertEqual(len(view), 0)

def test_filter_keypoints_embedded_document(self):
sample1 = fo.Sample(
filepath="image1.jpg",
dynamic=fo.DynamicEmbeddedDocument(
kp=fo.Keypoint(
label="person",
points=[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
confidence=[0.5, 0.6, 0.7, 0.8, 0.9],
),
kps=fo.Keypoints(
keypoints=[
fo.Keypoint(
label="person",
points=[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
confidence=[0.5, 0.6, 0.7, 0.8, 0.9],
),
fo.Keypoint(),
]
),
),
)

sample2 = fo.Sample(filepath="image2.jpg")

dataset = fo.Dataset()
dataset.add_samples([sample1, sample2], dynamic=True)

dataset.default_skeleton = fo.KeypointSkeleton(
labels=["nose", "left eye", "right eye", "left ear", "right ear"],
edges=[[0, 1, 2, 0], [0, 3], [0, 4]],
)

count_nans = lambda points: len([p for p in points if np.isnan(p[0])])

#
# Test `Keypoint` sample fields
#

# only_matches=True
view = dataset.filter_keypoints(
"dynamic.kp", filter=F("confidence") > 0.75
)
self.assertEqual(len(view), 1)
sample = view.first()
self.assertEqual(len(sample["dynamic.kp"].points), 5)
self.assertEqual(count_nans(sample["dynamic.kp"].points), 3)

# only_matches=False
view = dataset.filter_keypoints(
"dynamic.kp", filter=F("confidence") > 0.75, only_matches=False
)
self.assertEqual(len(view), 2)
sample = view.first()
self.assertEqual(len(sample["dynamic.kp"].points), 5)
self.assertEqual(count_nans(sample["dynamic.kp"].points), 3)

# view with no matches
view = dataset.filter_keypoints(
"dynamic.kp", filter=F("confidence") > 0.95
)
self.assertEqual(len(view), 0)

# only_matches=True
view = dataset.filter_keypoints(
"dynamic.kp", labels=["left eye", "right eye"]
)
self.assertEqual(len(view), 1)
sample = view.first()
self.assertEqual(len(sample["dynamic.kp"].points), 5)
self.assertEqual(count_nans(sample["dynamic.kp"].points), 3)

# only_matches=False
view = dataset.filter_keypoints(
"dynamic.kp", labels=["left eye", "right eye"], only_matches=False
)
self.assertEqual(len(view), 2)
sample = view.first()
self.assertEqual(len(sample["dynamic.kp"].points), 5)
self.assertEqual(count_nans(sample["dynamic.kp"].points), 3)

# view with no matches
view = dataset.filter_keypoints("dynamic.kp", labels=[])
self.assertEqual(len(view), 0)

#
# Test `Keypoints` sample fields
#

# only_matches=True
view = dataset.filter_keypoints(
"dynamic.kps", filter=F("confidence") > 0.75
)
self.assertEqual(len(view), 1)
self.assertEqual(view.count("dynamic.kps.keypoints"), 1)
sample = view.first()
self.assertEqual(len(sample["dynamic.kps"].keypoints[0].points), 5)
self.assertEqual(
count_nans(sample["dynamic.kps"].keypoints[0].points), 3
)

# only_matches=False
view = dataset.filter_keypoints(
"dynamic.kps", filter=F("confidence") > 0.75, only_matches=False
)
self.assertEqual(len(view), 2)
self.assertEqual(view.count("dynamic.kps.keypoints"), 2)
sample = view.first()
self.assertEqual(len(sample["dynamic.kps"].keypoints[0].points), 5)
self.assertEqual(
count_nans(sample["dynamic.kps"].keypoints[0].points), 3
)

# view with no matches
view = dataset.filter_keypoints(
"dynamic.kps", filter=F("confidence") > 0.95
)
self.assertEqual(len(view), 0)

# only_matches=True
view = dataset.filter_keypoints(
"dynamic.kps", labels=["left eye", "right eye"]
)
self.assertEqual(len(view), 1)
self.assertEqual(view.count("dynamic.kps.keypoints"), 1)
sample = view.first()
self.assertEqual(len(sample["dynamic.kps"].keypoints[0].points), 5)
self.assertEqual(
count_nans(sample["dynamic.kps"].keypoints[0].points), 3
)

# only_matches=False
view = dataset.filter_keypoints(
"dynamic.kps", labels=["left eye", "right eye"], only_matches=False
)
self.assertEqual(len(view), 2)
self.assertEqual(view.count("dynamic.kps.keypoints"), 2)
sample = view.first()
self.assertEqual(len(sample["dynamic.kps"].keypoints[0].points), 5)
self.assertEqual(
count_nans(sample["dynamic.kps"].keypoints[0].points), 3
)

# view with no matches
view = dataset.filter_keypoints("dynamic.kps", labels=[])
self.assertEqual(len(view), 0)

def test_filter_keypoints_frames(self):
sample1 = fo.Sample(filepath="video1.mp4")
sample1.frames[1] = fo.Frame(
Expand Down

0 comments on commit ca552ff

Please sign in to comment.