def match_objects_uniquely(candidates, targets, threshold=0.5):
g = nx.Graph()
for t in targets:
g.add_node(t)
for c in candidates:
g.add_node(c)
for t in targets:
tbb = BBox(*t.bbox)
for c in candidates:
cbb = BBox(*c.bbox)
intersection = tbb.intersection(cbb).area
union = tbb.area + cbb.area - intersection
try:
current_iou = intersection / float(union)
except ZeroDivisionError:
current_iou = float('inf')
pass
if current_iou >= threshold:
g.add_edge(t, c, weight=current_iou)
target_set = set(targets)
matching = nx.max_weight_matching(g, maxcardinality=True) # <- a dict with v->c and c->v both
hits = [(t, c) for (t, c) in matching.items() if t in target_set]
return hits
评论列表
文章目录