detectObjects

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 detectObjects component ?#

detectObjects component is used to detect objects from an input image.

  • Description : detectObjects() takes an input as image in base64 string format and probability and returns the object information with each object 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 object
        • Bounding box co-ordinates
        • Probability being object
        • Class label
      • int responseID

List of detectObjects features in shunya stack#

  1. Customize object probability.
  2. Get object data.
  3. Currently we support 20 classes. List below
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"

Using detectObjects#

Requirements to use detectObjects#

  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 detectObjects#

  1. Set input image location.
  2. Customize object probability.
  3. Get object 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 objects which have a probability of 80% or higher in an image.
  2. Get JSON output of all the detected objects.

Steps are

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

  1. Start with an ready to use template for detecting objects 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/object-detection/detectObjects
    • Open the file detect_objects.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/object-detection/detectObjects
    • Open the file detect_objects.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 object 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/object-detection/detectObjects
    • Open the file detect_objects.cpp
    • Modify the line to set the object 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/object-detection/detectObjects
    • Open the file detect_objects.py
    • Modify the line to set the object 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 object data.#

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

    mkdir build && cd build
    cmake ../
    make
    ./detectObjectsCpp
    python3 detect_objects.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

      {
      "Image":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUG",
      "DetectedObjectsJSON":[
      {
      "Object0":{
      "Rect":{
      "x":1.5101313591003419,
      "y":11.274658203125,
      "height":261.7319030761719,
      "width":177.1690673828125
      },
      "Label":15,
      "Prob":0.8778005838394165
      }
      },
      {
      "responseID":1606898488
      }
      ]
      }

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?#