def py_cpu_nms_poly(dets, thresh):
    scores = dets[:, 8]
    polys = []
    areas = []
    for i in range(len(dets)):
        tm_polygon = polyiou.VectorDouble([
            dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4],
            dets[i][5], dets[i][6], dets[i][7]
        ])
        polys.append(tm_polygon)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        ovr = []
        i = order[0]
        keep.append(i)
        for j in range(order.size - 1):
            iou = polyiou.iou_poly(polys[i], polys[order[j + 1]])
            ovr.append(iou)
        ovr = np.array(ovr)

        # print('ovr: ', ovr)
        # print('thresh: ', thresh)
        try:
            if math.isnan(ovr[0]):
                pdb.set_trace()
        except:
            pass
        inds = np.where(ovr <= thresh)[0]
        # print('inds: ', inds)

        order = order[inds + 1]

    return keep
Beispiel #2
0
def rbbox_overlaps_cy(boxes_np, query_boxes_np):
    # TODO: first calculate the hbb overlaps, for overlaps > 0, calculate the obb overlaps

    polys_np = RotBox2Polys(boxes_np).astype(np.float)
    query_polys_np = RotBox2Polys(query_boxes_np).astype(np.float)

    h_bboxes_np = poly2bbox(polys_np).astype(np.float)
    h_query_bboxes_np = poly2bbox(query_polys_np).astype(np.float)

    # hious
    ious = bbox_overlaps_cython(h_bboxes_np, h_query_bboxes_np)
    import pdb
    # pdb.set_trace()
    inds = np.where(ious > 0)
    for index in range(len(inds[0])):
        box_index = inds[0][index]
        query_box_index = inds[1][index]

        box = polys_np[box_index]
        query_box = query_polys_np[query_box_index]

        # calculate obb iou
        # import pdb
        # pdb.set_trace()
        overlap = polyiou.iou_poly(polyiou.VectorDouble(box), polyiou.VectorDouble(query_box))
        ious[box_index][query_box_index] = overlap

    return ious
Beispiel #3
0
            def calcoverlaps(BBGT_keep, bb):
                overlaps = []
                for index, GT in enumerate(BBGT_keep):

                    overlap = polyiou.iou_poly(polyiou.VectorDouble(BBGT_keep[index]), polyiou.VectorDouble(bb))
                    overlaps.append(overlap)
                return overlaps
Beispiel #4
0
def py_cpu_nms_poly_fast3_np(dets, thresh):
    obbs = dets[:, 0:-1]
    x1 = np.min(obbs[:, 0::2], axis=1)
    y1 = np.min(obbs[:, 1::2], axis=1)
    x2 = np.max(obbs[:, 0::2], axis=1)
    y2 = np.max(obbs[:, 1::2], axis=1)
    scores = dets[:, 8]
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)

    polys = []
    for i in range(len(dets)):
        tm_polygon = polyiou.VectorDouble([
            dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4],
            dets[i][5], dets[i][6], dets[i][7]
        ])
        polys.append(tm_polygon)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        ovr = []
        i = order[0]
        keep.append(i)
        # if order.size == 0:
        #     break
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])
        # w = np.maximum(0.0, xx2 - xx1 + 1)
        # h = np.maximum(0.0, yy2 - yy1 + 1)
        w = np.maximum(0.0, xx2 - xx1)
        h = np.maximum(0.0, yy2 - yy1)
        hbb_inter = w * h
        hbb_ovr = hbb_inter / (areas[i] + areas[order[1:]] - hbb_inter)
        # h_keep_inds = np.where(hbb_ovr == 0)[0]
        h_inds = np.where(hbb_ovr > 0)[0]
        tmp_order = order[h_inds + 1]
        for j in range(tmp_order.size):
            iou = polyiou.iou_poly(polys[i], polys[tmp_order[j]])
            hbb_ovr[h_inds[j]] = iou
            # ovr.append(iou)
            # ovr_index.append(tmp_order[j])

        try:
            if math.isnan(ovr[0]):
                pdb.set_trace()
        except:
            pass
        inds = np.where(hbb_ovr <= thresh)[0]
        order = order[inds + 1]
    return keep
