Skip to content

OCR.h

Go to the source code of this file.

OCR finds the location of a string in an image as a Bounding Box and returns the string in unicode format.

SAIGE VISION returns the detected string as a SaigeTextObject structure. This structure includes the location of the string as an array of points in Polygon form, the confidence score of the Polygon location, the probabilistic score of the recognized character, and the detected string.

Inference Options

OCR has the following inference options.

Inspection Size Width, Height

This is one of the options that can be set when creating a handle using the SaigeHandleConfig structure. Like SaigeInferenceOption::batchSize, this is an option that affects memory settings when creating a handle and cannot be changed after creating the handle. Identical to the Segmentation function.

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
// ExampleSegRuntime.cpp:64

SaigeInferenceHandle handle = nullptr;
SaigeInferenceHandle handle2 = nullptr;
SaigeInferenceHandle handle3 = nullptr;
SaigeHandleConfig config;
config.inspectionSizeHeight = 2496;
config.inspectionSizeWidth = 2496;
unsigned int handleFlags = 0;
if (optimize) {
    handleFlags |= SAIGE_OPTIMIZE_HANDLE;
}
retval = SaigeCreateInferenceHandle(&handle, kProjectTypeSEG, trainedModelPath2.c_str(),
                                    1, &config, handleFlags);
if (retval < 0) {
    std::cerr << "SaigeCreateInferenceHandle failed - " << SaigeStrError(retval) << std::endl;
    return retval;
}

retval = SaigeCreateInferenceHandle(&handle2, kProjectTypeSEG, trainedModelPath.c_str(),
                                    0, &config, handleFlags);
if (retval < 0) {
    std::cerr << "SaigeCreateInferenceHandle failed - " << SaigeStrError(retval) << std::endl;
    return retval;
}

retval = SaigeCreateInferenceHandle(&handle3, kProjectTypeSEG, trainedModelPath.c_str(), 1,
                                    &config, handleFlags);
if (retval < 0) {
    std::cerr << "SaigeCreateInferenceHandle failed - " << SaigeStrError(retval) << std::endl;
    return retval;
}

Detection Area Options

These options are applied during the detection of the character area in OCR. These options are used to filter the detected character area.

Text Recognition Options

These options are applied during the character recognition stage of OCR. These options are used to filter the recognized characters based on the score or a specific character string pattern.

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ExampleOCRRuntime.cpp:145

/*options->textFormatting.length = 5;
SaigeInitTextFormatting(options->textFormatting);
for (int i = 0; i < options->textFormatting.length; ++i) {
    options->textFormatting.engUpper[i] = true;
    options->textFormatting.number[i] = true;
}

retval = SaigeSetInferenceOption(handle, options, SAIGE_SET_TEXT_FORMATTING);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceOption() failed - " << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}*/
/* options->oversizedImageHandling = SAIGE_RESIZE_TO_FIT;
 retval = SaigeSetInferenceOption(handle, options, SAIGE_SET_OVERSIZED_IMAGE_HANDLING);
 if (retval < 0) {
     std::cerr << "SaigeGetInferenceOption() failed - " << SaigeStrError(retval) << std::endl;
     SaigeDestroyInferenceHandle(handle);
     SaigeDestroyInferenceResult(result);
     return retval;
 }*/

Inference Results

To get the detected string, first get the number of strings and then use the SaigeTextObject structure array to get the string information. Here, we use the C++ STL vector to handle memory allocation/deallocation in the vector.

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ExampleOCRRuntime.cpp:276

int numTextObject;
long long imreadTime, inferenceTime, postProcessingTime;
std::vector<SaigeTextObject> textObjects;
retval = SaigeGetInferenceResultNumOfTextObjects(result, &numTextObject);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultNumOfTextObjects() failed - "
              << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}
textObjects.resize(numTextObject);
retval = SaigeGetInferenceResultTextObjects(result, textObjects.data());
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultTextObjects() failed - " << SaigeStrError(retval)
              << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}