Skip to content

ObjectDetection.h

Go to the source code of this file.

Object Detection finds objects in an image and displays the location of the object as a Bounding Box.

SAIGE VISION returns the inspection results of Object Detection as a SaigeDetectedObject structure. This structure includes a class ID, Score, and a structure representing a rectangular Bounding Box. Inside SaigeDetectedObject, the Bounding Box is represented as a SaigeRotatedBox, which includes information such as the center point, vertices, width, height, and rotation angle.

Note

Object Detection does not represent the rotation angle.

Inference Options

Object Detection has the following inference options.

The following example shows how to set the option values.

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
// ExampleDetRuntime.cpp:120

SaigeInferenceOption* options = new SaigeInferenceOption;
retval = SaigeGetInferenceOption(handle, options);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceOption() failed - " << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}

// 옵션 수정
options->objectScoreThresholdPerClass[1] = 80;
options->objectScoreThresholdPerClass[2] = 80;

options->objectAreaThresholdPerClass[1] = 0;
options->objectAreaThresholdPerClass[2] = 0;

options->maxNumOfDetectedObjects[1] = -1;
options->maxNumOfDetectedObjects[2] = -1;

retval = SaigeSetInferenceOption(handle, options,
                                 SAIGE_SET_MAX_NUM_OF_DETECTED_OBJECTS |
                                     SAIGE_SET_OBJECT_SCORE_THRESHOLD |
                                     SAIGE_SET_OBJECT_AREA_THRESHOLD);
if (retval < 0) {
    std::cerr << "SaigeSetInferenceOption() failed - " << SaigeStrError(retval) << std::endl;
    if (modelInfo->numOfClasses != 3) {
        std::cerr << "이 예제는 3개의 클래스를 가지는 모델을 사용합니다." << std::endl;
    }
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}

Note

Object Detection starts class id from 1. This is because the class id 0 (OK) that indicates that no object was detected is excluded in the defect inspection. The important point to note here is that the SaigeModelInfo::numOfClasses value represents the total number of classes including class id 0.

Inference Results

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

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// ExampleDetRuntime.cpp:232

retval = SaigeGetInferenceResultNumOfDetectedObjects(result, &nObject);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultNumOfDetectedObjects() failed - "
              << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}

// Detection은 nObject에 따라 struct SaigeDetectedObject의 메모리를 할당해야합니다.
// 본 예제에서는 c++ vector를 이용해서 메모리 해제를 따로 하지않도록 합니다.
std::vector<SaigeDetectedObject> objects;
objects.resize(nObject);

retval = SaigeGetInferenceResultDetectedObjects(result, objects.data());
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultDetectedObjects() failed - "
                << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}