Beispiel #5
0
def rbbox_overlaps_cy_warp_fast(rbboxes, query_boxes):
    # TODO: first calculate the hbb overlaps, for overlaps > 0, calculate the obb overlaps
    # import pdb
    # pdb.set_trace()
    box_device = query_boxes.device
    query_boxes_np = query_boxes.detach().cpu().numpy().astype(np.float)

    # polys_np = RotBox2Polys(boxes_np)
    # TODO: change it to only use pos gt_masks
    # polys_np = mask2poly(gt_masks)
    # polys_np = np.array(Tuplelist2Polylist(polys_np)).astype(np.float)

    polys_np = RotBox2Polys(rbboxes).astype(np.float)
    query_polys_np = RotBox2Polys(query_boxes_np)

    overlaps = []
    for box, qbox in zip(polys_np, query_polys_np):
        overlaps.append(
            polyiou.iou_poly(polyiou.VectorDouble(box),
                             polyiou.VectorDouble(qbox)))

    return torch.from_numpy(np.array(overlaps)).to(box_device)
Beispiel #6
0
def rbbox_overlaps_cy_warp(rbboxes, query_boxes):
    # TODO: first calculate the hbb overlaps, for overlaps > 0, calculate the obb overlaps
    # import pdb
    # pdb.set_trace()
    box_device = query_boxes.device
    query_boxes_np = query_boxes.cpu().numpy().astype(np.float)

    # polys_np = RotBox2Polys(boxes_np)
    # TODO: change it to only use pos gt_masks
    # polys_np = mask2poly(gt_masks)
    # polys_np = np.array(Tuplelist2Polylist(polys_np)).astype(np.float)

    polys_np = RotBox2Polys(rbboxes).astype(np.float)
    query_polys_np = RotBox2Polys(query_boxes_np)

    h_bboxes_np = poly2bbox(polys_np)
    h_query_bboxes_np = poly2bbox(query_polys_np)

    # hious
    ious = bbox_overlaps_cython(h_bboxes_np, h_query_bboxes_np)
    import pdb
    # pdb.set_trace()
    inds = np.where(ious > 0)
    for index in range(len(inds[0])):
        box_index = inds[0][index]
        query_box_index = inds[1][index]

        box = polys_np[box_index]
        query_box = query_polys_np[query_box_index]

        # calculate obb iou
        # import pdb
        # pdb.set_trace()
        overlap = polyiou.iou_poly(polyiou.VectorDouble(box),
                                   polyiou.VectorDouble(query_box))
        ious[box_index][query_box_index] = overlap

    return torch.from_numpy(ious).to(box_device)
Beispiel #7
0
def py_cpu_nms_poly(dets, thresh):
    scores = dets[:, 8]
    polys = []
    areas = []
    for i in range(len(dets)):
        tm_polygon = polyiou.VectorDouble([
            dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4],
            dets[i][5], dets[i][6], dets[i][7]
        ])
        polys.append(tm_polygon)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        ovr = []
        i = order[0]
        keep.append(i)
        for j in range(order.size - 1):
            iou = polyiou.iou_poly(polys[i], polys[order[j + 1]])
            ovr.append(iou)
        ovr = np.array(ovr)
        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]
    return keep
Beispiel #8
0
def py_cpu_nms_poly_fast_np(dets, iou_threshold):
    try:
        obbs = dets[:, 0:-1]
    except:
        print('fail index')
        pdb.set_trace()
    x1 = np.min(obbs[:, 0::2], axis=1)
    y1 = np.min(obbs[:, 1::2], axis=1)
    x2 = np.max(obbs[:, 0::2], axis=1)
    y2 = np.max(obbs[:, 1::2], axis=1)
    scores = dets[:, 8]
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)

    polys = []
    for i in range(len(dets)):
        tm_polygon = polyiou.VectorDouble([
            dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4],
            dets[i][5], dets[i][6], dets[i][7]
        ])
        polys.append(tm_polygon)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        ovr = []
        i = order[0]
        keep.append(i)
        # if order.size == 0:
        #     break
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])
        # w = np.maximum(0.0, xx2 - xx1 + 1)
        # h = np.maximum(0.0, yy2 - yy1 + 1)
        w = np.maximum(0.0, xx2 - xx1)
        h = np.maximum(0.0, yy2 - yy1)
        hbb_inter = w * h
        hbb_ovr = hbb_inter / (areas[i] + areas[order[1:]] - hbb_inter)
        # h_keep_inds = np.where(hbb_ovr == 0)[0]
        h_inds = np.where(hbb_ovr > 0)[0]
        tmp_order = order[h_inds + 1]
        for j in range(tmp_order.size):
            iou = polyiou.iou_poly(polys[i], polys[tmp_order[j]])
            hbb_ovr[h_inds[j]] = iou
            # ovr.append(iou)
            # ovr_index.append(tmp_order[j])

        # ovr = np.array(ovr)
        # ovr_index = np.array(ovr_index)
        # print('ovr: ', ovr)
        # print('thresh: ', thresh)
        try:
            if math.isnan(ovr[0]):
                pdb.set_trace()
        except:
            pass
        inds = np.where(hbb_ovr <= thresh)[0]

        # order_obb = ovr_index[inds]
        # print('inds: ', inds)
        # order_hbb = order[h_keep_inds + 1]
        order = order[inds + 1]
        # pdb.set_trace()
        # order = np.concatenate((order_obb, order_hbb), axis=0).astype(np.int)
    return keep
