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()), )
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)
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]
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)
def get_cropped_face(img: np.ndarray, face_block: dlib.rectangle, margin=None) -> np.ndarray: if margin: margin = margin else: margin = BboxCropper.MARGIN w = face_block.width() h = face_block.height() lt_x = face_block.left() lt_y = face_block.top() margin = Cropper.get_proper_margin(w, h, lt_x, lt_y, margin) w_expand = int(w * margin) h_expand = int(h * margin) new_lt_x = lt_x - w_expand new_lt_y = lt_y - h_expand new_w = w + (2 * w_expand) new_h = h + (2 * h_expand) return img[new_lt_y:new_lt_y + new_h, new_lt_x:new_lt_x + new_w]
def get_top_left(box: dlib.rectangle, img: numpy.ndarray) -> tuple: """ Extract coordinates of bounding box's top left 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 top left 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.left()) y = max(0, box.top()) # Coordinate values should not # exceed the image's width / height. return _adjust_coordinates(x, y, img)
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
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)
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])
def dlib_rect_to_boxes(rect: dlib.rectangle) -> tuple: return rect.top(), rect.right(), rect.bottom(), rect.left()
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()))
def dlibrect_to_BoundingBox_twopoint( rect: dlib_rectangle) -> BoundingBox_twopoint: return BoundingBox_twopoint(rect.left(), rect.top(), rect.right(), rect.bottom())
def from_dlib_rect(cls, rect: rectangle) -> 'Rect': """Converts a Dlib rectangle into a Rect.""" return Rect(rect.left(), rect.top(), rect.width(), rect.height())
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)