Blocks.FaceDetector

 1import cv2
 2import numpy as np
 3
 4def main(inputs, outputs, parameters, synchronise):
 5    '''
 6    ## Detects Faces in the Image
 7    This block applies a Harr Cascade based model on the input image. 
 8    It takes as an input the parameter `BoxOrImage`. This parameter has two possible values:
 9    `BoxOrImage: image / box`\n
10    If `image` is given: The output is the image passed in with a bounding box around the area where 
11    a face is detected. Image is shared through the `share_image()` function.\n
12    Else if `box` is given, the output is the co-ordinates of the bounding box in the form of an array. It 
13    is chared through the `share_array()` function.
14    
15    **Inputs**: BGR Image
16
17    **Outputs**: BGR Image with Bounding Boxes
18
19    **Parameters**: BoxOrImage ('box' for Bounding Boxes, 'image' for Image with Detections)
20    '''
21    choice = parameters.read_string("BoxOrImage")
22    
23    auto_enable = False
24    try:
25        enable = inputs.read_number("Enable")
26    except Exception:
27        auto_enable = True
28
29    classifier = cv2.CascadeClassifier('utils/models/haar_cascade/haarcascade_frontalface_default.xml')
30
31    while(auto_enable or inputs.read_number('Enable')):
32        img = inputs.read_image("Img")
33        if img is None:
34            continue
35        bboxes = classifier.detectMultiScale(img)
36        
37        if choice == 'image':
38            for box in bboxes:
39                x, y, x1, y1 = box
40                x1, y1 = x+x1, y+y1
41                cv2.rectangle(img, (x, y), (x1, y1), (0,255,0), 2)
42            outputs.share_image('Out', img)
43        else:
44            to_write = [320, 240, 0, 0]
45            for box in bboxes:
46                to_write = [box[0], box[1], box[2], box[3]]
47                break
48            outputs.share_array("Out", to_write)
49    
50    synchronise()
51
52
53        
def main(inputs, outputs, parameters, synchronise)
 5def main(inputs, outputs, parameters, synchronise):
 6    '''
 7    ## Detects Faces in the Image
 8    This block applies a Harr Cascade based model on the input image. 
 9    It takes as an input the parameter `BoxOrImage`. This parameter has two possible values:
10    `BoxOrImage: image / box`\n
11    If `image` is given: The output is the image passed in with a bounding box around the area where 
12    a face is detected. Image is shared through the `share_image()` function.\n
13    Else if `box` is given, the output is the co-ordinates of the bounding box in the form of an array. It 
14    is chared through the `share_array()` function.
15    
16    **Inputs**: BGR Image
17
18    **Outputs**: BGR Image with Bounding Boxes
19
20    **Parameters**: BoxOrImage ('box' for Bounding Boxes, 'image' for Image with Detections)
21    '''
22    choice = parameters.read_string("BoxOrImage")
23    
24    auto_enable = False
25    try:
26        enable = inputs.read_number("Enable")
27    except Exception:
28        auto_enable = True
29
30    classifier = cv2.CascadeClassifier('utils/models/haar_cascade/haarcascade_frontalface_default.xml')
31
32    while(auto_enable or inputs.read_number('Enable')):
33        img = inputs.read_image("Img")
34        if img is None:
35            continue
36        bboxes = classifier.detectMultiScale(img)
37        
38        if choice == 'image':
39            for box in bboxes:
40                x, y, x1, y1 = box
41                x1, y1 = x+x1, y+y1
42                cv2.rectangle(img, (x, y), (x1, y1), (0,255,0), 2)
43            outputs.share_image('Out', img)
44        else:
45            to_write = [320, 240, 0, 0]
46            for box in bboxes:
47                to_write = [box[0], box[1], box[2], box[3]]
48                break
49            outputs.share_array("Out", to_write)
50    
51    synchronise()

Block Description

Detects Faces in the Image

This block applies a Harr Cascade based model on the input image. It takes as an input the parameter BoxOrImage. This parameter has two possible values: BoxOrImage: image / box

If image is given: The output is the image passed in with a bounding box around the area where a face is detected. Image is shared through the share_image() function.

Else if box is given, the output is the co-ordinates of the bounding box in the form of an array. It is chared through the share_array() function.

Inputs: BGR Image

Outputs: BGR Image with Bounding Boxes

Parameters: BoxOrImage ('box' for Bounding Boxes, 'image' for Image with Detections)