Beispiel #9
0
def py_cpu_nms_poly_fast(dets, iou_threshold):
    # TODO: check the type numpy()
    if dets.shape[0] == 0:
        keep = dets.new_zeros(0, dtype=torch.long)
        keep = keep.cpu().numpy()
        device = dets.device
        if isinstance(dets, torch.Tensor):
            dets = dets.cpu().numpy().astype(np.float64)
        if isinstance(iou_threshold, torch.Tensor):
            iou_threshold = iou_threshold.cpu().numpy().astype(np.float64)
    else:
        device = dets.device
        if isinstance(dets, torch.Tensor):
            dets = dets.cpu().numpy().astype(np.float64)
        if isinstance(iou_threshold, torch.Tensor):
            iou_threshold = iou_threshold.cpu().numpy().astype(np.float64)
        obbs = dets[:, 0:-1]
        # pdb.set_trace()
        x1 = np.min(obbs[:, 0::2], axis=1)
        y1 = np.min(obbs[:, 1::2], axis=1)
        x2 = np.max(obbs[:, 0::2], axis=1)
        y2 = np.max(obbs[:, 1::2], axis=1)
        scores = dets[:, 8]
        areas = (x2 - x1 + 1) * (y2 - y1 + 1)

        polys = []
        for i in range(len(dets)):
            tm_polygon = polyiou.VectorDouble([
                dets[i][0], dets[i][1], dets[i][2], dets[i][3], dets[i][4],
                dets[i][5], dets[i][6], dets[i][7]
            ])
            polys.append(tm_polygon)
        order = scores.argsort()[::-1]

        keep = []
        while order.size > 0:
            ovr = []
            i = order[0]
            keep.append(i)
            # if order.size == 0:
            #     break
            xx1 = np.maximum(x1[i], x1[order[1:]])
            yy1 = np.maximum(y1[i], y1[order[1:]])
            xx2 = np.minimum(x2[i], x2[order[1:]])
            yy2 = np.minimum(y2[i], y2[order[1:]])
            # w = np.maximum(0.0, xx2 - xx1 + 1)
            # h = np.maximum(0.0, yy2 - yy1 + 1)
            w = np.maximum(0.0, xx2 - xx1)
            h = np.maximum(0.0, yy2 - yy1)
            hbb_inter = w * h
            hbb_ovr = hbb_inter / (areas[i] + areas[order[1:]] - hbb_inter)
            # h_keep_inds = np.where(hbb_ovr == 0)[0]
            h_inds = np.where(hbb_ovr > 0)[0]
            tmp_order = order[h_inds + 1]
            for j in range(tmp_order.size):
                iou = polyiou.iou_poly(polys[i], polys[tmp_order[j]])
                hbb_ovr[h_inds[j]] = iou
                # ovr.append(iou)
                # ovr_index.append(tmp_order[j])

            # ovr = np.array(ovr)
            # ovr_index = np.array(ovr_index)
            # print('ovr: ', ovr)
            # print('thresh: ', thresh)
            try:
                if math.isnan(ovr[0]):
                    pdb.set_trace()
            except:
                pass
            inds = np.where(hbb_ovr <= iou_threshold)[0]

            # order_obb = ovr_index[inds]
            # print('inds: ', inds)
            # order_hbb = order[h_keep_inds + 1]
            order = order[inds + 1]
            # pdb.set_trace()
            # order = np.concatenate((order_obb, order_hbb), axis=0).astype(np.int)

    return torch.from_numpy(dets[keep, :]).to(device), torch.from_numpy(
        np.array(keep)).to(device)
