Continuous Video Frame Capture

This example demonstrates a custom Lambda Trigger that repeatedly captures still frames using a Raspberry Pi camera. It then demonstrates a Lambda responding to the trigger by delivering the images to a Kafka based inference pipeline.

Initial Provisioning

graph LR;

cli --> control_plane;
console --> control_plane;
control_plane <-- tunnel --> agent;

subgraph user_edge[User Edge]
cli[CLI];
console[Console];
end

subgraph cloud[Cloud <small>api.on-prem.net</small>]
control_plane[Control Plane];
end

subgraph device_edge[Device Edge]
agent[Agent];
end

Subsequent Autonomous Edge Operation

graph TB;

agent --> trigger[Lambda Trigger];
trigger --> lambda1 -- image.jpeg --> kafka;
trigger --> lambda2;
trigger --> lambda3;
camera -- libcamera --> trigger;

subgraph device_edge[Device Edge]
agent;
camera[Camera]
kafka[(Redpanda Edge)]
end

subgraph agent[Agent]
trigger;
lambda1[Lambda 1];
lambda2[Lambda 2];
lambda3[Lambda 3];
end

Define the lambda trigger

$ onprem generate xid
cmalphe56a11da41vqsg
# continuous_video_frame_capture_raspberry_pi.yaml
id: cmalphe56a11da41vqsg
kind: LambdaTriggerType
name: continuous_video_frame_capture_raspberry_pi
description: >
  Capture continuous still frames from a Raspberry Pi camera.
runsAtControlPlane: false
runsAtDevices: true
scriptContentType: text/x-lua
script: >
  local M = {}

  function M.init(context)
  end

  function M.run(context)
    local filename = os.tmpname()
    while true do
      os.execute('rpicam-jpeg --nopreview -o ' .. filename)
      local file = io.open(filename, 'rb')
      local event = {
        data = file:read('*a'),
      }
      io.remove(file)
      coroutine.yield(event)
    end
  end

  return M

Upload it to the control plane

$ onprem apply ./continuous_video_frame_capture_raspberry_pi.yaml

It will now show up in the cloud console, and will now show up as one of the trigger choices when editing a Lambda.

Cloud Console

Configure a Lambda to respond

$ onprem generate xid
cmalqo656a11eos3sqb0
# deliver_images_to_kafka.yaml
id: cmalqo656a11eos3sqb0
kind: Lambda
name: deliver_images_to_kafka
description: >
  Deliver still frame images to a Kafka based inference pipeline.
runAt:
  deviceId: ci2fabp32ckvhk1g9qe0
triggerTypeId: cmalphe56a11da41vqsg
scriptContentType: text/x-lua
script: >
  local kafka = require('kafka')
  local socket = require('socket')

  local settings = {
    ['bootstrap.servers'] = 'my-broker-0:9092,my-broker-1:9092,my-broker-2:9092',
    ['group.id']          = 'onprem.lambda.kafka',
  }
  local producer = kafka.producer(settings)
  
  local M={}
  
  function M.handler(event)
    local key = socket.gettime()
    local value = event.data
    producer:produce(key, value)
    producer:poll(1000)
  end
  
  return M
$ onprem apply deliver_images_to_kafka.yaml

It will now show up in the cloud console.

Cloud Console

Manually Trigger for testing

$ onprem run lambda cmalqo656a11eos3sqb0 --event-data-from-file ./my.jpeg