Search code examples
pythonface-detection

Error: draw_bounding_box() missing 2 required positional arguments: 'r' and 'd'


I am using the following code to detect face and draw rectangle on top of the face like this one .

enter image description here

Inference.py In this file we are trying to draw raw_bounding_box around the face:

import cv2
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing import image

def load_image(image_path, grayscale=False, target_size=None):
    pil_image = image.load_face_coordinates(image_path, grayscale, target_size)
    return image.face_coordinates_to_array(pil_image)

def load_detection_model(model_path):
    detection_model = cv2.CascadeClassifier(model_path)
    return detection_model

def detect_faces(detection_model, gray_image_array):
    return detection_model.detectMultiScale(gray_image_array, 1.3, 5)

def draw_bounding_box(face_coordinates, image_array, color,r,d):
    x1,y1,x2,y2 = face_coordinates
    # cv2.rectangle(image_array, (x, y), (x + w, y + h), color, 2)
    cv2.line(image_array, (x1 + r, y1), (x1 + r + d, y1), color, 2)
    cv2.line(image_array, (x1, y1 + r), (x1, y1 + r + d), color, 2)
    cv2.ellipse(image_array, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, 2)
    # Top right
    cv2.line(image_array, (x2 - r, y1), (x2 - r - d, y1), color, 2)
    cv2.line(image_array, (x2, y1 + r), (x2, y1 + r + d), color, 2)
    cv2.ellipse(image_array, (x2 - r, y1 + r), (r, r), 270, 0, 90, color, 2)

    # Bottom left
    cv2.line(image_array, (x1 + r, y2), (x1 + r + d, y2), color, 2)
    cv2.line(image_array, (x1, y2 - r), (x1, y2 - r - d), color, 2)
    cv2.ellipse(image_array, (x1 + r, y2 - r), (r, r), 90, 0, 90, color, 2)

    # Bottom right
    cv2.line(image_array, (x2 - r, y2), (x2 - r - d, y2), color, 2)
    cv2.line(image_array, (x2, y2 - r), (x2, y2 - r - d), color, 2)
    cv2.ellipse(image_array, (x2 - r, y2 - r), (r, r), 0, 0, 90, color, 2)

    image_array = np.zeros((256,256,3), dtype=np.uint8)

detectface.py In this file we are detecting the face and calling the functions from Inference.py to draw the boxes around the face.

# starting video streaming
cv2.namedWindow('window_frame')
video_capture = cv2.VideoCapture(0)
while True:
    bgr_image = video_capture.read()[1]
    gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
    faces = detect_faces(face_detection, gray_image)

    for face_coordinates in faces:

        x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]
        try:
            gray_face = cv2.resize(gray_face, (emotion_target_size))
        except:
            continue
        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        emotion_prediction = emotion_classifier.predict(gray_face)
        emotion_probability = np.max(emotion_prediction)
        emotion_label_arg = np.argmax(emotion_prediction)
        emotion_text = emotion_labels[emotion_label_arg]
        emotion_window.append(emotion_text)

        if len(emotion_window) > frame_window:
            emotion_window.pop(0)
        try:
            emotion_mode = mode(emotion_window)
        except:
            continue

        if emotion_text == 'angry':

            color = emotion_probability * np.asarray((255, 0, 0))
        elif emotion_text == 'sad':
            color = emotion_probability * np.asarray((0, 0, 255))

        elif emotion_text == 'happy':
            color = emotion_probability * np.asarray((0, 128, 255))
        elif emotion_text == 'surprise':
            color = emotion_probability * np.asarray((0, 255, 255))
        else:
            color = emotion_probability * np.asarray((0, 255, 0))

        color = color.astype(int)
        color = color.tolist()

        draw_bounding_box(face_coordinates, rgb_image, color)

The last line of code in this file (detectface.py) doesn't seems to be right so i don't know how to add the two missing required positional arguments: 'r' and 'd' in this file. Please share if you have any idea to achieve this goal


Solution

  • What draw_bounding_box() does is draw something like the green frame in your sample image, including support for rounded corners.

    IMHO this is a case where picture is worth a thousand words, so let's have a look at the top left corner (the other 3 segments follow the same pattern, just rotated).

    Diagram showing meaning of <code>r</code> and <code>d</code>

    which is generated by

    cv2.line(image_array, (x1 + r, y1), (x1 + r + d, y1), color, 2)
    cv2.line(image_array, (x1, y1 + r), (x1, y1 + r + d), color, 2)
    cv2.ellipse(image_array, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, 2)
    

    and where

    • (x1, y1) specifies the top-left corner of the rectangular area we want to draw the frame around.
    • r is the radius of the circular arc (the rounded corner)
    • d is the length of the 2 lines (horizontal and vertical)
    • color is the colour to draw the lines and arcs with
    • 2 is the thickness of the lines and arcs

    As to how to set the parameters...

    The r parameter seems more of an aesthetic choice -- I'd say something around 8 might look alright, although the sample image seem to have no rounded corners, which would mean r == 0. I'm not sure (meaning I'm too lazy to try right now ;) ) how happy cv2.ellipse will be drawing a 0 radius ellipse, but a simple if statement can solve that problem (i.e. only call cv2.ellipse when r > 0).

    The d parameter seems like it ought to be set such that the gap should be roughly 33% of the ROI. I'd select the smaller dimension (i.e. min(width, height)) of the ROI, divide it by 3, subtract r and use the result.