Beispiel #10
0
def soft_py_cpu_nms_poly(dets, iou_thr, method=1, sigma=0.5, min_score=0.001):
    # TODO: 1. check the correctness
    # TODO: 2. optimize it
    if dets.shape[0] == 0:
        keep = dets.new_zeros(0, dtype=torch.long)
        keep = keep.cpu().numpy()
        device = dets.device
        if isinstance(dets, torch.Tensor):
            dets.dets.cpu().numpy().astype(np.float64)
        if isinstance(iou_thr, torch.Tensor):
            iou_thr = iou_thr.cpu().numpy().astype(np.float64)
    else:
        device = dets.device
        if isinstance(dets, torch.Tensor):
            dets = dets.cpu().numpy().astype(np.float64)
        if isinstance(iou_thr, torch.Tensor):
            iou_thr = iou_thr.cpu().numpy().astype(np.float64)
        N = dets.shape[0]
        inds = np.arange(N)

        for i in range(N):
            maxscore = dets[i, 8]
            maxpos = i

            tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4 = \
                dets[i, 0], dets[i, 1], dets[i, 2], dets[i,
                                                         3], dets[i, 4], dets[i, 5], dets[i, 6], dets[i, 7]
            ts = dets[i, 8]
            ti = inds[i]

            pos = i + 1
            # get max box
            while pos < N:
                if maxscore < dets[pos, 8]:
                    maxscore = dets[pos, 8]
                    maxpos = pos
                pos = pos + 1

            # add max poly as a detection
            dets[i, 0] = dets[maxpos, 0]
            dets[i, 1] = dets[maxpos, 1]
            dets[i, 2] = dets[maxpos, 2]
            dets[i, 3] = dets[maxpos, 3]
            dets[i, 4] = dets[maxpos, 4]
            dets[i, 5] = dets[maxpos, 5]
            dets[i, 6] = dets[maxpos, 6]
            dets[i, 7] = dets[maxpos, 7]
            dets[i, 8] = dets[maxpos, 8]
            inds[i] = inds[maxpos]

            # swap ith poly with position of max poly
            dets[maxpos, 0] = tx1
            dets[maxpos, 1] = ty1
            dets[maxpos, 2] = tx2
            dets[maxpos, 3] = ty2
            dets[maxpos, 4] = tx3
            dets[maxpos, 5] = ty3
            dets[maxpos, 6] = tx4
            dets[maxpos, 7] = ty4
            dets[maxpos, 8] = ts
            inds[maxpos] = ti

            tx1 = dets[i, 0]
            ty1 = dets[i, 1]
            tx2 = dets[i, 2]
            ty2 = dets[i, 3]
            tx3 = dets[i, 4]
            ty3 = dets[i, 5]
            tx4 = dets[i, 6]
            ty4 = dets[i, 7]
            ts = dets[i, 8]

            pos = i + 1
            # NMS iterations, note that N changes if detection polys fall below threshold
            while pos < N:
                x1, y1, x2, y2, x3, y3, x4, y4 = \
                    dets[pos, 0], dets[pos, 1], dets[pos, 2], dets[pos, 3], \
                    dets[pos, 4], dets[pos, 5], dets[pos, 6], dets[pos, 7]
                s = dets[pos, -1]

                # TODO:finish the area calculation
                # area = ()
                # finish the iou calculation
                max_polygon = polyiou.VectorDouble(
                    [tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4])
                pos_polygon = polyiou.VectorDouble(
                    [x1, y1, x2, y2, x3, y3, x4, y4])
                ov = polyiou.iou_poly(max_polygon, pos_polygon)

                if method == 1:  # linear
                    if ov > iou_thr:
                        weight = 1 - ov
                    else:
                        weight = 1
                elif method == 2:  # gaussian
                    weight = np.exp(-(ov * ov) / sigma)
                else:  # original NMS
                    if ov > iou_thr:
                        weight = 0
                    else:
                        weight = 1

                dets[pos, 8] = weight * dets[pos, 8]

                # if det score falls below threshold, discard the poly by
                # swapping with last poly update N
                if dets[pos, 8] < min_score:
                    dets[pos, 0] = dets[N - 1, 0]
                    dets[pos, 1] = dets[N - 1, 1]
                    dets[pos, 2] = dets[N - 1, 2]
                    dets[pos, 3] = dets[N - 1, 3]
                    dets[pos, 4] = dets[N - 1, 4]
                    dets[pos, 5] = dets[N - 1, 5]
                    dets[pos, 6] = dets[N - 1, 6]
                    dets[pos, 7] = dets[N - 1, 7]
                    dets[pos, 8] = dets[N - 1, 8]
                    inds[pos] = inds[N - 1]
                    N = N - 1
                    pos = pos - 1
                pos = pos + 1

    return torch.from_numpy(dets[:N]).to(device), torch.from_numpy(
        inds[:N]).to(device)
