예제 #1
0
 def from_dlib_rect(cls, dlib_rect: dlib.rectangle):
     return cls.from_values(
         top_left_x=round(dlib_rect.left()),
         top_left_y=round(dlib_rect.top()),
         bottom_right_x=round(dlib_rect.right()),
         bottom_right_y=round(dlib_rect.bottom()),
     )
예제 #2
0
 def show_face_block(image: numpy.ndarray, face_block: dlib.rectangle):
     img_temp = image.copy()
     cv2.rectangle(img_temp, (face_block.left(), face_block.top()),
                   (face_block.right(), face_block.bottom()), (255, 0, 255),
                   2)
     cv2.imshow('', img_temp)
     cv2.waitKey(0)
def dlibrect_to_BoundingBox_onepoint(
        rect: dlib_rectangle) -> BoundingBox_onepoint:
    # take a bounding box and convert it
    # to the format (left, top, width, height)
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    # return a tuple of (left, top, w, h)
    return BoundingBox_onepoint(x, y, w, h)
예제 #4
0
def _crop(image: np.ndarray, rect: dlib.rectangle) -> np.ndarray:
    """
    Crop an image to the given rectangle.
    The input is not modified.

    Args:
        image: input image to be cropped.
        rect: rectangle to extract.
    Returns:
        the cropped image.
    """
    top = rect.top()
    bottom = rect.bottom()
    left = rect.left()
    right = rect.right()
    return image[top:bottom, left:right]
예제 #5
0
    def get_cropped_face(img, face_block: dlib.rectangle, margin='default'):
        if margin == 'default':
            margin = BboxCropper.MARGIN
        else:
            margin = margin

        lt_x, lt_y, rb_x, rb_y, w, h = face_block.left(), face_block.top(), \
                                       face_block.right() + 1, face_block.bottom() + 1, \
                                       face_block.width(), face_block.height()

        lt_ext_x = int(lt_x - margin * w)
        lt_ext_y = int(lt_y - margin * h)
        rb_ext_x = int(rb_x + margin * w)
        rb_ext_y = int(rb_y + margin * h)

        return BboxCropper.crop_image_to_dimensions(img, lt_ext_x, lt_ext_y,
                                                    rb_ext_x, rb_ext_y)
예제 #6
0
def get_bottom_right(box: dlib.rectangle, img: numpy.ndarray) -> tuple:
    """
    Extract coordinates of bounding box's bottom right corner making
    sure that corresponding pixel lies within specified image.
    :param box: dlib.rectangle - Bounding box.
    :param img: numpy.ndarray - Pixel matrix of image.
    :return: tuple - x and y coordinates of bottom right corner.
        Raises ValueError in case of an invalid pixel matrix or bounding box.
    """
    # Check bounding box.
    if box is None:
        raise ValueError

    # Check pixel matrix.
    if not is_valid_pixel_matrix(img):
        raise ValueError

    # Coordinate values should not be negative.
    x = max(0, box.right())
    y = max(0, box.bottom())

    # Coordinate values should not
    # exceed the image's width / height.
    return _adjust_coordinates(x, y, img)
예제 #7
0
def dlib_rect_to_box(rect: rectangle) -> BoundingBox:
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    return x, y, w, h
예제 #8
0
def draw_location(image: np.ndarray,
                  location: dlib.rectangle,
                  color=(0, 255, 0),
                  thickness=1) -> None:
    cv2.rectangle(image, (location.left(), location.top()),
                  (location.right(), location.bottom()), color, thickness)
예제 #9
0
 def rect_to_bbox(self, rect: dlib.rectangle):
     x = rect.left()
     y = rect.top()
     w = rect.right() - x
     h = rect.bottom() - y
     return np.array([x, y, w, h])
예제 #10
0
 def dlib_rect_to_boxes(rect: dlib.rectangle) -> tuple:
     return rect.top(), rect.right(), rect.bottom(), rect.left()
예제 #11
0
 def crate_from_dlib_rectangle(cls,
                               dlib_rect: dlib.rectangle) -> 'Rectangle':
     return Rectangle(left_top=Point(x=dlib_rect.left(), y=dlib_rect.top()),
                      right_bottom=Point(x=dlib_rect.right(),
                                         y=dlib_rect.bottom()))
예제 #12
0
def dlibrect_to_BoundingBox_twopoint(
        rect: dlib_rectangle) -> BoundingBox_twopoint:
    return BoundingBox_twopoint(rect.left(), rect.top(), rect.right(),
                                rect.bottom())
 def rect_to_bb(self, rect: dlib.rectangle) -> tuple:
     x = rect.left()
     y = rect.top()
     w = rect.right() - x
     h = rect.bottom() - y
     return (x, y, w, h)