背景
- 需要知道照片中人的位置及相应的人脸的位置。使用人体检测器和人脸检测器。
- 使用人脸检测器,方便做后续的人脸识别和表情识别等
步骤:
- 计算所有人体检测框和人脸框的iou
- 计算iou_loss。
iou_loss = 1 - iou
- 通过匈牙利算法得到人脸框和人体框之间的匹配关系。匈牙利算法使得分配问题的和最小
代码实现
def iou_match_grid(bbox1, bbox2):
bbox1 = np.array(bbox1)
bbox2 = np.array(bbox2)
bbox1 = bbox1[:, :4]
bbox2 = bbox2[:, :4]
M = bbox1.shape[0]
N = bbox2.shape[0]
bbox1 = np.expand_dims(bbox1, axis=1)
bbox1 = np.repeat(bbox1, N, axis=1)
bbox2 = np.expand_dims(bbox2, axis=0)
bbox2 = np.repeat(bbox2, M, axis=0)
xmin = np.max([bbox1[:, :, 0], bbox2[:, :, 0]], axis=0)
ymin = np.max([bbox1[:, :, 1], bbox2[:, :, 1]], axis=0)
xmax = np.min([bbox1[:, :, 2], bbox2[:, :, 2]], axis=0)
ymax = np.min([bbox1[:, :, 3], bbox2[:, :, 3]], axis=0)
w = xmax - xmin
h = ymax - ymin
w = np.maximum(w, 0)
h = np.maximum(h, 0)
area1 = ((bbox1[:, :, 2] - bbox1[:, :, 0]) * (bbox1[:, :, 3] - bbox1[:, :, 1]))
area2 = ((bbox2[:, :, 2] - bbox2[:, :, 0]) * (bbox2[:, :, 3] - bbox2[:, :, 1]))
inter = w * h
iou = inter / (area1 + area2 - inter)
return iou
ious = iou_match_grid(human_bboxes, faces_bboxes)
ious_loss = 1 - ious
row_ind, col_ind = linear_sum_assignment(ious_loss)
row_ind是匹配上的人体检测框的索引
col_ind是相应的人脸框的索引