Skip to main content

drawObjects

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 drawObjects component ?

drawObjects component is used to draw object data(detected object information) on image.

  • Description : drawObjects() takes the detected objects info(bounding boxes co-ordinates, probability and Label) as an input and original image as base64 sting and returns base64 string with drawn values on image. Check Input and output parameters for details.
  • Parameters :
    • Input(Via STDIN) : drawObjects.stdin() << inputJson << std::endl;
      • An inputJson String with following parameters:
        • Parameter1 : Information about detected Objects
          • Bounding box co-ordinates
          • Probability being object
          • Label
        • Parameter2 : Original Image (.jpg/.png) in base64 format
    • Output(Via STDOUT) : drawObjects.stdout() >> outputJson;
      • A JSON string with following contents
        • Base64 string with drawn bounding boxes and probability with label
        • int requestID

List of drawObject features in shunya stack

  1. Get Image with drawn object detections

Using drawObject

Requirements to use drawObject

  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 drawObject

  1. Read inputJson.
  2. Call API binary
  3. Store the image with drawn objectsInfo
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. Give detected objects information as as input in JSON format
  2. Get JSON output of all the detected objects drawn on image.

Steps are

Step 1. Read inputJson.

  1. Start with an ready to use template for drawing faces on 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/object-detection/drawObjects
    • Open the file draw_objects.cpp
    • Modify the line to set the input json string
    • IMP Note: inputJsonRaw.json file contains the parameter1 and parameter2 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 input image(parameter1) and face info(parameter2) as an input through STDIN.
    /*############### Call drawObjects component #############*/
    subprocess::popen drawObjects("/usr/bin/drawObjects", {});
    /* Passing inputJson to the API */
    drawObjects.stdin() << inputJson << std::endl;
    drawObjects.close();
    std::string outputJson;
    /* Getting output in outputJson */
    drawObjects.stdout() >> outputJson;
  2. You will get output in outputJson string.

Step 3. Store the image with drawn faceInfo

  1. Code to print the json output, got from drawFaces API.
    /* ---- Parse outputJson  ---- */
    /* ---- Parse draw object output ---- */
    rapidjson::Document drawObjectsJson = readJsonString(drawObjectsOut);
    std::string b64Img(drawObjectsJson["data"]["image"].GetString(),
    drawObjectsJson["data"]["image"].GetStringLength());
    cv::Mat image = base642Mat(b64Img);
    /* Storing image with draw objectDetectOut.png in system */
    cv::imwrite("objectDetectOut.png", image);

Run ready to use example.

```shell
mkdir build && cd build
cmake ../
make
# before running drawObjectsCpp make sure you have inputJsonRaw.json in build directory
./drawObjectsCpp
```
  1. 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 containing object data is

      {
      "data":{
      "objects":[
      {
      "object":"bird",
      "boundingBox":{
      "top":94.8699722290039,
      "left":146.53392028808595,
      "width":165.59767150878907,
      "height":256.9939880371094
      },
      "confidence":0.9828962087631226
      }
      ],
      "image":"/9j/4AAQSkZJRgABAQAAA
      }
      }
    • Then the JSON output is

      {
      "apiVersion": "1.2.0",
      "requestId": 1606318527,
      "data": {
      "image": "lkdn=sdfsdfin/slnsfd"
      }
      }

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

  • This is an example for object-detection and here we will be using 2 components: detectObjects and drawObjects

  • 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/object-detection
    ```shell git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git cd shunya-ai-examples/python-examples/object-detection ```
  • In this folder there is a file, object_detect.cpp or object_detect.py

  • detectbjects Components used

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

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

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

Facing errors with the component?