Code examples

The code examples are bundled with a Breeze installation and found in the Breeze installation folder in the Runtime/ExampleCode folder. For example: C:\Program Files\Prediktera\Breeze\Runtime\ExampleCode:

image-20260408-124842.png

For the C# sample code, you can open the BreezeRuntimeClientExample.sln in Visual Studio. It includes the C# projects:

  • BreezeClientExample - Includes the BreezeClientCore.dll which is used in these examples. It is a wrapper around the Runtime API to make it easy to use from NET and C#.

  • BreezeClientNutShellExample

  • BreezeClientPowderExample

For Python, there is:

The folder also contains TrainPythonModel described in Training ONNX model using external python script file.

Preconditions

Standard procedure for capturing data

  1. Initialize camera

  2. Load workflow

    1. Get a list of all workflows, then load the one you want or if you already know the id of the workflow you can load it right away.

  3. Take dark reference

    1. Close shutter

    2. Take the reference

    3. Open shutter

  4. Take white reference

    1. Put the white reference under the camera, then take the white reference.

  5. Start predicting

    1. Listen to events and/or data streams.

  6. Stop predicting

  7. Disconnect the camera

Connect to Breeze Runtime for sending commands

Create a new TcpClient and connect to the host where Breeze Runtime is running on the command port which by default is 2000.

Send JSON commands to Breeze Runtime as a byte array and wait for a response.

Example commands and replies

Initialize camera:

JSON
{
    "Command": "InitializeCamera",
    "Id": "1",
    "DeviceName": "",
    "RequestedPort": 3000
}

Reply:

JSON
{
    "Id": "1",
    "Success": true
}

Get workflows:

JSON
{
    "Command": "GetWorkflows",
    "Id": "2",
    "IncludeTestWorkflows": false
}

Reply with a message array:

JSON
{
    "Id": "2",
    "Success": true,
    "Message": "[{\"Name\":\"Nuts_Classification\",\"Id\":\"bd2c9b32\",\"Description\":\"\",\"CreatedTime\":\"20190417152441\",\"CreatedBy\":\"Administrator\",\"PredictionMode\":\"Normal\",\"ObjectFormat\":{\"Id\":\"2d81bdbe\",\"Name\":\"Sample - Nuts_Classification\",\"Descriptors\":[{\"Type\":\"Category\",\"Name\":\"Nut or Shell\",\"Index\":0,\"Id\":\"6a2876d0\",\"Classes\":[{\"Name\":\"-\",\"Color\":\"#ff0000\",\"Value\":0},{\"Name\": \"Nut\",\"Color\":\"#3ad23a\",\"Value\":1},{\"Name\":\"Shell\",\"Color\":\"#4664be\",\"Value\":2}]}]},\"StreamFo rmat\":{\"TimeFormat\":\"Utc100NanoSeconds\",\"Lines\":[{\"Type\":\"Category\",\"Name\":\"SampleCategory\",\" Index\":0,\"Id\":\"db8d7f96\",\"Classes\":[{\"Name\":\"-\",\"Color\":\"#ff0000\",\"Value\":0},{\"Name\":\"Sample\" ,\"Color\":\"#3ad23a\",\"Value\":1}]},{\"Type\":\"Category\",\"Name\":\"Nut or shell\",\"Index\":1,\"Id\":\"6a2876d0\",\"Classes\":[{\"Name\":\"-\",\"Color\":\"#ff0000\",\"Value\":0},{\"Name\": \"Nut\",\"Color\":\"#3ad23a\",\"Value\":1},{\"Name\":\"Shell\",\"Color\":\"#4664be\",\"Value\":2}]}]}}]"
}

Pseudo-code

C#
public string SendCommandToServer(byte[] data) 
{ 
    using var tcpClient = new TcpClient(hostName, 2000); 
    using var stream = tcpClient.GetStream(); 
    const string lineEnding = "\r\n";

    // Send command 
    stream.Write(data, 0, data.Length); 
    stream.Write(Encoding.ASCII.GetBytes(lineEnding), 0, lineEnding.Length); 

    // Read response 
    while (!stream.DataAvailable)
    { 
        Thread.Sleep(100); 
    } 
    using var memoryStream = new MemoryStream(); 

    var response = new byte[tcpClient.ReceiveBufferSize]; 
    int readByteCount; 
    while (stream.DataAvailable && (readByteCount = stream.Read(response, 0, response.Length)) > 0)
    { 
        memoryStream.Write(response, 0, readByteCount); 
    } 
    string responseStr = Encoding.ASCII.GetString(memoryStream.ToArray(), 0, (int) memoryStream.Length).Replace(lineEnding, ""); 

    return responseStr; 
}

Listening to server events (optional)

Start a new tread and create a new TcpClient and connect to the host where Breeze Runtime is running on the server event port which by default is 2500.

Pseudo-code

C#
Task.Run(() =>
{
    try
    {
        ListenToServerEvents();
    }
    catch (Exception e)
    {
        HandleException(e);
    }
});

public void ListenToServerEvents()
{
    using var tcpClient = new TcpClient(hostName, 2500);
    using var stream = tcpClient.GetStream();
    while (!_stopListeningToServerEvents)
    {
        if (!stream.DataAvailable)
        {
            Thread.Sleep(100);
            continue;
        }
        using var streamReader = new StreamReader(stream, Encoding.UTF8);
        var json = streamReader.ReadLine();
        var runtimeEvent = RuntimeEvent.FromJson(json);

        if (runtimeEvent.Event == "PredictionObject")
        {
            var predictionObjectJson = runtimeEvent.Message;
            var predictionObject =
            PredictionObject.FromJson(predictionObjectJson,_workflowSeteeup.ObjectFormat);
            HandlePredictionObject(predictionObject);
        }
    }
}

Listening to data stream (optional)

Start a new tread and create a new TcpClient and connect to the host where Breeze Runtime is running on the stream port which by default is 3000.

Pseudo-code

C#
Task.Run(() =>
{
    try
    {
        ListenToDataStream();
    }
    catch (Exception e)
    {
        HandleException(e);
    }
});

public void ListenToDataStream()
{
    using var tcpClient = new TcpClient(hostName, 3000);
    using var stream = tcpClient.GetStream();
    const int streamTypeFieldSize = sizeof(byte);
    const int inTimeFieldSize = sizeof(long);
    const int outTimeFieldSize = sizeof(long);
    const int frameNumberSize = sizeof(long);
    const int dataBodyFieldSize = sizeof(int);

    const int headerSize = streamTypeFieldSize +
        inTimeFieldSize +
        outTimeFieldSize +
        frameNumberSize +
        dataBodyFieldSize;

    const int dataBodySize = BitConverter.ToInt32(header,
        streamTypeFieldSize +
        inTimeFieldSize +
        outTimeFieldSize +
        frameNumberSize);

    while (!stopped)
    {
        byte[] header = ReadHeaderFromStream(stream, headerSize);
        byte[] data = ReadDataFromStream(stream, dataBodySize);
        HandleData(data, GetStreamType(header));
    }
}