def _load_pascal_annotation(self, index):
#Load image and bounding boxes info from XML file in the PASCAL VOC format
#翻譯:以PASCAL VOC格式從XML文件中加載圖像和邊框信息
filename = os.path.join(self._data_path, 'Annotations', index + '.xml')
tree = ET.parse(filename)
objs = tree.findall('object')
size = tree.find('size')
width = size.find('width').text
height = size.find('height').text
print(width, height)
num_objs = len(objs)
boxes = np.zeros((num_objs, 4), dtype=np.unit16)
gt_classes = np.zeros((num_objs), dtype=np.int32)
overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
# "Seg" area for pascal is just the box area 翻譯:pascal的“Seg”區域就是方框區域
seg_areas = np.zeros((num_objs), dtype=np.float32)
# Load object bounding boxes into a data frame 翻譯:將對象包圍框加載到數據幀中
for ix, obj in enumerate(objs):
clsname = obj.find('name').text.lower().strip()
bbox = obj.find('bndbox')
# Make pixel indexes 0-based 翻譯:使像素索引基於0
x1 = max(float(bbox.find('xmin').text - 1), 0)
y1 = max(float(bbox.find('ymin').text - 1), 0)
x2 = min(float(bbox.find('xmax').text), float(width))
y2 = min(float(bbox.find('ymax').text), float(height))
if x1 > x2: x1 = x2
if y1 > y2: y1 = y2
cls = self._classes_to_ind[clsname]
boxes[ix, :] = [x1, y1, x2, y2]
seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)
gt_classes[ix] = cls
overlaps[ix, cls] = 1.0
NMS
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import cv2
import numpy as np
"""
Non-max Suppression Algorithm
@param list Object candidate bounding boxes
@param list Confidence score of bounding boxes
@param float IoU threshold
@return Rest boxes after nms operation
"""
# 首先選定一個IOU閾值,例如爲0.4。然後將所有3個窗口(bounding box)
# 按照得分由高到低排序。然後選中得分最高的窗口,
# 遍歷計算剩餘的2個窗口與該窗口的重疊面積比例(IOU),
# 如果IOU大於閾值0.4,則將窗口刪除。然後,
# 再從剩餘的窗口中選中一個得分最高的,
# 重複上述過程。直至所有窗口都被處理。
def nms(bounding_boxes, confidence_score, threshold):
# If no bounding boxes, return empty list 如果沒有bbox,則返回空列表
if len(bounding_boxes) == 0:
return [], []
# Bounding boxes 將所有3個窗口(bounding box)按照得分由高到低排序
boxes = np.array(bounding_boxes)
# coordinates of bounding boxes
start_x = boxes[:, 0]
start_y = boxes[:, 1]
end_x = boxes[:, 2]
end_y = boxes[:, 3]
# Confidence scores of bounding boxes
score = np.array(confidence_score)
# Picked bounding boxes
picked_boxes = []
picked_score = []
# Compute areas of bounding boxes
areas = (end_x - start_x + 1) * (end_y - start_y + 1)
# Sort by confidence score of bounding boxes
order = np.argsort(score)
# Iterate bounding boxes
while order.size > 0:
# The index of largest confidence score
index = order[-1]
# Pick the bounding box with largest confidence score
picked_boxes.append(bounding_boxes[index])
picked_score.append(confidence_score[index])
# a=start_x[index]
# b=order[:-1]
# c=start_x[order[:-1]]
# Compute ordinates of intersection-over-union(IOU)
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]])
# Compute areas of intersection-over-union
w = np.maximum(0.0, x2 - x1 + 1)
h = np.maximum(0.0, y2 - y1 + 1)
intersection = w * h
print("------w------")
print(w)
print("------h------")
print(h)
# Compute the ratio between intersection and union
ratio = intersection / (areas[index] + areas[order[:-1]] - intersection)
print("----ratio-----")
print(ratio)
left = np.where(ratio < threshold)
print("-----left------")
print(left)
order = order[left]
return picked_boxes, picked_score
# Image name
image_name = 'nms.jpg'
# Bounding boxes
bounding_boxes = [(187, 82, 337, 317), (150, 67, 305, 282), (246, 121, 368, 304)]
confidence_score = [0.9, 0.75, 0.8]
# Read image
image = cv2.imread(image_name)
# Copy image as original
org = image.copy()
# Draw parameters
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2
# IoU threshold
threshold = 0.4
# Draw bounding boxes and confidence score
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)
# Run non-max suppression algorithm
picked_boxes, picked_score = nms(bounding_boxes, confidence_score, threshold)
# Draw bounding boxes and confidence score after non-maximum supression
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)
# Show image
cv2.imshow('Original', org)
cv2.imshow('NMS', image)
cv2.waitKey(0)
圖片:
結果: