merge_overlapping_detections
- ketos.neural_networks.dev_utils.detection.merge_overlapping_detections(detections_df)[source]
Merge overlapping or adjacent detections with the same label.
The score of the merged detection is computed as the average of the individual detection scores.
Note: The detections are assumed to be sorted by start time in chronological order.
- Args:
- detections_df: pandas DataFrame
Dataframe with detections. It should have the following columns: - ‘filename’: The name of the file containing the detection. - ‘start’: The start time of the detection in seconds. - ‘end’: The end time of the detection in seconds. - ‘label’: The label associated with the detection. - ‘score’: The score associated with the detection.
- Returns:
- merged: pandas DataFrame
DataFrame with the merged detections.
- Example:
Given a DataFrame with the following format:
filename
start
end
label
score
file1
0
5
0
1
file1
3
7
0
2
file2
0
5
1
3
The function would return:
filename
start
end
label
score
file1
0
7
0
1.5
file2
0
5
1
3
>>> import pandas as pd >>> detections_df = pd.DataFrame([ ... {'filename': 'file1', 'start': 0, 'end': 5, 'label': 0, 'score': 1}, ... {'filename': 'file1', 'start': 3, 'end': 7, 'label': 0, 'score': 2}, ... {'filename': 'file2', 'start': 0, 'end': 5, 'label': 1, 'score': 3} ... ]) >>> merged = merge_overlapping_detections(detections_df) >>> merged.to_dict('records') [{'filename': 'file1', 'start': 0, 'end': 7, 'label': 0, 'score': 1.5}, {'filename': 'file2', 'start': 0, 'end': 5, 'label': 1, 'score': 3.0}]