import warnings


def _prepare_bbox(self, bbox, image_id):
    """ Checks and converts a bbox to (4,) """
    if bbox is None:
        return None

    image_height = self.get_image_height(image_id)
    image_width  = self.get_image_width(image_id)

    # Convert to (4,)
    if len(bbox) == 2:
        assert len(bbox[0]) == 2, 'Invalid bbox shape must be either (4,) or (2, 2) but not {}'.format(bbox)
        assert len(bbox[1]) == 2, 'Invalid bbox shape must be either (4,) or (2, 2) but not {}'.format(bbox)
        bbox = [bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1]]
    assert len(bbox) == 4, 'Invalid bbox shape must be either (4,) or (2, 2) but not {}'.format(bbox)

    # Check valid
    if (bbox[2] <= bbox[0])  or (bbox[3] <= bbox[1]):
        warnings.warn('Invalid bbox {}'.format(bbox))
    if (bbox[0] < 0) or (bbox[1] < 0):
        warnings.warn('Invalid bbox {}'.format(bbox))
    if (bbox[2] > image_width) or (bbox[3] > image_height):
        warnings.warn('Invalid bbox {}, image height is {}, image width is {}'.format(
            bbox, image_width, image_height))

    return bbox