Skip to content

Instantly share code, notes, and snippets.

@shriyaRam
Created August 28, 2019 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shriyaRam/0d35e06b71ef1f5409350ded59284b39 to your computer and use it in GitHub Desktop.
Save shriyaRam/0d35e06b71ef1f5409350ded59284b39 to your computer and use it in GitHub Desktop.
def _overlaps(match1, match2):
bbox_tl1, bbox_br1, _, _ = match1
bbox_tl2, bbox_br2, _, _ = match2
return (bbox_br1[0] > bbox_tl2[0] and
bbox_br2[0] > bbox_tl1[0] and
bbox_br1[1] > bbox_tl2[1] and
bbox_br2[1] > bbox_tl1[1])
def _group_overlapping_rectangles(matches):
matches = list(matches)
num_groups = 0
match_to_group = {}
for idx1 in range(len(matches)):
for idx2 in range(idx1):
if _overlaps(matches[idx1], matches[idx2]):
match_to_group[idx1] = match_to_group[idx2]
break
else:
match_to_group[idx1] = num_groups
num_groups += 1
groups = collections.defaultdict(list)
for idx, group in match_to_group.items():
groups[group].append(matches[idx])
return groups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment