def test_frame_merge_between_predicted_and_user(skeleton, centered_pair_vid): user_inst = Instance(skeleton=skeleton, points={skeleton.nodes[0]: Point(1, 2)},) user_labels = Labels( [LabeledFrame(video=centered_pair_vid, frame_idx=0, instances=[user_inst],)] ) pred_inst = PredictedInstance( skeleton=skeleton, points={skeleton.nodes[0]: PredictedPoint(1, 2, score=1.0)}, score=1.0, ) pred_labels = Labels( [LabeledFrame(video=centered_pair_vid, frame_idx=0, instances=[pred_inst],)] ) # Merge predictions into current labels dataset _, _, new_conflicts = Labels.complex_merge_between( user_labels, new_labels=pred_labels, unify=False, # since we used match_to when loading predictions file ) # new predictions should replace old ones Labels.finish_complex_merge(user_labels, new_conflicts) # We should be able to cleanly merge the user and the predicted instance, # and we want to retain both even though they perfectly match. assert user_inst in user_labels[0].instances assert pred_inst in user_labels[0].instances assert len(user_labels[0].instances) == 2
def test_frame_merge_predicted_and_user(skeleton, centered_pair_vid): user_inst = Instance( skeleton=skeleton, points={skeleton.nodes[0]: Point(1, 2)}, ) user_frame = LabeledFrame( video=centered_pair_vid, frame_idx=0, instances=[user_inst], ) pred_inst = PredictedInstance( skeleton=skeleton, points={skeleton.nodes[0]: PredictedPoint(1, 2, score=1.0)}, score=1.0, ) pred_frame = LabeledFrame( video=centered_pair_vid, frame_idx=0, instances=[pred_inst], ) LabeledFrame.complex_frame_merge(user_frame, pred_frame) # We should be able to cleanly merge the user and the predicted instance, # and we want to retain both even though they perfectly match. assert user_inst in user_frame.instances assert pred_inst in user_frame.instances assert len(user_frame.instances) == 2
def simple_predictions(): video = Video.from_filename("video.mp4") skeleton = Skeleton() skeleton.add_node("a") skeleton.add_node("b") track_a = Track(0, "a") track_b = Track(0, "b") labels = Labels() instances = [] instances.append( PredictedInstance( skeleton=skeleton, score=2, track=track_a, points=dict(a=PredictedPoint(1, 1, score=0.5), b=PredictedPoint(1, 1, score=0.5)), )) instances.append( PredictedInstance( skeleton=skeleton, score=5, track=track_b, points=dict(a=PredictedPoint(1, 1, score=0.7), b=PredictedPoint(1, 1, score=0.7)), )) labeled_frame = LabeledFrame(video, frame_idx=0, instances=instances) labels.append(labeled_frame) instances = [] instances.append( PredictedInstance( skeleton=skeleton, score=3, track=track_a, points=dict(a=PredictedPoint(4, 5, score=1.5), b=PredictedPoint(1, 1, score=1.0)), )) instances.append( PredictedInstance( skeleton=skeleton, score=6, track=track_b, points=dict(a=PredictedPoint(6, 13, score=1.7), b=PredictedPoint(1, 1, score=1.0)), )) labeled_frame = LabeledFrame(video, frame_idx=1, instances=instances) labels.append(labeled_frame) return labels
def test_predicted_points_array_with_score(skeleton): pred_inst = PredictedInstance( skeleton=skeleton, points={ skeleton.nodes[0]: PredictedPoint(1, 2, score=0.3), skeleton.nodes[1]: PredictedPoint(4, 5, score=0.6, visible=False), }, score=1.0, ) pts = pred_inst.points_and_scores_array # Make sure we got (x, y, score) for first point assert pts[0, 0] == 1 assert pts[0, 1] == 2 assert pts[0, 2] == 0.3 # Make sure invisible point has NaNs assert np.isnan(pts[1, 0])
def test_constructor(): p = Point(x=1.0, y=2.0, visible=False, complete=True) assert p.x == 1.0 assert p.y == 2.0 assert p.visible == False assert p.complete == True p = PredictedPoint(x=1.0, y=2.0, visible=False, complete=True, score=0.3) assert p.x == 1.0 assert p.y == 2.0 assert p.visible == False assert p.complete == True assert p.score == 0.3
def get_frame_predicted_instances(video_id, frame_idx): points = predicted_points is_in_frame = (points["videoId"] == video_id) & ( points["frameIdx"] == frame_idx ) if not is_in_frame.any(): return [] instances = [] frame_instance_ids = np.unique(points["instanceId"][is_in_frame]) for i, instance_id in enumerate(frame_instance_ids): is_instance = is_in_frame & (points["instanceId"] == instance_id) track_id = predicted_instances.loc[ predicted_instances["id"] == instance_id ]["trackId"].values[0] match_score = predicted_instances.loc[ predicted_instances["id"] == instance_id ]["matching_score"].values[0] track_score = predicted_instances.loc[ predicted_instances["id"] == instance_id ]["tracking_score"].values[0] instance_points = { data["skeleton"]["nodeNames"][n]: PredictedPoint( x, y, visible=v, score=confidence ) for x, y, n, v, confidence in zip( *[ points[k][is_instance] for k in ["x", "y", "node", "visible", "confidence"] ] ) } instance = PredictedInstance( skeleton=skeleton, points=instance_points, track=tracks[track_id], score=match_score, ) instances.append(instance) return instances
import numpy as np import pytest from sleap.instance import Point, PredictedPoint, PointArray, PredictedPointArray @pytest.mark.parametrize( "p1", [ Point(0.0, 0.0), PredictedPoint(0.0, 0.0, 0.0), PointArray(3)[0], PredictedPointArray(3)[0], ], ) def test_point(p1): """ Test the Point and PredictedPoint API. This is mainly a safety check to make sure numpy record array stuff doesn't change """ # Make sure we are getting Points or PredictedPoints only. # This makes sure that PointArray(3)[0] returns a point for # example assert type(p1) in [PredictedPoint, Point] # Check getters and setters p1.x = 3.0 assert p1.x == 3.0 if type(p1) is PredictedPoint: