Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

computation.py #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions model/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,26 @@

import numpy as np

def intersection(rect1, rect2):
"""
intersecton of units
compute boarder line top, left, right and bottom.
rect is defined as [ top_left_x, top_left_y, width, height ]
"""
top = max(rect1[1], rect2[1])
left = max(rect1[0], rect2[0])
right = min(rect1[0] + rect1[2], rect2[0] + rect2[2])
bottom = min(rect1[1] + rect1[3], rect2[1] + rect2[3])

if bottom > top and right > left:
return (bottom-top)*(right-left)

return 0


def jaccard(rect1, rect2):
"""
Jaccard index.
Jaccard index is defined as #(A∧B) / #(A∨B)
"""
rect1_ = [x if x >= 0 else 0 for x in rect1]
rect2_ = [x if x >= 0 else 0 for x in rect2]
s = rect1_[2]*rect1_[3] + rect2_[2]*rect2_[3]

# rect1 and rect2 => A∧B
intersect = intersection(rect1_, rect2_)

# rect1 or rect2 => A∨B
union = s - intersect

# A∧B / A∨B
return intersect / union

# determine the (x, y)-coordinates of the intersection rectangle
xA = max(rect1[0], rect2[0])
yA = max(rect1[1], rect2[1])
xB = min(rect1[2], rect2[2])
yB = min(rect1[3], rect2[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (rect1[2] - rect1[0] + 1) * (rect1[3] - rect1[1] + 1)
boxBArea = (rect2[2] - rect2[0] + 1) * (rect2[3] - rect2[1] + 1)
# return the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the intersection area
return interArea / float(boxAArea + boxBArea - interArea)

def corner2center(rect):
"""
Expand Down Expand Up @@ -88,4 +73,4 @@ def convert2wh(rect):
output format is...
[ top_left_x, top_left_y, width, height ]
"""
return np.array([rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1]])
return np.array([rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1]])