Open topic with navigation

# Configure Drawing with a Lua Script

The `Draw` transformation task draws regions on images or video frames. This section demonstrates how to customize the regions that are drawn by using a Lua script.

The Lua script that you write must define a function with the name `draw`. The function is passed each record from the input track. For example, the following function draws an ellipse on each record from the `ResultWithSource` track produced by face detection:

    function draw(record)
       drawEllipse(record.FaceResultAndImage.face.ellipse, 3, rgb(255,255,255))
    end

For information about the Lua functions that you can use to draw regions on images and video frames, refer to the HPE Media Server Reference.

To configure drawing with a Lua script

1. Create a new configuration to send to HPE Media Server with the `process` action, or open an existing configuration that you want to modify.

2. In the `[Transform]` section, add a new transformation task by setting the `TransformEngineN` parameter. You can give the task any name, for example:

       [Transform]
       TransformEngine0=Draw

3. Create a new configuration section to contain the task settings and set the following parameters:

   `Type` | The transformation engine to use. Set this parameter to `Draw`.
   `Input` | The name of the track that contains the images to draw on, with region data. The track must supply records that contain both an image and at least one region.
   `LuaScript` | The path of a Lua script to run to draw on the source images or video frames.

   For example:

       [Draw]
       Type=Draw
       Input=Demographics.ResultWithSource
       LuaScript=./configurations/drawDemographics.lua

4. Save and close the configuration file. HPE recommends that you save your configuration files in the location specified by the `ConfigDirectory` parameter.

Example configuration to draw regions on encoded images using a Lua script

An example configuration file, `drawFaces.cfg`, is included in the `configurations` folder of your HPE Media Server installation. This example runs face detection and demographics analysis on a video file or stream. It encodes images of the detected faces, using a Lua script to outline male faces in orange and female faces in purple.

    [Ingest]
    IngestEngine=AV

    [AV]
    Type=libav

    [Analysis]
    AnalysisEngine0=FaceDetect
    AnalysisEngine1=Demographics

    [FaceDetect]
    Type=FaceDetect
    NumParallel=6
    SizeUnit=percent
    MinSize=10

    [Demographics]
    Type=Demographics
    Input=FaceDetect.ResultWithSource
    NumParallel=2

    [Transform]
    TransformEngine0=Draw

    [Draw]
    Type=Draw
    Input=Demographics.ResultWithSource
    LuaScript=./configurations/drawDemographics.lua

    [Encoding]
    EncodingEngine0=Images

    [Images]
    Type=ImageEncoder
    ImageInput=Draw.Output
    OutputPath=./_output/%session.token%/%record.starttime.iso8601%.png

The Lua script `drawDemographics.lua` is included below:

    function draw(record)
      local result = record.DemographicsResultAndImage

      if ('Male' == result.gender) then
        -- draw orange ellipse
        drawEllipse(result.face.ellipse, 5, rgb(255, 128, 0))

      elseif ('Female' == result.gender) then
        -- draw purple ellipse
        drawEllipse(result.face.ellipse, 5, rgb(64, 0, 128))

      else
        -- draw grey ellipse
        drawEllipse(result.face.ellipse, 5, rgb(128, 128, 128))

      end
    end

Example Lua script to process combined/nested records

To draw outlines around multiple types of region, for example both detected faces and recognized logos, you can combine multiple tracks using a combine ESP task. The output from the combine ESP task, and the input for the drawing task, is therefore a track where records contain other records. In this case, your Lua script must process the nested records. The following script demonstrates how to do this:

    function dispatchResult(record)
    	-- handle the single records here
    end

    function uncombine(record)
    	if (record.CombineOperationData) then
    		local i = 1
    		while (record.CombineOperationData.combinedRecords[i]) do
    			uncombine(record.CombineOperationData.combinedRecords[i])
    			i = i + 1
    		end
    		uncombine(record.CombineOperationData.record0)
    	else
    		dispatchResult(record)
    	end
    end

    function draw(record)
    	uncombine(record)
    end

© 2017 Hewlett Packard Enterprise Development LP | Send documentation feedback to HPE Close Send documentation feedback To open the configured email client on this computer, open an email window. Otherwise, copy the information below to a web mail client, and send this email to AutonomyTPFeedback@hpe.com. We appreciate your feedback! Product: Topic Title: Help Topic: Feedback:

_HP_HTML5_bannerTitle.htm