Skip to content

Demo code for Base Detector Solution

This demo shows how to implement a basic detector inference model that draws bounding boxes on an output image.

Configuration files

base_config.json Base config for solution defines the detector for all instances

{
    "PVADetector": {  
        "model_file": "auto://pva_det/rgb/medium/220204b"  
    },
    "Global": {} 
}

detector_instance.json Instance config only needs to define the input source path

{
     "Input": {
        "config": {
            "uri": [
                "ffmpeg:///assets/videos/demo.mp4"
            ]
        }
    }
}

Instance code

The main solution code, present at assets/lua/index.lua, declares an Inference plugin (with a Detector model) and an Input plugin to load the frames from a video file, then applies the loaded inference on each frame and outputs the resulting bounding boxes to the sink, which afterwards are shown in the screen.

dofile(luaroot .. "/api/api.lua")
dofile(luaroot .. "/api/plugin/detector.lua")

function onStartInstance(name, config)
    local instance = api.thread.getCurrentInstance()
    instance:createWorker("OnInit", "OnRun")
end

function OnInit()
    print("onInit()")

    instance = api.thread.getCurrentInstance()

    input = api.factory.input.create(instance, "Input")
    input:setSourceFromConfig()

    detectInst = api.factory.inference.create(instance, "PVADetector")
    detectInst:loadModel(detectInst:getConfig()["model_file"])

    output = api.factory.output.create(instance, "Output")
    output:setSource()

    return true
end

function OnRun(currentTime)

    -- Get next frame, if the instance is paused we must ignore the skip_frame functionality
    local inputimg = input:readFrame(instance:isPaused())
    if inputimg ~= nil and inputimg:is_valid() then

        -- Run detector
        local inputData = instance:getInputShapes("Rect")
        local detections = detectInst:runInference(inputData, inputimg)

        instance:writeOutputSink("source", "image", inputimg, "Input image")
        instance:writeOutputSink("detections", "bbox", detections, "Raw detections (untracked)")
        instance:flushOutputSink()

        return InstanceStepResult.INSTANCE_STEP_OK
    else
        return InstanceStepResult.INSTANCE_STEP_INVALID
    end
end

solution.lua Code

Every solution must have solution.lua file tha must be present on the project folder, this file is responsible for creating the solution scope interfaces and modules, if existent. It also defines the regions and objects that can be configured on the Input configuration Window.

A base template for this file is the following:

dofile(luaroot .. "/api/api.lua")

function onInit(solution)
    solution:registerUiCallback("onInputSinkConfig", "userlabel", inputSinkConfig)  -- Register the Input configuration Window callback that is called by the UI
end

function onFirstStart(solution)
end

function onLastStop(solution)
end

function inputSinkConfig(input)

    config = {
        {name = "Detector regions", configkey = "Rect", introtext = "", itemprefix = "Region", type = "rectangle"}
    }

    return config
end