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 from an input image.

  • 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 (x,y,height,width)
        • Probability being face
        • 5 Landmarks co-ordinates values (leftEye, RightEye, noseTip, leftMouth and RightMouth)
      • int responseID

List of detectFaces features in shunya stack#

  1. Customize face probability.
  2. Get face data.

Using detectFaces#

Requirements to use detectFaces#

  1. Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
  2. Shunya AI installed in Shunya OS.

Steps to use detectFaces#

  1. Set input image location.
  2. Customize face probability.
  3. Get face data
note

Run the steps given below inside Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices) terminals.

Lets take an example use case: Say we need to

  1. Detect all those faces which have a probability of 80% or higher in an image.
  2. Get JSON output of all the detected faces.

Steps are

Step 1: Set location of the input image for face detection#

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

    git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
    cd shunya-ai-examples/indiv-components
  2. Open the examples in a text editor and modify as per your usecase.

    • For CPP you will find the examples in the folder cpp-examples/face-detection/detectFaces
    • Open the file detect_faces.cpp
    • Modify the line to set the location of the input image
    /* Modify the location of the image to your input image */
    cv::Mat inputImage = cv::imread("../images/f2.jpeg");
    • For Python you will find the examples in the folder python-examples/face-detection/detectFaces
    • Open the file detect_faces.py
    • Modify the line to set the location of the input image
    # Modify the location of the image to your input image
    img = cv2.imread("images/f2.jpeg")

Step 2: Customize face probability#

  1. Open the examples in a text editor and modify as per your usecase.

    • For CPP you will find the examples in the folder cpp-examples/face-detection/detectFaces
    • Open the file detect_faces.cpp
    • Modify the line to set the face probability to 80% or higher.
    /* Set value to 0.8 i.e (80/100) to set the probability to 80% or higher */
    probability.SetString("0.8");
    • For Python you will find the examples in the folder python-examples/face-detection/detectFaces
    • Open the file detect_faces.py
    • Modify the line to set the face probability to 80% or higher.
    # Set value to 0.8 i.e (80/100) to set the probability to 80% or higher
    inputJson['probability'] = '0.8'

Step 3: Get face data.#

  1. Once you are done editing, save and run the code, by running

    mkdir build && cd build
    cmake ../
    make
    ./faceDetectCpp
    python3 detect_faces.py
  2. Running the codes will print the JSON output on the terminal (to STDOUT).

    For Example:

    • Lets say the input image is

      Oops!, No Image to display.
    • Input JSON is

      {
      "inputImage": "4h32e898473urmcrkd947ryuemcc3x21j98k09754ycmx1k030i1d98754yn==",
      "probability": 0.8
      }
    • Then the JSON output is

      {
      "DetectedFacesJSON": [
      {
      "Face0": {
      "Rect": {
      "x": 124.75111389160156,
      "y": 124.99150848388672,
      "height": 71.9333267211914,
      "width": 59.7119140625
      },
      "Landmarks": {
      "x0": 140.8402099609375,
      "y0": 149.54171752929688,
      "x1": 167.64346313476563,
      "y1": 150.42926025390626,
      "x2": 153.3344268798828,
      "y2": 161.6752471923828,
      "x3": 141.37945556640626,
      "y3": 174.49305725097657,
      "x4": 164.75201416015626,
      "y4": 175.1620635986328
      },
      "Prob": 0.9994495511054993
      }
      },
      {
      "responseID": 1600262891
      }
      ],
      "Image": "/e98239u9r93ynm9rfumu02398r8umc9ejmrc8ew0r374y8uhmcwe,=="
      }

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

  • Download the code

    git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
    cd shunya-ai-examples/cpp-examples/face-detection
    ```shell git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git cd shunya-ai-examples/python-examples/face-detection ```
  • In this folder there is a file, face_detect.cpp or face_detect.py

  • detectFaces Components used

    subprocess::popen detectFaces("/usr/bin/detectFaces", {});
    ```shell detectFaces = Popen(['/usr/bin/detectFaces'], stdout=PIPE, stdin=PIPE) ```
  • drawFaces component used

    subprocess::popen drawFaces("/usr/bin/drawFaces", {});
    ```shell drawFaces = Popen(['/usr/bin/drawFaces'], stdout=PIPE, stdin=PIPE) ```
  • Run code by yourself

    mkdir build && cd build
    cmake .. && make
    ./faceDetectCpp
    ```shell python3 face_detect.py ```
    - You will get a new image stored in system.Oops!, No Image to display.

Facing errors with the component?#