def test_save_labels_with_frame_data(multi_skel_vid_labels, tmpdir, format): """ Test saving and loading a labels dataset with frame data included as JSON. """ # Lets take a subset of the labels so this doesn't take too long multi_skel_vid_labels.labeled_frames = multi_skel_vid_labels.labeled_frames[ 5:30] filename = os.path.join(tmpdir, "test.json") Labels.save_json( multi_skel_vid_labels, filename=filename, save_frame_data=True, frame_data_format=format, ) # Load the data back in loaded_labels = Labels.load_json(f"{filename}.zip") # Check that we have the same thing _check_labels_match(multi_skel_vid_labels, loaded_labels, format=format) # Make sure we can load twice loaded_labels = Labels.load_json(f"{filename}.zip")
def test_load_labels_json_old(tmpdir): new_file_path = os.path.join(tmpdir, "centered_pair_v2.json") # Function to run some checks on loaded labels def check_labels(labels): skel_node_names = [ "head", "neck", "thorax", "abdomen", "wingL", "wingR", "forelegL1", "forelegL2", "forelegL3", "forelegR1", "forelegR2", "forelegR3", "midlegL1", "midlegL2", "midlegL3", "midlegR1", "midlegR2", "midlegR3", "hindlegL1", "hindlegL2", "hindlegL3", "hindlegR1", "hindlegR2", "hindlegR3", ] # Do some basic checks assert len(labels) == 70 # Make sure we only create one video object and it works assert len({label.video for label in labels}) == 1 assert labels[0].video.get_frame(0).shape == (384, 384, 1) # Check some frame objects. assert labels[0].frame_idx == 0 assert labels[40].frame_idx == 573 # Check the skeleton assert labels[0].instances[0].skeleton.node_names == skel_node_names labels = Labels.load_json("tests/data/json_format_v1/centered_pair.json") check_labels(labels) # Save out to new JSON format Labels.save_json(labels, new_file_path) # Reload and check again. new_labels = Labels.load_json(new_file_path) check_labels(new_labels)
def test_labels_json(tmpdir, multi_skel_vid_labels): json_file_path = os.path.join(tmpdir, "dataset.json") if os.path.isfile(json_file_path): os.remove(json_file_path) # Save to json Labels.save_json(labels=multi_skel_vid_labels, filename=json_file_path) # Make sure the filename is there assert os.path.isfile(json_file_path) # Lets load the labels back in and make sure we haven't lost anything. loaded_labels = Labels.load_json(json_file_path) # Check that we have the same thing _check_labels_match(multi_skel_vid_labels, loaded_labels) # Check that we don't have the very same objects assert not multi_skel_vid_labels.skeletons[0] is loaded_labels.skeletons[0] assert not multi_skel_vid_labels.nodes[3] in loaded_labels.nodes assert not multi_skel_vid_labels.videos[0] is loaded_labels.videos[0] # Reload json using objects from original labels # We'll also test load_file() here loaded_labels = Labels.load_file(json_file_path, match_to=multi_skel_vid_labels) # Check that we now do have the same objects assert multi_skel_vid_labels.skeletons[0] in loaded_labels.skeletons assert multi_skel_vid_labels.nodes[3] in loaded_labels.nodes assert multi_skel_vid_labels.videos[0] in loaded_labels.videos
return np.stack(points_arrays) def point_match_count(dist_array: np.ndarray, thresh: float = 5) -> int: """Given an array of distances, returns number which are <= threshold.""" return np.sum(dist_array[~np.isnan(dist_array)] <= thresh) def point_nonmatch_count(dist_array: np.ndarray, thresh: float = 5) -> int: """Given an array of distances, returns number which are not <= threshold.""" return dist_array.shape[0] - point_match_count(dist_array, thresh) if __name__ == "__main__": labels_gt = Labels.load_json( "tests/data/json_format_v1/centered_pair.json") labels_pr = Labels.load_json( "tests/data/json_format_v2/centered_pair_predictions.json") # OPTION 1 # Match each ground truth instance node to the closest corresponding node # from any predicted instance in the same frame. nodewise_matching_func = match_instance_lists_nodewise # OPTION 2 # Match each ground truth instance to a distinct predicted instance: # We want to maximize the number of "matching" points between instances, # where "match" means the points are within some threshold distance.