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:
For the C# sample code, you can open the BreezeRuntimeClientExample.sln in Visual Studio. It includes the C# projects:
-
BreezeClientExample- Includes theBreezeClientCore.dllwhich 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 Python Apply Changes example shows a different use-case where you can do Apply Changes to files using the Breeze Runtime. Workflow Analysis Tree and applying changes.
The folder also contains TrainPythonModel described in Training ONNX model using external python script file.
Preconditions
-
Intro to Breeze: Classification of nuts step 1 is finished.
-
Quantification of powder samples is finished.
-
Breeze runtime workflows have been exported from Breeze as in the Runtime Classification of nuts tutorial.
-
Prediktera Simulator Camera has been configured in Breeze.
-
When Breeze Runtime starts, make sure it uses the same workspace as the tutorials.
-
To run an example, set the preferred example as a startup project in Visual Studio and press play.
Standard procedure for capturing data
-
Initialize camera
-
Load workflow
-
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.
-
-
Take dark reference
-
Close shutter
-
Take the reference
-
Open shutter
-
-
Take white reference
-
Put the white reference under the camera, then take the white reference.
-
-
Start predicting
-
Listen to events and/or data streams.
-
-
Stop predicting
-
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:
{
"Command": "InitializeCamera",
"Id": "1",
"DeviceName": "",
"RequestedPort": 3000
}
Reply:
{
"Id": "1",
"Success": true
}
Get workflows:
{
"Command": "GetWorkflows",
"Id": "2",
"IncludeTestWorkflows": false
}
Reply with a message array:
{
"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
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
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
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));
}
}