Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions projects/Compute_IoU/Compute_IoU.py
Original file line number Diff line number Diff line change
@@ -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))
19 changes: 19 additions & 0 deletions projects/Compute_IoU/README.md
Original file line number Diff line number Diff line change
@@ -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)