pythoncssimageface-recognitionbounding-box

Enlarge bounding box by factor


I'm getting coordinate for recognized face bounding box from "face_recognition.face_location" as (top, right, bottom, left) in css order, and I want to enlarge the box by some factor below is the function I wrote for that using python, But it didn't give the expected results.

EXPANDING_FACTOR=0.75
def enlarge_bounding_box(t, r, b, l):
    """
    Enlarge the bounding box based on the expanding factor
    """
    # create a larger bounding box with buffer around keypoints
    t = int(t - EXPANDING_FACTOR * t)
    r = int(r - EXPANDING_FACTOR * r)
    b = int(b - EXPANDING_FACTOR * b)
    l = int(l - EXPANDING_FACTOR * l)
    t = 0 if t < 0 else t
    r = 0 if r < 0 else r
    b = 0 if b < 0 else b
    l = 0 if l < 0 else l
    return t, r, b, l

Solution

  • After some trial and error I was able to achieve desired results. Here shape is the image numpy array shape.

    EXPANDING_FACTOR=0.75
    def enlarge_bounding_box(t, r, b, l, shape):
        """
        Enlarge the bounding box based on the expanding factor
        """
        # create a larger bounding box with buffer around keypoints
        t = int(t - EXPANDING_FACTOR * t)
        r = int(r + EXPANDING_FACTOR * r)
        b = int(b + EXPANDING_FACTOR * b)
        l = int(l - EXPANDING_FACTOR * l)
        t = 0 if t < 0 else t
        r = shape[1] if r > shape[1] else r
        b = shape[0] if b > shape[0] else b
        l = 0 if l < 0 else l
        return t, r, b, l