콘텐츠로 이동

File ExampleMain.cpp

Go to the documentation of this file.

  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
#include "ExampleMain.h"

#ifdef _WIN32
#include <Windows.h>
#endif

#include <iostream>
#include <string>

using namespace std;
int main(int argc, char* argv[]) {
    int retval;
    char cls, det, seg, iad, ocr, ien, cls_det, cls_batch, iad_batch, seg_batch, mul, all;
    char util, err, cls_with_meta;
    char cls_multipage;

#ifdef _WIN32
    SetConsoleOutputCP(CP_UTF8);
#endif

    // 실행하려는 예제를 선택하는 변수입니다. 기본 값은 'n'으로 되어있고 실행하려는 예제를 'y'로
    // 변경하면 됩니다.
    iad_batch = seg_batch = iad = cls = det = seg = ocr = ien = cls_det = cls_batch = mul = 'n';
    err = util = cls_with_meta = 'n';
    cls_multipage = 'n';
    all = 'n';
    string task = "";
    if (argv[1] != NULL) task = argv[1];
    do {
        // 실행 argument를 받아서 실행하려는 예제를 선택하는 경우
        if (task.compare("cls") == 0) {
            cls = 'y';
            break;
        } else if (task.compare("det") == 0) {
            det = 'y';
            break;
        } else if (task.compare("seg") == 0) {
            seg = 'y';
            break;
        } else if (task.compare("iad") == 0) {
            iad = 'y';
            break;
        } else if (task.compare("ocr") == 0) {
            ocr = 'y';
            break;
        } else if (task.compare("ien") == 0) {
            ien = 'y';
            break;
        } else if (task.compare("mul") == 0) {
            mul = 'y';
            break;
        } else if (task.compare("all") == 0) {
            all = 'y';
            iad_batch = seg_batch = iad = cls = det = seg = ocr = ien = cls_det = cls_batch = err =
                util = cls_with_meta = 'y';
            break;
        }
        // 코드에서 직접 실행하려는 예제를 선택하는 경우
        cls_multipage = 'y';
        if (all == 'y') {
            iad_batch = seg_batch = iad = cls = det = seg = ocr = ien = cls_det = cls_batch = err =
                util = cls_with_meta = mul = 'y';
        }
    } while (false);

    if (cls == 'n' && det == 'n' && seg == 'n' && iad == 'n' && ocr == 'n' && ien == 'n' &&
        cls_det == 'n' && cls_batch == 'n' && all == 'n' && util == 'n' && err == 'n' &&
        cls_with_meta == 'n' && mul == 'n' && cls_multipage == 'n') {
        cout << "Select example code in ExampleMain.cpp" << endl;
        return 0;
    }
    std::string saige_runtime_version =
        SV_VER(SAIGEVISION_RUNTIME_VERSION_MAJOR, SAIGEVISION_RUNTIME_VERSION_MINOR,
               SAIGEVISION_RUNTIME_VERSION_PATCH);
    cout << "SaigeVision Runtime Example (" << saige_runtime_version << ")" << endl;

    if (err == 'y') {
        cout << "== Error Code Example ==" << endl;
        retval = err::run();
        if (retval < 0) {
            cerr << "Example Error Code occurs an error: " << retval << endl;
        }
    }

    if (cls == 'y') {
        cout << "== Classification Example ==" << endl;
        retval = cls::run();
        if (retval < 0) {
            cerr << "Example Classification occurs an error: " << retval << endl;
        }
    }
    if (det == 'y') {
        cout << "== Object Detection Example ==" << endl;
        retval = det::run();
        if (retval < 0) {
            cerr << "Example Object Detection occurs an error: " << retval << endl;
        }
    }
    if (seg == 'y') {
        cout << "== Segmentation Example ==" << endl;
        retval = seg::run();
        if (retval < 0) {
            cerr << "Example Segmentation occurs an error: " << retval << endl;
        }
    }
    if (iad == 'y') {
        cout << "== Anomaly Detection Example ==" << endl;
        retval = iad::run();
        if (retval < 0) {
            cerr << "Example Anomaly Detection occurs an error: " << retval << endl;
        }
    }
    if (ocr == 'y') {
        cout << "== OCR Example ==" << endl;
        retval = ocr::run();
        if (retval < 0) {
            cerr << "Example OCR occurs an error: " << retval << endl;
        }
    }
    if (ien == 'y') {
        cout << "== Image Enhancement Example ==" << endl;
        retval = ien::run();
        if (retval < 0) {
            cerr << "Example Image Enhancement occurs an error: " << retval << endl;
        }
    }
    if (cls_det == 'y') {
        cout << "== Classification + Object Detection Example ==" << endl;
        retval = cls_det::run();
        if (retval < 0) {
            cerr << "Example Classification + Object Detection occurs an error: " << retval << endl;
        }
    }

    if (cls_batch == 'y') {
        cout << "== Classification Batch Example ==" << endl;
        retval = cls::run_batch();
        if (retval < 0) {
            cerr << "Example Classification Batch occurs an error: " << retval << endl;
        }
    }

    if (iad_batch == 'y') {
        cout << "== Anomaly Detection Batch Example ==" << endl;
        retval = iad::run_batch();
        if (retval < 0) {
            cerr << "Example Anomaly Detection Batch occurs an error: " << retval << endl;
        }
    }
    if (seg_batch == 'y') {
        cout << "== Segmentation Batch Example ==" << endl;
        retval = seg::run_batch();
        if (retval < 0) {
            cerr << "Example Segmentation Batch occurs an error: " << retval << endl;
        }
    }
    if (util == 'y') {
        cout << "== Utility Example ==" << endl;
        retval = util::run();
        if (retval < 0) {
            cerr << "Example Utility occurs an error: " << retval << endl;
        }
    }
    if (cls_with_meta == 'y') {
        cout << "== Classification with Meta Example ==" << endl;
        retval = cls::run_with_meta();
        if (retval < 0) {
            cerr << "Example Classification with Meta occurs an error: " << retval << endl;
        }
    }

    if (mul == 'y') {
        cout << "== Multi Thread Inference Example ==" << endl;
        retval = mul::run_multi_models();
        if (retval < 0) {
            cerr << "Example Multi Thread Inference occurs an error: " << retval << endl;
        }
    }

    if (cls_multipage == 'y') {
        cout << "== Classification Multi Page Example ==" << endl;
        retval = cls::run_multipage();
        if (retval < 0) {
            cerr << "Example Classification Multi Page occurs an error: " << retval << endl;
        }
    }
    std::cout << "SaigeVision successfully completed, please press enter key to terminate"
              << std::endl;
    std::cin.get();
}

#ifdef _WIN32
std::string get_env_var(const std::string& key) {
    char* envvar;
    size_t required_size;
    getenv_s(&required_size, NULL, 0, key.c_str());
    if (required_size == 0) {
        return "";
    }
    envvar = (char*)malloc(required_size * sizeof(char));
    if (!envvar) {
        printf("Failed to allocate memory!\n");
        exit(1);
    }
    getenv_s(&required_size, envvar, required_size, key.c_str());
    std::string env_var = std::string(envvar);
    free(envvar);
    return env_var;
}
#elif defined(__linux__)
std::string get_env_var(const std::string& key) {
    char* envvar = getenv(key.c_str());
    std::string env_var = std::string(envvar);
    return env_var;
}
#endif