# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
import numpy as np
def unique_boxes(boxes, scale=1.0):
"""Return indices of unique boxes."""
#将boxes的坐标与缩放比例scale的乘积四舍五入,得到的结果与矩阵V点乘,最后将得到的结果转为整数(即取整数部分)
v = np.array([1, 1e3, 1e6, 1e9])
hashes = np.round(boxes * scale).dot(v)
_, index = np.unique(hashes, return_index=True)
return np.sort(index)
def xywh_to_xyxy(boxes):
#将bbox的【x,y,w,h】转换成【x1,y1,x2,y2】
#某些数据集例如 pascal_voc 的标注方式是采用【x,y,w,h】
"""Convert [x y w h] box format to [x1 y1 x2 y2] format."""
return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1))
def xyxy_to_xywh(boxes):
#上面函数的逆函数
"""Convert [x1 y1 x2 y2] box format to [x y w h] format."""
return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1))
def validate_boxes(boxes, width=0, height=0):
"""Check that a set of boxes are valid."""
#检查一个bbox是否合法
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
#np.all()检查的是前面的是不是所有元素都满足条件
#例如x1>=0,如果某一个元素小于0则返回false,反之为true
assert (x1 >= 0).all()
assert (y1 >= 0).all()
assert (x2 >= x1).all()
assert (y2 >= y1).all()
assert (x2 < width).all()
assert (y2 < height).all()
def filter_small_boxes(boxes, min_size):
#过滤掉尺度过小的bbox
w = boxes[:, 2] - boxes[:, 0] #求宽
h = boxes[:, 3] - boxes[:, 1] #求高
keep = np.where((w >= min_size) & (h > min_size))[0] #保留高和宽都大于min_size的bbox
return keep