diff --git a/model/computation.py b/model/computation.py index 717c7e6..85e7853 100644 --- a/model/computation.py +++ b/model/computation.py @@ -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): """ @@ -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]]) \ No newline at end of file + return np.array([rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1]])