storeFace
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 storeFace component ?
storeFace component is used to store face-embeddings of person with person name in database file.
- Description : storeFace() takes an input(through STDIN) as 128 float face embeddings, person name, database file name wrapped in JSON format and returns 1 or 0 for success or failure respectively. Check Input and output parameters for details.
- Parameters :
- Input(Via STDIN) :
storeFace.stdin() << inputJson << std::endl;
- An inputJson String with following parameters:
- Parameter1 : Vector of 128 float embeddings
- Parameter2 : Person name
- Parameter3 : Database file name
- An inputJson String with following parameters:
- Output(Via STDOUT) :
storeFace.stdout() >> outputJson;
- A outputJson String with following contents
- 1 (success) or 0 (failure)
- int responseID
- A outputJson String with following contents
- Input(Via STDIN) :
- Using storeFace component
- With our getEmbeddings component: To understand how to use storeFace with getEmbeddings component, check this section
- You can also use storeFace with any other getEmbeddings component but inputJson should be in the mentioned format.
List of storeFace features in shunya stack
- Store face embeddings in database.
Using storeFace
Requirements to use storeFace
- Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
- Shunya AI installed in Shunya OS.
Steps to use storeFace
- Read inputJson.
- Call API binary
- Get output for storing face embeddings in dbfile
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
- Store face embeddings of particular person in database file.
Steps are
Step 1: Read inputJson
Start with an ready to use template for aligning faces from image.
git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
cd shunya-ai-examples/indiv-componentsOpen 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-recognition/storeFace
- Open the file
store_face.cpp
- Modify the line to set input parameters
IMP Note
: inputJsonRaw.json file contains the parameter1, parameter2 and parameter3 of input. If you want to see how it looks you can check below.
/* Reading the json file and convert it into the Json string format */
std::ifstream ifs { R"(inputJsonRaw.json)" };
if ( !ifs.is_open() )
{
std::cerr << "Could not open file for reading!\n";
return EXIT_FAILURE;
}
IStreamWrapper isw { ifs };
Document doc {};
doc.ParseStream( isw );
StringBuffer buffer {};
Writer<StringBuffer> writer { buffer };
doc.Accept( writer );
if ( doc.HasParseError() )
{
std::cout << "Error : " << doc.GetParseError() << '\n'
<< "Offset : " << doc.GetErrorOffset() << '\n';
return EXIT_FAILURE;
}
/* Finally we get inputJson string */
const std::string inputJson { buffer.GetString() };- For CPP you will find the examples in the folder
Step 2. Call API binary
- We will now call API binary by giving embeddings(parameter1), person name(parameter2), database file name(parameter3) as an input through STDIN.
/*########### Call storeFace Component ###############*/
subprocess::popen storeFace("/usr/bin/storeFace", {});
/* Passing inputJson to the API */
storeFace.stdin() << inputJson << std::endl;
storeFace.close();
std::string outputJson;
/* Getting output in outputJson */
storeFace.stdout() >> outputJson; - You will get output in outputJson string.
Step 3. Get output for storing face embeddings in dbfile
- Code to print the json output, got from storeFace API.
/* ---- Parse outputJson ---- */
/* ---- Parse outputJson ---- */
rapidjson::Document storeFaceJson = readJsonString(outputJson);
int res1 = storeFaceJson["data"]["success"].GetInt();
if(res1==1) {
std::cout<<"\nFace stored successfully !";
} else {
std::cout<<"\nError storing the face !";
}
Run ready to use example.
Run example by yourself.
mkdir build && cd build
cmake ../
make
# before running storeFaceCpp make sure you have inputJsonRaw.json in build directory
./storeFaceCppRunning the codes will print the JSON output on the terminal (to STDOUT).
For Example:
Lets say the input image is
Input JSON is
{
"embeddings": [{
"value": -0.00516019
}{
"value": -0.00523019
}
],
"fname": "file.txt",
"pname": "Sneha"
}Then the JSON output is
{
"apiVersion": "1.2.0",
"requestId": 1606318527,
"data": {
"success": 1
}
}
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-recognition and here we will be using 5 components: detectFaces, alignFace, getEmbeddings, storeFace, findFace
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-recognition```shell git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git cd shunya-ai-examples/python-examples/face-recognition ```In this folder there is a file, find_face.cpp or find_face.py
detectFaces Components used
subprocess::popen detectFaces("/usr/bin/detectFaces", {});
```shell detectFaces = Popen(['/usr/bin/detectFaces'], stdout=PIPE, stdin=PIPE) ```alignFace component used
subprocess::popen alignFace("/usr/bin/alignFace", {});
```shell alignFace = Popen(['/usr/bin/alignFace'], stdout=PIPE, stdin=PIPE) ```getEmbeddings component used
subprocess::popen getEmbeddings("/usr/bin/getEmbeddings", {});
```shell getEmbeddings = Popen(['/usr/bin/getEmbeddings'], stdout=PIPE, stdin=PIPE) ```storeFace component used
subprocess::popen storeFace("/usr/bin/storeFace", {});
```shell storeFace = Popen(['/usr/bin/storeFace'], stdout=PIPE, stdin=PIPE) ```findFace component used
subprocess::popen findFace("/usr/bin/findFace", {});
```shell findFace = Popen(['/usr/bin/findFace'], stdout=PIPE, stdin=PIPE) ```Run code by yourself
- You will get a name of personmkdir build && cd build
cmake .. && make
./findFaceCpp```shell python3 find_face.py ```