Beispiel #11
0
def soft_py_cpu_nms_poly_np(dets_in,
                            iou_thr,
                            method=1,
                            sigma=0.5,
                            min_score=0.05):

    dets = dets_in.copy()
    N = dets.shape[0]
    inds = np.arange(N)

    for i in range(N):
        maxscore = dets[i, 8]
        maxpos = i

        tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4 = \
            dets[i, 0], dets[i, 1], dets[i, 2], dets[i,
                                                     3], dets[i, 4], dets[i, 5], dets[i, 6], dets[i, 7]
        ts = dets[i, 8]
        ti = inds[i]

        pos = i + 1
        # get max box
        while pos < N:
            if maxscore < dets[pos, 8]:
                maxscore = dets[pos, 8]
                maxpos = pos
            pos = pos + 1

        # add max poly as a detection
        dets[i, 0] = dets[maxpos, 0]
        dets[i, 1] = dets[maxpos, 1]
        dets[i, 2] = dets[maxpos, 2]
        dets[i, 3] = dets[maxpos, 3]
        dets[i, 4] = dets[maxpos, 4]
        dets[i, 5] = dets[maxpos, 5]
        dets[i, 6] = dets[maxpos, 6]
        dets[i, 7] = dets[maxpos, 7]
        dets[i, 8] = dets[maxpos, 8]
        inds[i] = inds[maxpos]

        # swap ith poly with position of max poly
        dets[maxpos, 0] = tx1
        dets[maxpos, 1] = ty1
        dets[maxpos, 2] = tx2
        dets[maxpos, 3] = ty2
        dets[maxpos, 4] = tx3
        dets[maxpos, 5] = ty3
        dets[maxpos, 6] = tx4
        dets[maxpos, 7] = ty4
        dets[maxpos, 8] = ts
        inds[maxpos] = ti

        tx1 = dets[i, 0]
        ty1 = dets[i, 1]
        tx2 = dets[i, 2]
        ty2 = dets[i, 3]
        tx3 = dets[i, 4]
        ty3 = dets[i, 5]
        tx4 = dets[i, 6]
        ty4 = dets[i, 7]
        ts = dets[i, 8]

        # hbb
        txmin, tymin, txmax, tymax = min(tx1, tx2, tx3, tx4), \
            min(ty1, ty2, ty3, ty4), \
            max(tx1, tx2, tx3, tx4), \
            max(ty1, ty2, ty3, ty4)

        pos = i + 1
        # NMS iterations, note that N changes if detection polys fall below threshold
        while pos < N:
            x1, y1, x2, y2, x3, y3, x4, y4 = \
                dets[pos, 0], dets[pos, 1], dets[pos, 2], dets[pos, 3], \
                dets[pos, 4], dets[pos, 5], dets[pos, 6], dets[pos, 7]
            s = dets[pos, 8]

            xmin, ymin, xmax, ymax = min(x1, x2, x3, x4), \
                min(y1, y2, y3, y4), \
                max(x1, x2, x3, x4), \
                max(y1, y2, y3, y4)
            iw = (min(txmax, xmax) - max(txmin, xmin) + 1)
            if iw > 0:
                ih = (min(tymax, ymax) - max(tymin, ymin) + 1)
                if ih > 0:
                    max_polygon = polyiou.VectorDouble(
                        [tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4])
                    pos_polygon = polyiou.VectorDouble(
                        [x1, y1, x2, y2, x3, y3, x4, y4])
                    ov = polyiou.iou_poly(max_polygon, pos_polygon)
                    if ov > 0:
                        if method == 1:  # linear
                            if ov > iou_thr:
                                weight = 1 - ov
                            else:
                                weight = 1
                        elif method == 2:  # gaussian
                            weight = np.exp(-(ov * ov) / sigma)
                        else:  # original NMS
                            if ov > iou_thr:
                                weight = 0
                            else:
                                weight = 1

                        dets[pos, 8] = weight * dets[pos, 8]

                        # if det score falls below threshold, discard the poly by
                        # swapping with last poly update N
                        if dets[pos, 8] < min_score:
                            dets[pos, 0] = dets[N - 1, 0]
                            dets[pos, 1] = dets[N - 1, 1]
                            dets[pos, 2] = dets[N - 1, 2]
                            dets[pos, 3] = dets[N - 1, 3]
                            dets[pos, 4] = dets[N - 1, 4]
                            dets[pos, 5] = dets[N - 1, 5]
                            dets[pos, 6] = dets[N - 1, 6]
                            dets[pos, 7] = dets[N - 1, 7]
                            dets[pos, 8] = dets[N - 1, 8]
                            inds[pos] = inds[N - 1]
                            N = N - 1
                            pos = pos - 1
            pos = pos + 1

    return inds[:N]
