Skip to main content

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
    • Output(Via STDOUT) : storeFace.stdout() >> outputJson;
      • A outputJson String with following contents
        • 1 (success) or 0 (failure)
        • int responseID
  • 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

  1. Store face embeddings in database.

Using storeFace

Requirements to use storeFace

  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 storeFace

  1. Read inputJson.
  2. Call API binary
  3. 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

  1. Store face embeddings of particular person in database file.

Steps are

Step 1: Read inputJson

  1. 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-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-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() };

Step 2. Call API binary

  1. 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;
  2. You will get output in outputJson string.

Step 3. Get output for storing face embeddings in dbfile

  1. 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.

  1. Run example by yourself.

    mkdir build && cd build
    cmake ../
    make
    # before running storeFaceCpp make sure you have inputJsonRaw.json in build directory
    ./storeFaceCpp
  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

      {
      "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

    mkdir build && cd build
    cmake .. && make
    ./findFaceCpp
    ```shell python3 find_face.py ```
    - You will get a name of person

Facing errors with the component?