dlstreamer / pipeline-server

Home of Intel(R) Deep Learning Streamer Pipeline Server (formerly Video Analytics Serving)
BSD 3-Clause "New" or "Revised" License
124 stars 50 forks source link

Using gvapython in VA Serving Pipelines #24

Closed vtimofeev01 closed 4 years ago

vtimofeev01 commented 4 years ago

I try to store some data directly from python script, set in gstreamer pipeline. but when i try to save the script stops. Please tell me how to do it. Thank u

nnshah1 commented 4 years ago

@vtimofeev01,

Most likely there is an exception being thrown in your gvapython code either on initialization or when saving data.

Before starting VA Serving, you can increase the GST_DEBUG level to 3 (you can do this via environment variable) which in most cases will give you more information on the error in the python script.

If possible - if you can share the: 1) pipeline 2) gvapython script

We might be able to see more easily what might be the issue - but I suspect a python exception.

I've added here a simple example of the gvapython code for dumping the caps from a pipeline to a file.

'''
* Copyright (C) 2019-2020 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
'''
import os
import json

class FrameInfo:

    def __init__(self):
        self._output_path = "/tmp"
        self._initialized = False
        self._source = "example"

    def write_caps(self, frame):
        path = "{}/caps.json".format(self._output_path)
        try:
            os.makedirs(self._output_path,exist_ok=True)
            os.remove(path)
        except Exception as e:
            pass

        with open(path,"w") as file:
            value = {'caps':str(frame.video_info().to_caps()),
                     'source':self._source}
            file.write(json.dumps(value))
            file.flush()

    @staticmethod
    def read_caps(input_path):
        path = "{}/caps.json".format(input_path)
        caps = None
        with open(path,"r") as file:
            caps = json.load(file)

        return caps

    def process_frame(self,frame):
        if (not self._initialized):
            self.write_caps(frame)
            self._initialized = True
        return True
vtimofeev01 commented 4 years ago

Sorry that was my fault, storing is working well but I can`t send kwarg argument to gvapython (by starting the script via sh all works well)

i tried something like this:

{
  "name": "main_settings",
  "version": 2,
  "type": "GStreamer",
  "template": [
    "urisourcebin buffer-size=4096 name=source ! decodebin ! videoconvert ! video/x-raw,format=BGR ! queue ! ",
    "gvadetect model-instance-id=inf0 model={models[person-detection-retail-0013][1][network]} model-proc={models[person-detection-retail-0013][1][proc]} name=detection ! queue ! ",
    "gvapython module=\"/home/openvino/gst/main/py_modules/test_area.py\" class=FrameInfo name=main ! ",
    "appsink name=appsink"
  ],
  "description": "persons detection w.o. face recognition",
  "parameters": {
    "type": "object",
    "properties": {
      "kwarg": {
          "element": "main",
          "type": "string",
          "default": "{\"config\":\"/home/openvino/gst/main/model_proc/filter_detections.json\",\"cam_name\":\"CAM_00\"}"
      }
    }
  }
}
nnshah1 commented 4 years ago

https://github.com/OpenVisualCloud/Smart-City-Sample/blob/master/analytics/crowd/Xeon/gst/pipeline/2/pipeline.json

Please add a "format":"json" property to the parameter. You can see the example above from smart city. Apologies as we haven't documented this requirement yet.

"element": { "name": "crowdcounting", "property": "kwarg", "format": "json" },

vtimofeev01 commented 4 years ago

I tried, but the script doesn't got kwarg...

{ "name": "main_settings", "version": 2, "type": "GStreamer", "template": [ "urisourcebin buffer-size=4096 name=source ! decodebin ! videoconvert ! video/x-raw,format=BGR ! queue ! ", "gvadetect model-instance-id=inf0 model={models[person-detection-retail-0013][1][network]} model-proc={models[person-detection-retail-0013][1][proc]} name=detection ! queue ! ", "gvapython module=\"/home/openvino/gst/main/py_modules/test_area.py\" class=FrameInfo name=main ! ", "appsink name=appsink" ], "description": "persons detection w.o. face recognition", "parameters": { "type": "object", "properties": { "main": { "element": { "name": "main", "property": "kwarg", "format": "json" }, "type": "object", "properties": { "width": { "type": "integer", "default": 100 }, "height": { "type": "integer", "default": 200 }, "zonemap": { "type": "array", "items": { "type": "object" } } } } } } }

py: `import os import json import cv2 from datetime import datetime class FrameInfo:

def __init__(self, height=1, width=1, zonemap=[]):
    self._output_path = "/home/openvino/area"
    self._initialized = False
    self._source = "example"
    self.k = {'h': height, 'w': width}

def write_caps(self, frame):
    path = "{}/caps2.json".format(self._output_path)
    try:
        os.makedirs(self._output_path, exist_ok=True)
        os.remove(path)
    except Exception as e:
        pass

    with open(path, "w") as file:
        file.write(json.dumps(self.k))
        file.flush()
    with frame.data() as mat:
        cv2.imwrite(os.path.join(self._output_path, 'im.jpg'), img=mat)

@staticmethod
def read_caps(input_path):
    path = "{}/caps2.json".format(input_path)
    caps = None
    with open(path, "r") as file:
        caps = json.load(file)

    return caps

def process_frame(self, frame):
    if (not self._initialized):
        self.write_caps(frame)
        self._initialized = True
    return True`

but in json I get: {"h": 1, "w": 1}

PS Image is stored well Thank U

nnshah1 commented 4 years ago

The default value for the object needs to be specified at the parameter level and not in the individual properties (due to a quirk in how default values are applied).

Pipeline Definition:

{
    "type": "GStreamer",
    "template": ["videotestsrc ! gvapython name=main module=temp.py class=Test ! fakesink"],
    "description": "Test Pipeline",
    "parameters": {
        "type": "object",
        "properties": {
        "main":{
            "element": {
            "name":"main",
            "format":"json",
            "property":"kwarg"
            },
            "type":"object",
            "properties": {
            "height": {
                "type":"integer",
                "minimum":1,
                "maximum":200
            },
            "width": {
                "type":"integer",
                "minimum":1,
                "maximum":100
            }
            },
            "default": {
            "height":100,
            "width":100
            }
        }
        }
    }
}

gvapython code

class Test:
    def __init__(self,height,width):
        print(height,width)

    def process_frame(self,frame):
        pass

Simple va serving code example:

'''
* Copyright (C) 2019 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
'''

import os
import json

from vaserving.vaserving import VAServing

request = {
    "source": {
        "uri": "file:///example",
        "type": "uri"
    },
    "destination": {
        "type":"file",
        "path":"/dev/stdout"
    }
}

if __name__=="__main__":

    VAServing.start({'log_level': 'DEBUG',
                     "ignore_init_errors":True})

    pipeline = VAServing.pipeline("test", 1)

    parameters = {}
    # uncomment to change default
    # parameters = {"main":{"height":50,"width":6}}

    pipeline.start(request,
                   parameters=parameters)

    VAServing.stop()
vtimofeev01 commented 4 years ago

Thank U It s great please put it into your documentation (how to make a json)! all python-zombi will be glad about josinifying of args and kwargs