Processing the output on a solution¶
The Output Sink¶
Each instance outputs data by writing into different keys on the Output Sink, this is the default interface between an instance and the following components of a data pipeline.
To send data to the output sink, the following function is used:
--- Writes data to a sink
--- @param key string Key name to write data to
--- @param type OutputType Type of data to write
--- @param data table metadata to write
--- @param displayName string Display name of the data for the output panel
function instance:writeOutputSink(key, type, data, displayName) end
After all the data have been written specifc key, a call to the flush method must be made:
instance:flushOutputSink()
When the flushOutputSink method is called, CVEDIA-RT will trigger an internal event of type "output" with all the data inside. This event will then be picked up by the different modules and handlers to process the data.
Output Sink and UI¶
CVEDIA-RT UI uses the Ouput sink to get the data to visualize, for every different type that is defined when writing to the Ouput, a different type of tab will be created to show de data, these types can be:
Output Types
Different output types create different tabs on the Output or Data panels. These can be:
- image: Will create a new image source on the image dropdown list
- bbox: Allows displaying and configuring bounding boxes on top the output image
- poly: Allows displaying regions/zones on top the output image
- line: Allows displaying lines/tripwires on top the output image
- event: Allows displaying events in the Data panel
- [custom]: You can also specify other keys that will not appear in the UI, but that can be used to send data to Output Handlers and other modules
Output Sink and Output Handlers¶
To export data using the output handlers, it's required instantiating the the Output module in the onInit
funcition of the instance:
local output = api.factory.output.create(instance, "Output")
output:loadHandlersFromConfig()
The Output
name specified here represents the JSON name of the field used to look for the configured handlers.
If an Output Handler is defined in an Instance, it will be bound to the output event that is generated from flushOutputSink(), then it will get the data corresponding to the key that was defined on the Output Handler configuration. For example, for the following Output Handler configuration:
"Output": {
"handlers": [
{
"sink": "events",
"uri": "mqtt://127.0.0.1:1883/events_out",
"config": {
"script": "assets/scripts/output_handler_script.lua"
}
}
]
}
The Output Handler will look the for the output key in the output data, so in the Lua code, the following line must exist:
local events = {} -- any output metadata
Instance:writeOutputSink("events", "event", events, "")
Available Outout Schemes
For more information on the supported output schemes, please check the Export documentation.
Output Handler Scripts¶
When the script
property is defined inside the config
property of an Output handler, the Output Handler will first pass the data object through a script defined in the corresponding lua file. It uses the function with name OutputHandlerProcess
, that receives the original data as input and writes its output.
These scripts are loaded from the assets/scripts
folder under the CVEDIA-RT binary folder.
Example:
-- Example for a detections array parser. This function expects receiving the output of a Detector inference
function OutputHandlerProcess(metadata)
local result = {}
for _, entry in pairs(metadata) do
table.insert(result, {entry.x, entry.y, entry.width, entry.height, entry.label, entry.confidence})
end
return result;
end
WriteData Module¶
The WriteData module is a more low-level approach to export data using Lua.
For a detailed list of methods, view the WriteData Plugin page.
Setup¶
Declare and initialize the WriteData module state Lua variable in the onInit
function of the instance, in the index.lua
file:
local writeInst = api.factory.writedata.create(instance, "WriteData")
Usage¶
In the instance lua code, write to the Output file
local wirteInst = api.factory.writedata.get(instance, "WriteData")
writeInst:appendText("output/demotext.txt", "hello world\n")