feat: Add minimum affinity filter to affinity result grouping

- Introduced a `min_affinity` parameter to the `affinity_result_by_tracking` function, allowing users to specify a threshold for filtering affinity results.
- Updated the logic to skip results with affinities below the specified minimum, enhancing the relevance of grouped detections.
- Improved function documentation to include details about the new parameter and its purpose.
This commit is contained in:
2025-05-03 17:29:50 +08:00
parent 1f8d70803f
commit 4bc3fce0b1

View File

@ -1197,13 +1197,22 @@ display(affinities)
# %% # %%
def affinity_result_by_tracking( def affinity_result_by_tracking(
results: Iterable[AffinityResult], results: Iterable[AffinityResult],
min_affinity: float = 0.0,
) -> dict[TrackingID, list[Detection]]: ) -> dict[TrackingID, list[Detection]]:
""" """
Group affinity results by target ID. Group affinity results by target ID.
Args:
results: the affinity results to group
min_affinity: the minimum affinity to consider
Returns:
a dictionary mapping tracking IDs to a list of detections
""" """
res: dict[TrackingID, list[Detection]] = defaultdict(list) res: dict[TrackingID, list[Detection]] = defaultdict(list)
for affinity_result in results: for affinity_result in results:
for _affinity, t, d in affinity_result.tracking_association(): for affinity, t, d in affinity_result.tracking_association():
if affinity < min_affinity:
continue
res[t.id].append(d) res[t.id].append(d)
return res return res