Example of processing with Runtime API
This example shows how to process data from an ENVI cube on disk using the perClass Mira Runtime API. The paths to the model and cube file should be modified to point to existing files.
The example shows how to:
- Query the version of the acquisition library (line 21-24)
- Initialize the acquisition plugin (line 26-30)
- Scan for available computational devices and print their names (line 34-38)
- Select a computational device (line 40-42)
- Load a model from disk and print the classifier decisions (line 44-51)
- Load the correction from disk (line 53-54)
- Initialize acquisition (line 44-50)
- Query frame geometry, data type, and layout, and check if it is valid (line 56-70)
- Load the data from the scan on disk (line 72-85)
- Enable object segmentation (line 87-89)
- Process all lines from the cube and print information of any detect object (line 91-122)
In order to compile this example, save it as example.c, and then use the command for the operating system to compile on:
On Windows using the Microsoft Visual C/C++ compiler:
> cl example.c -I "C:\Program Files\perClass Mira\include" "C:\Program Files\perClass Mira\lib\perclass_mira_gpu.lib"
On Linux using GCC:
> gcc example.c -o example -I"/opt/perClass/Mira/include" -L"/opt/perClass/Mira/lib" -lperclass_mira_gpu
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "perclass_mira.h"
- #define MODEL_PATH "./model.mpl"
- #define CUBE_DIR "./data"
- #define CUBE_NAME "cube"
- #define CUBE_EXT ".pcf"
- #define CUBE_WIDTH 640
- #define CUBE_BANDS 200
- #define CUBE_HEIGHT 1550
- int main(int argc, const char* argv[])
- {
- int res = MIRA_OK;
- uint16_t* pBuf = NULL;
- printf("Example of processing data using perClass Mira Runtime\n");
- const char* pVersionStr = mira_GetVersion();
- printf("Version: '%s'\n", pVersionStr);
- mrkernel* pmr = mira_Init(".");
- printf("Init: %s", mira_GetErrorMsg(pmr));
- if (pmr == NULL) {
- goto Error;
- }
- MIRA_CHECK(mira_RefreshDeviceList(pmr, 1, 0));
- const int deviceCount = mira_GetDeviceCount(pmr);
- printf("\n%d devices:\n", deviceCount);
- for (int i = 0; i < deviceCount; i++) {
- printf(" %d : %s\n", i, mira_GetDeviceName(pmr, i));
- }
- int deviceInd = 0;
- MIRA_CHECK(mira_SetDevice(pmr, deviceInd));
- printf("Device selected: %d '%s'\n", deviceInd, mira_GetDeviceName(pmr, deviceInd));
- printf("Loading model from '%s'...\n", MODEL_PATH);
- MIRA_CHECK(mira_LoadModel(pmr, MODEL_PATH));
- const int decisionCount = mira_GetDecCount(pmr);
- printf("\n%d model decisions:\n", decisionCount);
- for (int i = 0; i < decisionCount; i++) {
- printf(" %d : %s\n", i, mira_GetDecName(pmr, i));
- }
- printf("Loading correction...\n");
- MIRA_CHECK(mira_LoadCorrection(pmr, CUBE_DIR, CUBE_NAME));
- printf("Expected geometry: width=%d bands=%d lines=%d dataType=%d dataLayout=%d\n",
- mira_GetInputWidth(pmr),
- mira_GetInputBands(pmr),
- mira_GetInputHeight(pmr),
- mira_GetInputDataType(pmr),
- mira_GetInputDataLayout(pmr));
- if (mira_GetInputBands(pmr) != CUBE_BANDS) {
- printf("The number of cube bands (%d) does not match the number of expected bands\n", CUBE_BANDS);
- goto Error;
- }
- if (mira_GetInputHeight(pmr) != 1) {
- printf("Line-scan model expected by this example\n", CUBE_BANDS);
- goto Error;
- }
- printf("Loading data from '%s/%s'...\n", CUBE_DIR, CUBE_NAME CUBE_EXT);
- FILE* pFile = fopen(CUBE_DIR "/" CUBE_NAME CUBE_EXT, "rb");
- if (pFile == NULL) {
- printf("Cannot open file for reading\n");
- goto Error;
- }
- size_t frameSize = CUBE_WIDTH * CUBE_BANDS * sizeof(uint16_t);
- pBuf = malloc(frameSize * CUBE_HEIGHT);
- size_t frames = fread(pBuf, frameSize, CUBE_HEIGHT, pFile);
- printf("Read %zu frames\n", frames);
- fclose(pFile);
- printf("Setting segmentation...\n");
- MIRA_CHECK(mira_SetMinObjSize(pmr, 500));
- MIRA_CHECK(mira_SetSegmentation(pmr, 1));
- printf("Starting acquisition...\n");
- MIRA_CHECK(mira_StartAcquisition(pmr));
- uint16_t* ptr = pBuf;
- int objectCount = 0;
- int* pObjectData = NULL;
- printf("Processing %zu frames:\n", frames);
- for (int i = 0; i < frames; i++) {
- mira_ProcessFrame(pmr, ptr);
- objectCount = mira_GetObjCount(pmr);
- if (objectCount > 0) {
- for (int j = 0; j < objectCount; j++) {
- mira_GetObjDataInt(pmr, j, &pObjectData);
- printf("Object %d : %d,%d size=%d decision=%d box=[%d:%d,%d:%d]\n",
- pObjectData[MIRA_OBJECT_ID],
- pObjectData[MIRA_OBJECT_POS], pObjectData[MIRA_OBJECT_FRAME],
- pObjectData[MIRA_OBJECT_SIZE],
- pObjectData[MIRA_OBJECT_CLASS],
- pObjectData[MIRA_OBJECT_MINCOL], pObjectData[MIRA_OBJECT_MAXCOL],
- pObjectData[MIRA_OBJECT_MINFRAME], pObjectData[MIRA_OBJECT_MAXFRAME]);
- }
- }
- ptr += frameSize;
- }
- printf("Stopping acquisition\n");
- MIRA_CHECK(mira_StopAcquisition(pmr));
- Error:
- if (res != MIRA_OK) {
- printf("Error %d: %s", mira_GetErrorCode(pmr), mira_GetErrorMsg(pmr));
- }
- mira_Release(pmr);
- if (pBuf != NULL) free(pBuf);
- return 0;
- }