Skip to content

Commit

Permalink
Add pull request arabian9ts#20 from original repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ejuarezg committed Apr 25, 2021
1 parent 70ddcea commit 6ed5e06
Showing 1 changed file with 16 additions and 31 deletions.
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]])

0 comments on commit 6ed5e06

Please sign in to comment.