1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 | #include <stdint.h>
#ifndef SAIGE_VISION_WRAPPER_SAIGE_VISION_API_H_
#define SAIGE_VISION_WRAPPER_SAIGE_VISION_API_H_
#define SAIGEVISION_RUNTIME_VERSION_MAJOR 2
#define SAIGEVISION_RUNTIME_VERSION_MINOR 12
#define SAIGEVISION_RUNTIME_VERSION_PATCH 0
#define SV_STR(x) #x
#define SV_VER(M, m, p) SV_STR(M) "." SV_STR(m) "." SV_STR(p)
#define SAIGEVISION_RUNTIME_VERSION "2.12.0"
#ifdef __linux__
#define SAIGE_VISION_API extern "C"
#elif defined(SAIGEVISIONWRAPPER_EXPORTS)
#define SAIGE_VISION_API extern "C" __declspec(dllexport)
#else
#define SAIGE_VISION_API extern "C" __declspec(dllimport)
#endif
#include <stdint.h>
#include "saige_error.h"
// SaigeVision v2 Constants
constexpr int kMaxLenOfClassName = 63;
constexpr int kMaxNumOfClasses = 255;
constexpr int kMaxLenOfModelName = 63;
constexpr int kMaxLenOfCameraName = 63;
constexpr int kMaxLenOfTrainedVersion = 63;
constexpr int kProjectTypeCLS = 0; // Classification
constexpr int kProjectTypeDET = 1; // Detection
constexpr int kProjectTypeSEG = 2; // Segmentation
constexpr int kProjectTypeIAD = 3; // Anomaly Detection
constexpr int kProjectTypeOCR = 4; // OCR
constexpr int kProjectTypeIEN = 5; // IEN
// Monitoring
constexpr int kMaxLenOfToolName = 63;
// SaigeVision v2 APIs
typedef void* SaigeInferenceHandle;
typedef struct {
int batchSize;
int inspectionSizeWidth;
int inspectionSizeHeight;
} SaigeHandleConfig;
#define SAIGE_SET_BATCH_SIZE 0x01
#define SAIGE_SET_INSPECTION_SIZE 0x02
#define SAIGE_OPTIMIZE_HANDLE 0x04
typedef void* SaigeInferenceResult;
SAIGE_VISION_API int SaigeCreateInferenceHandle(SaigeInferenceHandle* handle, const int projectType,
const char* modelFilePath, const int deviceIndex,
SaigeHandleConfig* config,
const unsigned int flags);
SAIGE_VISION_API int SaigeDestroyInferenceHandle(SaigeInferenceHandle handle);
SAIGE_VISION_API int SaigeCreateInferenceResult(SaigeInferenceResult* result);
SAIGE_VISION_API int SaigeDestroyInferenceResult(SaigeInferenceResult result);
typedef struct {
char roiMode[kMaxLenOfModelName];
double left;
double top;
double right;
double bottom;
int intensityMin;
int intensityMax;
int expansion;
bool inversion;
double offsetLeft;
double offsetRight;
double offsetTop;
double offsetBottom;
unsigned char* blindMask;
int blindMaskWidth;
int blindMaskHeight;
} SaigeRoiInfo;
typedef struct {
char networkType[kMaxLenOfModelName + 1];
int numOfClasses;
char classNames[kMaxNumOfClasses][kMaxLenOfClassName + 1];
char classColors[kMaxNumOfClasses][8];
bool classIsNg[kMaxNumOfClasses];
const char* description;
const char* usedGpuName;
const char* numOfIterations;
int numOfTrainImages;
int numOfValidationImages;
long long trainingTimeStarted;
long long trainingTimeCompleted;
long long trainingTimeElapsed;
char developerVersion[kMaxLenOfTrainedVersion + 1];
char serverVersion[kMaxLenOfTrainedVersion + 1];
char moduleVersion[kMaxLenOfTrainedVersion + 1];
SaigeRoiInfo roiInfo;
float resizeFactor;
int cropSizeWidth;
int cropSizeHeight;
int imageResizeWidth;
int imageResizeHeight;
int targetTextSize;
const char* targetImageQuality;
const char* inputDataProcessingMode;
int cropNumOfRows;
int cropNumOfCols;
const char* targetTextShape;
} SaigeModelInfo;
SAIGE_VISION_API int SaigeGetModelInfo(SaigeInferenceHandle handle, SaigeModelInfo* info);
#define SAIGE_DO_NOT_INSPECT "do_not_inspect"
#define SAIGE_DO_NOT_ENHANCE "do_not_enhance"
#define SAIGE_RESIZE_TO_FIT "resize_to_fit"
#define SAIGE_CROP_INTO_TILES "crop_into_tiles"
constexpr int kMaxLenOfTextFormatting = 200;
typedef struct {
int length; // length < kMaxLenOfTextFormatting
bool engLower[kMaxLenOfTextFormatting];
bool engUpper[kMaxLenOfTextFormatting];
bool number[kMaxLenOfTextFormatting];
bool kor[kMaxLenOfTextFormatting];
bool symbol[kMaxLenOfTextFormatting];
} SaigeTextFormatting;
typedef struct {
bool calcTime;
int batchSize;
const char* oversizedImageHandling;
int inspectionSizeWidth;
int inspectionSizeHeight;
// Cls
float additionalScores[kMaxNumOfClasses];
bool calcClassActivationMap;
// Det
int maxNumOfDetectedObjects[kMaxNumOfClasses];
// Seg
bool calcScoremap;
bool calcObject;
// if calcObject == true
bool calcObjectAreaAndApplyThreshold;
bool calcObjectScoreAndApplyThreshold;
int objectBoundaryThresholdPerClass[kMaxNumOfClasses];
int objectAreaThresholdPerClass[kMaxNumOfClasses];
double objectScoreThresholdPerClass[kMaxNumOfClasses];
// IAD
bool calcMask;
bool calcHeatmap;
double anomalyScoreThreshold;
double heatmapColorRangeMin;
double heatmapColorRangeMax;
// if calcObject == true
double objectBoundaryThreshold;
int objectAreaThreshold;
double objectScoreThreshold;
// OCR
double polygonScoreThreshold;
int textSizeRangeMin;
int textSizeRangeMax;
int maxNumOfTextObjects;
double textScoreThreshold;
SaigeTextFormatting textFormatting;
} SaigeInferenceOption;
// Flags for Inference Options
#define SAIGE_WRITE_ON_MODEL_FILE 0x01
#define SAIGE_SET_CALC_TIME 0x2
#define SAIGE_SET_OVERSIZED_IMAGE_HANDLING 0x4
// CLS
#define SAIGE_SET_ADDITIONAL_SCORES 0x8
#define SAIGE_SET_CALC_CLASS_ACTIVATION_MAP 0x10
// DET
#define SAIGE_SET_MAX_NUM_OF_DETECTED_OBJECTS 0x20
// SEG
#define SAIGE_SET_CALC_SCOREMAP 0x40
#define SAIGE_SET_CALC_OBJECT 0x80
#define SAIGE_SET_CALC_OBJECT_AREA_AND_APPLY_THRESHOLD 0x100
#define SAIGE_SET_CALC_OBJECT_SCORE_AND_APPLY_THRESHOLD 0x200
#define SAIGE_SET_OBJECT_BOUNDARY_THRESHOLD 0x400
#define SAIGE_SET_OBJECT_AREA_THRESHOLD 0x800
#define SAIGE_SET_OBJECT_SCORE_THRESHOLD 0x1000
#define SAIGE_SET_CALC_MASK 0x2000
#define SAIGE_SET_CALC_HEATMAP 0x4000
#define SAIGE_SET_ANOMALY_SCORE_THRESHOLD 0x8000
#define SAIGE_SET_HEATMAP_COLOR_RANGE 0x10000
// OCR
#define SAIGE_SET_POLYGON_SCORE_THRESHOLD 0x20000
#define SAIGE_SET_TEXT_SIZE_RANGE 0x40000
#define SAIGE_SET_NUM_OF_TEXT_OBJECTS 0x80000
#define SAIGE_SET_TEXT_SCORE_THRESHOLD 0x100000
#define SAIGE_SET_TEXT_FORMATTING 0x200000
SAIGE_VISION_API int SaigeSetInferenceOption(SaigeInferenceHandle handle,
SaigeInferenceOption* option,
const unsigned int flags);
SAIGE_VISION_API int SaigeGetInferenceOption(SaigeInferenceHandle handle,
SaigeInferenceOption* option);
// Inference Functions
#define SAIGE_IMAGE_COLOR 0x01
#define SAIGE_IMAGE_16BIT 0x02
#define SAIGE_IMAGE_ALPHA 0x04
#define SAIGE_IMAGE_BGR 0x08
#define SAIGE_IMAGE_8BIT_GRAY 0x00
#define SAIGE_IMAGE_24BIT_RGB (SAIGE_IMAGE_COLOR)
#define SAIGE_IMAGE_24BIT_BGR (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_BGR)
#define SAIGE_IMAGE_32BIT_RGBA (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_ALPHA)
#define SAIGE_IMAGE_32BIT_BGRA (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_ALPHA | SAIGE_IMAGE_BGR)
#define SAIGE_IMAGE_16BIT_GRAY (SAIGE_IMAGE_16BIT)
#define SAIGE_IMAGE_48BIT_RGB (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_16BIT)
#define SAIGE_IMAGE_48BIT_BGR (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_16BIT | SAIGE_IMAGE_BGR)
#define SAIGE_IMAGE_64BIT_RGBA (SAIGE_IMAGE_COLOR | SAIGE_IMAGE_16BIT | SAIGE_IMAGE_ALPHA)
#define SAIGE_IMAGE_64BIT_BGRA \
(SAIGE_IMAGE_COLOR | SAIGE_IMAGE_16BIT | SAIGE_IMAGE_ALPHA | SAIGE_IMAGE_BGR)
SAIGE_VISION_API int SaigeInferFromImageFile(SaigeInferenceHandle handle, const char* imagePath,
SaigeInferenceResult result);
SAIGE_VISION_API int SaigeInferFromByteArray(SaigeInferenceHandle handle,
const unsigned char* imageData, const int width,
const int height, const unsigned int flags,
SaigeInferenceResult result);
SAIGE_VISION_API int SaigeInferFromByteArrayWithProductId(
SaigeInferenceHandle handle, const unsigned char* imageData, const int width, const int height,
const unsigned int flags, const char* productId, SaigeInferenceResult result);
// Batch Processing API
SAIGE_VISION_API int SaigeInferFromMultipleByteArray(
SaigeInferenceHandle handle, const int numOfImages, const unsigned char** imageData,
const int width, const int height, const unsigned int flags, SaigeInferenceResult* results);
// Multi-page Processing API
SAIGE_VISION_API int SaigeInferFromMultiPageByteArray(SaigeInferenceHandle handle,
const int numOfPages,
const unsigned char** imageData,
const int width, const int height,
const int* channels, const unsigned int flags,
SaigeInferenceResult result);
// Get Result APIs
// Classification
SAIGE_VISION_API int SaigeGetInferenceResultScore(SaigeInferenceResult result, float* scores,
bool includeAddScore);
SAIGE_VISION_API int SaigeGetInferenceResultClass(SaigeInferenceResult result, int* classIndex);
SAIGE_VISION_API int SaigeGetInferenceResultClassActivationMap(SaigeInferenceResult result,
unsigned char** classActivationMap);
// Detection
typedef struct {
double center[2];
double points[8];
double width;
double height;
double angle;
} SaigeRotatedBox;
typedef struct {
SaigeRotatedBox boundingBox;
int score;
int classIndex;
unsigned int area;
} SaigeDetectedObject;
SAIGE_VISION_API int SaigeGetInferenceResultNumOfDetectedObjects(SaigeInferenceResult result,
int* numOfObjects);
SAIGE_VISION_API int SaigeGetInferenceResultDetectedObjects(SaigeInferenceResult result,
SaigeDetectedObject* detectedObjects);
// Segmentation, Anomaly Detection
typedef struct {
double center[2];
double width;
double height;
double angle;
} SaigeEllipse;
typedef struct {
SaigeRotatedBox boundingBox;
SaigeRotatedBox boundingRotBox;
SaigeEllipse fittedEllipse;
unsigned int area;
unsigned int classIndex;
double score;
int numOfContours;
int* numOfPointsOnContour;
int* pointsOnContour;
} SaigeSegmentedObject;
// Segmentation
SAIGE_VISION_API int SaigeGetInferenceResultNumOfSegmentedObjects(SaigeInferenceResult result,
int* num);
SAIGE_VISION_API int SaigeGetInferenceResultSegmentedObjects(SaigeInferenceResult result,
SaigeSegmentedObject* segObjects);
SAIGE_VISION_API int SaigeGetInferenceResultMask(SaigeInferenceResult result, unsigned char** mask);
SAIGE_VISION_API int SaigeGetInferenceResultScoremapSEG(SaigeInferenceResult result,
unsigned char** scoremap);
SAIGE_VISION_API int SaigeGetInferenceResultIsOversized(SaigeInferenceResult result,
bool* isOversized);
// Anomaly Detection
SAIGE_VISION_API int SaigeGetInferenceResultAnomalyScores(SaigeInferenceResult result,
double* anomalyScore, double* minScore,
double* maxScore);
SAIGE_VISION_API int SaigeGetInferenceResultIsNg(SaigeInferenceResult result, bool* isNg);
SAIGE_VISION_API int SaigeGetInferenceResultScoremapIAD(SaigeInferenceResult result,
float** scoremap);
SAIGE_VISION_API int SaigeGetInferenceResultHeatmap(SaigeInferenceResult result, float** heatmap);
// OCR
typedef struct {
int numOfPoints;
double* points;
double polygonScore;
double textSize;
double textScore;
char* text;
int textLength;
} SaigeTextObject;
SAIGE_VISION_API int SaigeGetInferenceResultNumOfTextObjects(SaigeInferenceResult result, int* num);
SAIGE_VISION_API int SaigeGetInferenceResultTextObjects(SaigeInferenceResult result,
SaigeTextObject* textObjects);
SAIGE_VISION_API int SaigeGetInferenceResultEnhancedImage(SaigeInferenceResult result,
unsigned char** image);
// Common
SAIGE_VISION_API int SaigeGetInferenceResultInspectionTime(SaigeInferenceResult result,
long long* imreadTime,
long long* inferenceTime,
long long* postProcessingTime);
SAIGE_VISION_API int SaigeGetInferenceResultProductId(SaigeInferenceResult result,
const char** productId, unsigned int* length);
// Error Handling & Error Message
SAIGE_VISION_API const char* SaigeStrError(const int errnum);
SAIGE_VISION_API const char* SaigeGetModuleVersion();
// Set Monitoring Information
SAIGE_VISION_API int SaigeSetHandleMetadata(SaigeInferenceHandle handle, const char* key,
const char* value);
SAIGE_VISION_API int SaigeSetInferenceMetadata(SaigeInferenceResult result, const char* key,
const char* value);
SAIGE_VISION_API int SaigeSetResultMetadata(SaigeInferenceResult result, const char* key,
const char* value);
SAIGE_VISION_API int SaigeInitializeAlignModule(const int gpuIndex, const int sourceWidth,
const int sourceHeight, const int targetWidth,
const int targetHeight);
SAIGE_VISION_API int SaigeFinalizeAlignModule();
SAIGE_VISION_API int SaigeAlignImageFromByteArray(const unsigned char* sourceImage,
const int sourceWidth, const int sourceHeight,
const int sourceChannel,
const unsigned char* targetImage,
const int targetWidht, const int targetHeight,
const int targetChannel, unsigned int flag,
unsigned char* croppedSourceImage);
SAIGE_VISION_API int SaigeAlignImageFromFile(const char* sourceImagePath,
const char* targetImagePath, int* cropWidth,
int* cropHeight, int* cropChannel,
unsigned char* croppedSourceImage);
#endif // SAIGE_VISION_WRAPPER_SAIGE_VISION_API_H_
|