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 (inputJson) and returns the face information with each face probability greater than input given(outputJson). Check Input and output parameters for details.
- Parameters :
- Input(Via STDIN) :
detectFaces.stdin() << jsonDoc2Text(inputJson) << std::endl;
- An inputJson String with following parameters:
- Parameter1 : Image (.jpg/.png) in base64 format
- Parameter2 : Float probability (value between 0 to 1)
- Check Step 5 to understand creation of inputJson
- An inputJson String with following parameters:
- Output(Via STDOUT) :
detectFaces.stdout() >> outputJson;
- An outputJson 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 requestID
- For each face
- Check Step 6 to understand accessing above mentioned values from outputJson json.
- An outputJson string with following contents
- Input(Via STDIN) :
#
List of detectFaces features in shunya stack- Customize face probability.
- Get face data.
detectFaces
#
Using detectFaces
#
Requirements to use - Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
- Shunya AI installed in Shunya OS.
- Shunya Video component installed in Shunya OS.
detectFaces
#
Steps to use - Set input video location.
- Read Video frames
- Convert cv image to base64 string
- Customize face probability
- Call API binary
- Print detected faces output
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
- Detect all those faces which have a probability of 80% or higher in an image.
- Get JSON output of all the detected faces.
Steps are
#
Step 1: Set input video location- Start with a ready to use template for detecting faces from video/image.
- Open the examples in a text editor and modify as per your usecase.
- We will use video compnent to set input video location in configuration file.
- Please check here, how to setup the video source path in video component.
#
Step 2: Read video framesOnce video path is set using video component, lets read video frames one by one.
- For CPP you will find the examples in the folder
cpp-examples/face-detection/
- Open the file
face_detect.cpp
- Code to read first video frame. If you want to read all frames one by one, put following code in while loop.
- For CPP you will find the examples in the folder
#
Step 3: Convert cv image to base64 string- Since detectFaces API needs image in base64 string format, we will convert image from cv::Mat to base64 string.
- Code to do it
#
Step 4: Customize face probabilityOpen 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.
- For CPP you will find the examples in the folder
#
Step 5: Call API binaryWe will now call API binary by giving input image(parameter1) and face probability(parameter2) as an input through STDIN.
You will get output in outputJson string.
#
Step 6: Print detected faces output.- Code to print the json output, got from detectFaces API.
#
Run ready to use example.Run example by yourself.
Running the codes will print the JSON output on the terminal (to STDOUT).
For Example:
Lets say the input image is
Input JSON is
Then the JSON output is
#
This part is not updated yet (please do not use below example)#
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
```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
```shell detectFaces = Popen(['/usr/bin/detectFaces'], stdout=PIPE, stdin=PIPE) ```drawFaces component used
```shell drawFaces = Popen(['/usr/bin/drawFaces'], stdout=PIPE, stdin=PIPE) ```Run code by yourself
- You will get a new image stored in system.```shell python3 face_detect.py ```