Beispiel #12
0
def BOXES_cpt(single_poly):
    xmin, ymin, xmax, ymax = min(single_poly[0::2]), min(single_poly[1::2]), \
                             max(single_poly[0::2]), max(single_poly[1::2])
    width, height = xmax - xmin, ymax - ymin
    BOX = xmin, ymin, xmax, ymax

    BOXP = np.array(single_poly).reshape(1, -1, 2)
    BOXP = np.append(BOXP, BOXP[0, :2].reshape(2, 2)).reshape(-1, 2).tolist()
    # print(BOXP)
    box_lefttop = [xmin, ymin]
    poly_1 = BOXP.pop(0)
    poly_2 = BOXP.pop(0)

    rotate_num = 0
    while len(BOXP[0]) != 0:
        rotate_num += 1
        if poly_1[0] != box_lefttop[0]:
            poly_1 = poly_2
            poly_2 = BOXP.pop(0)
        else:
            break
    # print(box_lefttop)
    # print(poly_1, poly_2)
    poly_3 = BOXP.pop(0)
    poly_1 = np.array(poly_1)
    poly_2 = np.array(poly_2)
    poly_3 = np.array(poly_3)
    vecter_1 = poly_2 - box_lefttop
    vecter_2 = poly_2 - poly_1
    Lx = np.sqrt(vecter_1.dot(vecter_1))
    Ly = np.sqrt(vecter_2.dot(vecter_2))
    if (Lx * Ly) == 0.0:
        # print(Lx, Ly)
        theta = np.pi / 2
    else:
        cos_theta = vecter_1.dot(vecter_2) / (Lx * Ly)
        # theta = np.arccos(cos_theta)
        if cos_theta > 1:
            cos_theta = 1.0
            # print(theta)
        elif cos_theta < -1:
            cos_theta = -1.0
        theta = np.arccos(cos_theta)
    poly_W = math.hypot(vecter_2[0], vecter_2[1])
    vecter_3 = poly_3 - poly_2
    poly_H = math.hypot(vecter_3[0], vecter_3[1])
    poly_1 = poly_1.tolist()  # must be list
    # print(poly_1, obj['poly'])
    # print(poly_W, poly_H, theta)
    URBOX = poly_1[0], poly_1[1], poly_W, poly_H, theta
    # print(single_poly, Nrotate(URBOX[:4], URBOX[4]))
    URBOX_iou = polyiou.iou_poly(
        polyiou.VectorDouble(single_poly),
        polyiou.VectorDouble(Nrotate(URBOX[:4], URBOX[4])))
    BBOX_iou = polyiou.iou_poly(polyiou.VectorDouble(single_poly),
                                polyiou.VectorDouble(dots4ToRec8(BOX)))
    if BBOX_iou > URBOX_iou:
        # print('exit!')
        URBOX = xmin, ymin, width, height, 0.0
        ORBOX = URBOX
    else:
        # print(URBOX_iou)
        if not rotate_num % 2:
            ORBOX = single_poly[0], single_poly[
                1], poly_W, poly_H, theta + np.pi * (rotate_num / 2.0)
        else:
            ORBOX = single_poly[0], single_poly[
                1], poly_H, poly_W, theta + np.pi * (rotate_num / 2.0)
    return BOX, URBOX, ORBOX