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]
Exemple #3
0
 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())