detectFaces

warning

The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.

What is detectFaces component ?

detectFaces component is used to detect faces. It takes image and probability as json string and returns bounding box , probability ,landmarks and response id.

  • Description : detectFaces() takes an input(through STDIN) as image in base64 format and probability in float both wrapped in JSON format and returns the face information with each face probability greater than input given. Check Input and output parameters for details.
  • Parameters :
    • Input(Via STDIN) : A JSON String with following contents:
      • Input1 : Image (.jpg/.png) in base64 format
      • Input2 : Float probability (value between 0 to 1)
    • Output(Via STDOUT) : A JSON string with following contents
      • For each face
        • Bounding box co-ordinates
        • Probability being face
        • 5 Landmarks co-ordinates values
      • int responseID

List of detectFaces features in shunya stack

  1. At the input level, you can customize the image probability, i.e. if you set probability = 0.8, then you will get detections for those faces whose probability > 0.8.
  2. Face detection APIs give you bounding boxes values, 5 landmarks and probability for detected faces.

Using detectFaces features in Shunya stack

1. Setting probability threshold for faces being detected

Start with an ready to use template for detecting face from image.

git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
cd shunya-ai-examples/indiv-components

You can see there are 2 folders: cpp-examples and python-examples.

C++ Example

cd cpp-examples/face-detection/detectFaces

In C++ example(detect_faces.cpp), you can set the probability at line number 40. Range of probability is between 0 to 1.

probability.SetString("0.8");

Python Example

cd python-examples/face-detection/detectFaces

In python example(detect_faces.py), you can set the probability at line number 18.

inputJson['probability'] = '0.8'

2. Getting following details after detectFaces API

detectFaces APIs give the detected faces with

  • Bounding box co-ordinates
  • Probability being face
  • 5 Landmarks co-ordinates values

for each face. Lets look into code to understand how to get those values.

C++

// calling detectFaces API
subprocess::popen detectFaces("/usr/bin/detectFaces", {});
// Passing input through STDIN converted from json to text
detectFaces.stdin() << jsonDoc2Text(inputJson) << std::endl;
detectFaces.close();
std::string detectFacesOut;
// Get Output from STDOUT
detectFaces.stdout() >> detectFacesOut;

with above c++ program you will get the output in detectFacesOut variable which you can use to store the information about detected faces.

Python

# calling detectFaces API
detectFaces = Popen(['/usr/bin/detectFaces'], stdout=PIPE, stdin=PIPE)
# Passing input to STDIN and getting an output from STDOUT
detectFacesOut = detectFaces.communicate(input=inputJson_data.encode('utf-8'))[0]

with above python program, you will get the output in detectFacesOut variable which you can use to store the information about detected faces.

Understand this component with an example (ready to use code)

  • This is an example for face-detection and here we will be using 2 components: detectFaces and drawFaces
  • Check this ready to use example in c++ and python
  • C++ Example
    git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
    cd shunya-ai-examples/cpp-examples/face-detection
  • In this folder there is a file, face_detect.cpp
  • detectFaces Components used
    subprocess::popen detectFaces("/usr/bin/detectFaces", {});
  • drawFaces component used
    subprocess::popen drawFaces("/usr/bin/drawFaces", {});
  • Run code by yourself
    mkdir build && cd build
    cmake .. && make
    ./faceDetectCpp
    • You will get a new image stored in system.

Facing errors with the component?