def rotate_nms_cc(dets, trackers): trackers_corners = box_np_ops.center_to_corner_box2d( trackers[:, :2], trackers[:, 2:4], trackers[:, 4]) trackers_standup = box_np_ops.corner_to_standup_nd(trackers_corners) dets_corners = box_np_ops.center_to_corner_box2d(dets[:, :2], dets[:, 2:4], dets[:, 4]) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, trackers_standup, eps=0.0) return standup_iou
def get_iou(dets_corners, track_corners, flag): if flag == 1: dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) track_standup = box_np_ops.corner_to_standup_nd(track_corners) else: dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) track_standup = dets_standup standup_iou = box_np_ops.iou_jit(dets_standup, track_standup, eps=0.0) standup_iou_new = standup_iou if flag != 1: for i in range(standup_iou.shape[0]): standup_iou[i][i] = 0 re_cal = np.where(standup_iou > 0) no_care = [] if re_cal[0].shape[0] == 0: return standup_iou, standup_iou_new for i in range(re_cal[0].shape[0]): if re_cal[0][i] == re_cal[1][i]: continue if re_cal[0][i] in no_care: continue # # #####***** 调用模块进行IOU计算 *****##### # IOU = intersection(dets_corners[re_cal[0][i]].tolist(), trackers_corners[re_cal[1][i]].tolist()) # #####***** 调用自定义函数进行IOU计算耗时稍微小一些 *****##### interpoly = [] b = cal_iou.PolygonClip(dets_corners[re_cal[0][i]].tolist(), track_corners[re_cal[1][i]].tolist(), interpoly) if b: list2 = [] for num_i in range(len(interpoly)): list2.append(interpoly[num_i]) for num_i in range(len(list2) - 1): for num_j in range(num_i + 1, len(list2)): if operator.eq(list2[num_i], list2[num_j]): interpoly.pop(num_j) area = cal_iou.CalArea(interpoly) area1 = cal_iou.CalArea(dets_corners[re_cal[0][i]].tolist()) area2 = cal_iou.CalArea(track_corners[re_cal[1][i]].tolist()) IOU = area / (area2 + area1 - area) IOU_new = area / area1 else: IOU = 0 IOU_new = 0 no_care.append(re_cal[1][i]) if flag: standup_iou[re_cal[0][i]][re_cal[1][i]] = IOU standup_iou_new[re_cal[0][i]][re_cal[1][i]] = IOU_new else: standup_iou[re_cal[0][i]][re_cal[1][i]] = IOU standup_iou[re_cal[1][i]][re_cal[0][i]] = IOU standup_iou_new[re_cal[0][i]][re_cal[1][i]] = IOU_new standup_iou_new[re_cal[1][i]][re_cal[0][i]] = IOU_new return standup_iou, standup_iou_new
def draw_box_in_bev(img, coors_range, boxes, color, thickness=1, labels=None, label_color=None): """ Args: boxes: center format. """ coors_range = np.array(coors_range) bev_corners = box_np_ops.center_to_corner_box2d(boxes[:, [0, 1]], boxes[:, [3, 4]], boxes[:, 6]) bev_corners -= coors_range[:2] bev_corners *= np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) standup = box_np_ops.corner_to_standup_nd(bev_corners) text_center = standup[:, 2:] text_center[:, 1] -= (standup[:, 3] - standup[:, 1]) / 2 bev_lines = np.concatenate( [bev_corners[:, [0, 2, 3]], bev_corners[:, [1, 3, 0]]], axis=2) bev_lines = bev_lines.reshape(-1, 4) colors = np.tile(np.array(color).reshape(1, 3), [bev_lines.shape[0], 1]) colors = colors.astype(np.int32) img = cv2_draw_lines(img, bev_lines, colors, thickness) if boxes.shape[1] == 9: # draw velocity arrows for box in boxes: velo = box[-2:] # velo = np.array([-np.sin(box[6]), -np.cos(box[6])]) velo_unified = velo if np.isnan(velo_unified[0]): continue velo_unified = velo_unified * np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) center = box[:2] - coors_range[:2] center = center * np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) center = tuple(map(lambda x: int(x), center)) center2 = tuple(map(lambda x: int(x), center + velo_unified)) cv2.arrowedLine(img, center, center2, color, thickness, tipLength=0.3) if labels is not None: if label_color is None: label_color = colors else: label_color = np.tile( np.array(label_color).reshape(1, 3), [bev_lines.shape[0], 1]) label_color = label_color.astype(np.int32) img = cv2_draw_text(img, text_center, labels, label_color, thickness) return img
def rotate_nms_cc(dets, thresh): scores = dets[:, 5] order = scores.argsort()[::-1].astype(np.int32) # highest->lowest dets_corners = box_np_ops.center_to_corner_box2d(dets[:, :2], dets[:, 2:4], dets[:, 4]) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, dets_standup, eps=0.0) # print(dets_corners.shape, order.shape, standup_iou.shape) return rotate_non_max_suppression_cpu(dets_corners, order, standup_iou, thresh)
def rotate_nms_cc(dets, trackers, usespeed=0): '''数据形式:x,y,l,w,vx,vy,theta''' trackers_corners = box_np_ops.center_to_corner_box2d( trackers[:, :2], trackers[:, 2:4], trackers[:, 6]) trackers_standup = box_np_ops.corner_to_standup_nd(trackers_corners) dets_corners = box_np_ops.center_to_corner_box2d(dets[:, :2], dets[:, 2:4], dets[:, 6]) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, trackers_standup, eps=0.0) #位置iou # print('standup_iou',standup_iou) # 速度的iou if usespeed == 1: trackers_boxlist = IOU_Calculation.point_to_box( trackers[:, 4:6], 0.4, 0.2) #vx,vy,vx_width,vy_width dets_boxlist = IOU_Calculation.point_to_box(dets[:, 4:6], 0.4, 0.2) speed_iou = IOU_Calculation.iou_matrix(dets_boxlist, trackers_boxlist) # print('speed_iou', speed_iou) IOU_matrix = standup_iou * 0.8 + speed_iou * 0.2 #分配权重 return IOU_matrix else: return standup_iou
def draw_box_in_bev(img, coors_range, boxes, color, thickness=1, labels=None, label_color=None): """ Args: boxes: center format. """ coors_range = np.array(coors_range) bev_corners = box_np_ops.center_to_corner_box2d(boxes[:, [0, 1]], boxes[:, [3, 4]], boxes[:, 6]) bev_corners -= coors_range[:2] bev_corners *= np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) standup = box_np_ops.corner_to_standup_nd(bev_corners) text_center = standup[:, 2:] text_center[:, 1] -= (standup[:, 3] - standup[:, 1]) / 2 bev_lines = np.concatenate( [bev_corners[:, [0, 2, 3]], bev_corners[:, [1, 3, 0]]], axis=2) bev_lines = bev_lines.reshape(-1, 4) colors = np.tile(np.array(color).reshape(1, 3), [bev_lines.shape[0], 1]) colors = colors.astype(np.int32) img = cv2_draw_lines(img, bev_lines, colors, thickness) if labels is not None: if label_color is None: label_color = colors else: label_color = np.tile( np.array(label_color).reshape(1, 3), [bev_lines.shape[0], 1]) label_color = label_color.astype(np.int32) img = cv2_draw_text(img, text_center, labels, label_color, thickness * 2) return img
def rotate_nms_merge(dets, thresh): ''' input dets_corners, order, standup_iou,thresh return bbox_list ( not inds, cause we merges the bboxes, new boxes is not in the original boxlist) we can use score or centerness as weight to get better merge method parameter for merge nms iou_thresh : > thresh hold , then we add those bboxes to same list, and merge them intersect_bbox_nums_thresh : numbers of intersection bboxes > threshold , including itself, if the number is too small, we can ignore the bboxes TODO: score weight : with or without centerness.sigmoid ''' scores = dets[:, 5] order = scores.argsort()[::-1].astype(np.int32) # highest->lowest dets_corners = box_np_ops.center_to_corner_box2d(dets[:, :2], dets[:, 2:4], dets[:, 4]) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, dets_standup, eps=0.0) unmerged_boxlist = [] while order.size > 0: i = order[0] merged_boxes_ind = np.where(standup_iou[i] > thresh) unmerged_boxlist.append(merged_boxes_ind[0]) # filter out the merge inds in the order for ele in merged_boxes_ind[0]: if ele in order: remove_inds = np.where(order == ele)[0] order = np.delete(order, remove_inds[0]) return unmerged_boxlist #depreciate : moving anchor to absord the bbox merged_boxes_num = 0 rounds = 0 thresh = 0.1 intersect_bbox_nums_thresh = 1 start = 0 while order.size > 0: print('we have {} bboxes in round {}'.format(order.size, rounds)) i = order[0] merged_boxes_ind = np.where(standup_iou[i] >= thresh) # we can set a thresh hold for intersetction number, if intersetion bbox num too small, we give up the bbox if merged_boxes_ind[0].size > intersect_bbox_nums_thresh: print('round {} have intersection{} nums'.format( rounds, merged_boxes_ind[0].size)) if not merged_boxes_num in unmerged_boxlist: unmerged_boxlist[merged_boxes_num] = merged_boxes_ind unmerged_boxes_inds = np.where(standup_iou[i] < thresh) else: unmerged_boxlist[merged_boxes_num] = np.concatenate( (unmerged_boxlist[merged_boxes_num], merged_boxes_ind[1:]), axis=0) unmerged_boxes_inds = np.where(standup_iou[i] < thresh) else: order = order[unmerged_boxes_inds + 1] #rounds+=1 final_bboxes_list = [] total_box_check = 0 for key, bboxes in unmerged_boxlist.items(): final_bboxes_list.append(bboxes.mean(axis=0)) total_box_check += unmerged_boxlist[key].shape[0] embed() return final_bboxes_list
def draw_box_in_bev(img, coors_range, boxes, color, thickness=1, labels=None, label_color=None): """ Args: boxes: center format. """ # img: (600, 1000, 3) # coors_range: array([-50, -30, -3, 50, 30, 1]) # boxes: N, 7: x,y,z,w,l,h,theta #import pdb; pdb.set_trace() coors_range = np.array(coors_range) bev_corners = box_np_ops.center_to_corner_box2d( boxes[:, [0, 1]], boxes[:, [3, 4]], boxes[:, 6]) # [x, y], [w, l], [theta] # bev_corners: N, 4, 2 bev_corners -= coors_range[:2] bev_corners *= np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) #[1000, 600] / [100, 60] => multiply by 10 to all coords. standup = box_np_ops.corner_to_standup_nd(bev_corners) # [30, 4] text_center = standup[:, 2:] text_center[:, 1] -= (standup[:, 3] - standup[:, 1]) / 2 bev_lines = np.concatenate( [bev_corners[:, [0, 1, 2, 3]], bev_corners[:, [1, 2, 3, 0]]], axis=2) # bev_lines.shape = N, 3, 4 bev_lines = bev_lines.reshape(-1, 4) colors = np.tile(np.array(color).reshape(1, 3), [bev_lines.shape[0], 1]) colors = colors.astype(np.int32) img = cv2_draw_lines(img, bev_lines, colors, thickness) if boxes.shape[1] == 9: # draw velocity arrows for box in boxes: velo = box[-2:] # velo = np.array([-np.sin(box[6]), -np.cos(box[6])]) velo_unified = velo if np.isnan(velo_unified[0]): continue velo_unified = velo_unified * np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) center = box[:2] - coors_range[:2] center = center * np.array( img.shape[:2])[::-1] / (coors_range[3:5] - coors_range[:2]) center = tuple(map(lambda x: int(x), center)) center2 = tuple(map(lambda x: int(x), center + velo_unified)) cv2.arrowedLine(img, center, center2, color, thickness, tipLength=0.3) if labels is not None: if label_color is None: label_color = colors else: label_color = np.tile( np.array(label_color).reshape(1, 3), [bev_lines.shape[0], 1]) label_color = label_color.astype(np.int32) img = cv2_draw_text(img, text_center, labels, label_color, thickness) return img
def fusion_box(dets_corners, track_corners, flag): if flag == 1: dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) track_standup = box_np_ops.corner_to_standup_nd(track_corners) else: dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) track_standup = dets_standup standup_iou = box_np_ops.iou_jit(dets_standup, track_standup, eps=0.0) if flag != 1: for i in range(standup_iou.shape[0]): standup_iou[i][i] = 0 re_cal = np.where(standup_iou > 0) no_care = [] if re_cal[0].shape[0] == 0: return np.empty((0, 2), dtype=int), np.arange(len(dets_corners)), np.empty( (0, 5), dtype=int), 0 for i in range(re_cal[0].shape[0]): if re_cal[0][i] == re_cal[1][i]: continue if re_cal[0][i] in no_care: continue # # #####***** 调用模块进行IOU计算 *****##### # IOU = intersection(dets_corners[re_cal[0][i]].tolist(), trackers_corners[re_cal[1][i]].tolist()) # #####***** 调用自定义函数进行IOU计算耗时稍微小一些 *****##### interpoly = [] b = cal_iou.PolygonClip(dets_corners[re_cal[0][i]].tolist(), track_corners[re_cal[1][i]].tolist(), interpoly) if b: list2 = [] for num_i in range(len(interpoly)): list2.append(interpoly[num_i]) for num_i in range(len(list2) - 1): for num_j in range(num_i + 1, len(list2)): if operator.eq(list2[num_i], list2[num_j]): interpoly.pop(num_j) area = cal_iou.CalArea(interpoly) area1 = cal_iou.CalArea(dets_corners[re_cal[0][i]].tolist()) area2 = cal_iou.CalArea(track_corners[re_cal[1][i]].tolist()) IOU = area / (area2 + area1 - area) else: IOU = 0 no_care.append(re_cal[1][i]) if flag: standup_iou[re_cal[0][i]][re_cal[1][i]] = IOU else: standup_iou[re_cal[0][i]][re_cal[1][i]] = IOU standup_iou[re_cal[1][i]][re_cal[0][i]] = IOU iou_threshold = 0.000001 matched_indices = linear_assignment(-standup_iou) unmatched_detections = [] for d, det in enumerate(dets_corners): if (d not in matched_indices[:, 0]): unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(track_corners): if (t not in matched_indices[:, 1]): unmatched_trackers.append(t) matches = [] for m in matched_indices: if standup_iou[m[0], m[1]] < iou_threshold: unmatched_detections.append(m[0]) unmatched_trackers.append(m[1]) else: matches.append(m.reshape(1, 2)) if (len(matches) == 0): matches = np.empty((0, 2), dtype=int) else: matches = np.concatenate(matches, axis=0) return matches, np.array(unmatched_detections), np.array( unmatched_trackers), standup_iou