import cv2
import numpy as np
def nms(bounding_boxes, confidence_score, threshold):
'''
:param bounding_boxes: 候选框列表,[左上角坐标, 右下角坐标], [min_x, min_y, max_x, max_y], 原点在图像左上角
:param confidence_score: 候选框置信度
:param threshold: IOU阈值
:return: 抑制后的bbox和置信度
'''
if len(bounding_boxes) == 0:
return [], []
boxes = np.array(bounding_boxes)
start_x = boxes[:, 0]
start_y = boxes[:, 1]
end_x = boxes[:, 2]
end_y = boxes[:, 3]
score = np.array(confidence_score)
picked_boxes = []
picked_score = []
areas = (end_x - start_x + 1) * (end_y - start_y + 1)
order = np.argsort(score)
while order.size > 0:
index = order[-1]
picked_boxes.append(bounding_boxes[index])
picked_score.append(confidence_score[index])
x1 = np.maximum(start_x[index], start_x[order[:-1]])
x2 = np.minimum(end_x[index], end_x[order[:-1]])
y1 = np.maximum(start_y[index], start_y[order[:-1]])
y2 = np.minimum(end_y[index], end_y[order[:-1]])
w = np.maximum(0.0, x2 - x1 + 1)
h = np.maximum(0.0, y2 - y1 + 1)
intersection = w * h
ratio = intersection / (areas[index] + areas[order[:-1]] - intersection)
left = np.where(ratio < threshold)
order = order[left]
return picked_boxes, picked_score
image_name = 'lena.jpg'
bounding_boxes = [(210, 180, 337, 380), (180, 120, 330, 340), (270, 160, 350, 360), (220, 210, 345, 410)]
confidence_score = [0.9, 0.75, 0.8, 0.85]
image = cv2.imread(image_name)
org = image.copy()
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2
threshold = 0.4
for (start_x, start_y, end_x, end_y), confidence in zip(bounding_boxes, confidence_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(org, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(org, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(org, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)
picked_boxes, picked_score = nms(bounding_boxes, confidence_score, threshold)
for (start_x, start_y, end_x, end_y), confidence in zip(picked_boxes, picked_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(image, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(image, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(image, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)
cv2.imshow('Original', org)
cv2.imshow('NMS', image)
cv2.waitKey(0)