diff --git a/projects/Compute_IoU/Compute_IoU.py b/projects/Compute_IoU/Compute_IoU.py new file mode 100644 index 000000000..79cedc78b --- /dev/null +++ b/projects/Compute_IoU/Compute_IoU.py @@ -0,0 +1,33 @@ +import numpy as np + +def Cal_IoU(GT_bbox, Pred_bbox): + ''' + Args: + GT_bbox: the bounding box of the ground truth + Pred_bbox: the bounding box of the predicted + Returns: + IoU: Intersection over Union + ''' + #1. Calculate the area of the intersecting area + ixmin = max(GT_bbox[0], Pred_bbox[0]) + iymin = max(GT_bbox[1], Pred_bbox[1]) + ixmax = min(GT_bbox[2], Pred_bbox[2]) + iymax = min(GT_bbox[3], Pred_bbox[3]) + iw = np.maximum(ixmax - ixmin + 1., 0.) # the weight of the area + ih = np.maximum(iymax - iymin + 1., 0.) # the height of the area + area = iw * ih + + #2. Calculate the area of all area + #S = S1 + S2 - area + S1 = (Pred_bbox[2] - GT_bbox[0] + 1) * (Pred_bbox[3] - GT_bbox[1] + 1) + S2 = (GT_bbox[2] - GT_bbox[0] + 1) * (GT_bbox[3] - GT_bbox[1] + 1) + S = S1 + S2 - area + + #3. Calculate the IoU + iou = area / S + return iou + +if __name__ == "__main__": + pred_bbox = np.array([40, 40, 100, 100]) + gt_bbox = np.array([70, 80, 110, 130]) + print(Cal_IoU(pred_bbox, gt_bbox)) diff --git a/projects/Compute_IoU/README.md b/projects/Compute_IoU/README.md new file mode 100644 index 000000000..b10f3dbca --- /dev/null +++ b/projects/Compute_IoU/README.md @@ -0,0 +1,19 @@ + +# Image-Watermark + +## Description + +The project will help you calculate Intersection over Union (IoU). + +## About this Project + +Intersection over Union (IoU) is used when calculating mAP. It is a number from 0 to 1 that specifies the amount of overlap between the predicted and ground truth bounding box. + +## Usage + +Use the Script [Compute_IoU.py](https://github.com/Python-World/python-mini-projects/blob/master/projects/Compute_IoU/Compute_IoU.py) . In the command line, Enter + +`python3 Compute_IoU.py ` + +## Author +[Mason](https://github.com/JohnMasoner) \ No newline at end of file