Пример #1
0
def generate_negative_2d_bb(obj_label, boxes2d, iou_threshold_min,
                            iou_threshold_max, samples):
    """Generates negative 2D bounding boxes.
    This is computed in a brute force fashion. For any given
    bounding box, we randomly shift the centroid and generate
    new bounding boxes and if it lies within the desired IoU
    threshold bound, we will keep it. Otherwise it is thrown
    out and this is repeated until number of valid samples is
    satisfied.
    Keyword arguments:
        obj_label: single label for a detected 2D object
        boxes2d: a list of numpy array representing all
            bounding boxes in the image
        iou_threshold_min: determines the min variation
            between the original iou and the negative samples
        iou_threshold_max: determines the max variation
            between the original iou and the negative samples
        samples: number of negative samples per detected object
    Returns: a list of generated ObjectLabels
    """

    x1 = obj_label.x1
    y1 = obj_label.y1
    x2 = obj_label.x2
    y2 = obj_label.y2

    diff_x = (x2 - x1) / 2
    diff_y = (y2 - y1) / 2

    current_samples = 0
    new_objects = []

    while current_samples < samples:
        # Keep trying to generate samples that
        # lie within reasonable bound
        new_xp = np.random.uniform(x1, x2, 1)
        new_yp = np.random.uniform(y1, y2, 1)

        new_x1 = float(new_xp - diff_x)
        new_x2 = float(new_xp + diff_x)
        new_y1 = float(new_yp - diff_y)
        new_y2 = float(new_yp + diff_y)

        new_obj = od.ObjectLabel()
        new_obj.x1 = new_x1
        new_obj.x2 = new_x2
        new_obj.y1 = new_y1
        new_obj.y2 = new_y2

        new_box = np.array([new_x1, new_y1, new_x2, new_y2])

        # calculate the IoU
        iou = evaluation.two_d_iou(new_box, boxes2d)

        if iou_threshold_min < max(iou) < iou_threshold_max:
            # keep the new object label
            current_samples += 1
            new_objects.append(new_obj)

    return new_objects
Пример #2
0
def _construct_new_2d_object(new_xp, half_w, new_yp, half_l):
    """Helper function to construct a
       new object label and prepare
       arguments to calculate IoU. Used
       inside generate_negative_2d_bb

    """

    new_x1 = float(new_xp - half_w)
    new_x2 = float(new_xp + half_w)
    new_y1 = float(new_yp - half_l)
    new_y2 = float(new_yp + half_l)

    new_obj = od.ObjectLabel()
    new_obj.x1 = new_x1
    new_obj.x2 = new_x2
    new_obj.y1 = new_y1
    new_obj.y2 = new_y2

    new_box = np.array([new_x1, new_y1, new_x2, new_y2])

    return new_obj, new_box
Пример #3
0
def box_3d_to_object_label(box_3d, obj_type='Car'):
    """Turns a box_3d into an ObjectLabel

    Args:
        box_3d: 3D box in the format [x, y, z, l, w, h, ry]
        obj_type: Optional, the object type

    Returns:
        ObjectLabel with the location, size, and rotation filled out
    """

    obj_label = obj_utils.ObjectLabel()

    obj_label.type = obj_type

    obj_label.t = box_3d.take((0, 1, 2))
    obj_label.l = box_3d[3]
    obj_label.w = box_3d[4]
    obj_label.h = box_3d[5]
    obj_label.ry = box_3d[6]

    return obj_label
Пример #4
0
    def test_check_object_label_format(self):
        test_obj = obj_utils.ObjectLabel()
        test_obj.h = 1
        test_obj.w = 1
        test_obj.l = 1
        test_obj.t = [1, 1, 1]
        test_obj.ry = 0

        # Case 1, Single instance of object label
        test_obj_list = [test_obj]
        fc.check_object_label_format(test_obj_list)

        test_obj_list = [test_obj, test_obj, test_obj]
        fc.check_object_label_format(test_obj_list)

        test_obj_list = [test_obj, test_obj, '0']
        np.testing.assert_raises(TypeError, fc.check_object_label_format,
                                 test_obj_list)

        # Case 2, Range check
        test_obj.t = [1, 1]
        test_obj_list = [test_obj]
        np.testing.assert_raises(TypeError, fc.check_object_label_format,
                                 test_obj_list)
Пример #5
0
def box_2d_to_object_label(box_2d, obj_type='Car'):
    """Turns a box_2d into an ObjectLabel

    Args:
        box_2d: 3D box in the format [x1, y1, x2, y2]
        obj_type: Optional, the object type

    Returns:
        ObjectLabel with the location, size, and rotation filled out
    """

    # fc.check_box_3d_format(box_3d)
    assert box_2d.shape[1] == 4, "the box_2d should be Nx4"

    obj_label = obj_utils.ObjectLabel()

    obj_label.type = obj_type

    obj_label.x1 = box_2d[0]
    obj_label.y1 = box_2d[1]
    obj_label.x2 = box_2d[2]
    obj_label.y2 = box_2d[3]

    return obj_label
Пример #6
0
    def test_object_label_eq(self):
        # Case 1, positive case
        object_1 = obj_utils.ObjectLabel()
        object_2 = obj_utils.ObjectLabel()
        self.assertTrue(object_1 == object_2)

        object_1.t = (1., 2., 3.)
        object_2.t = (1., 2., 3.)
        self.assertTrue(object_1 == object_2)

        # Case 2, negative case (single value)
        object_1 = {}  # Not a object label type
        object_2 = obj_utils.ObjectLabel()
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.truncation = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.occlusion = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.alpha = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.x1 = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.y1 = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.x2 = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.y2 = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.h = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.w = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.l = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.t = (1., 1., 1.)
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.ry = 1.
        self.assertFalse(object_1 == object_2)

        object_1 = obj_utils.ObjectLabel()
        object_1.score = 1.
        self.assertFalse(object_1 == object_2)

        # Case 2, negative case (multiple values)
        object_1 = obj_utils.ObjectLabel()
        object_1.type = ""  # Type of object
        object_1.truncation = 1.
        object_1.occlusion = 1.
        object_1.alpha = 1.
        object_1.x1 = 1.
        object_1.y1 = 1.
        object_1.x2 = 1.
        object_1.y2 = 1.
        object_1.h = 1.
        object_1.w = 1.
        object_1.l = 1.
        object_1.t = [1., 1., 1.]
        object_1.ry = 1.
        object_1.score = 1.
        self.assertFalse(object_1 == object_2)