Skip to main content
Skip table of contents

Code example

The code example can be found in the Breeze installation directory in the Runtime/ExampleCode folder.

For example: C:\Program Files\Prediktera\Breeze\Runtime\ExampleCode

There are example projects available in C# for .NET and python.

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

    2. Listen to events and/or data streams.

  5. Stop predicting

  6. 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));
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.