콘텐츠로 이동

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

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

#include <filesystem>
#include <iostream>
#include <limits>
#include <string>

using namespace std;

// ===== [site:tlb] 사이트 전용 시작 =====
static bool runSiteExample = false;  // "tlb" 인자로 선택
// ===== [site:tlb] 사이트 전용 끝 =====
int main(int argc, char* argv[]) try {
    int retval;


#ifdef _WIN32
    SetConsoleOutputCP(CP_UTF8);
#endif

    UpdateExampleSelectorFromArgv(argv[1]);

    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 (exampleSelector[ERROR_CODE] == true) {
        cout << "== Error Code Example ==" << endl;
        retval = err::run();
        if (retval < 0) {
            cerr << "Example Error Code occurs an error: " << retval << endl;
        }
    }

    if (exampleSelector[CLASSIFICATION] == true) {
        cout << "== Classification Example ==" << endl;
        retval = cls::run();
        if (retval < 0) {
            cerr << "Example Classification occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[DETECTION] == true) {
        cout << "== Object Detection Example ==" << endl;
        retval = det::run();
        if (retval < 0) {
            cerr << "Example Object Detection occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[SEGMENTATION] == true) {
        cout << "== Segmentation Example ==" << endl;
        retval = seg::run();
        if (retval < 0) {
            cerr << "Example Segmentation occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[ANOMALY_DETECTION] == true) {
        cout << "== Anomaly Detection Example ==" << endl;
        retval = iad::run();
        if (retval < 0) {
            cerr << "Example Anomaly Detection occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[OCR] == true) {
        cout << "== OCR Example ==" << endl;
        retval = ocr::run();
        if (retval < 0) {
            cerr << "Example OCR occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[IMAGE_ENHANCEMENT] == true) {
        cout << "== Image Enhancement Example ==" << endl;
        retval = ien::run();
        if (retval < 0) {
            cerr << "Example Image Enhancement occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[ROTATED_OBJECT_DETECTION] == true) {
        cout << "== Rotated Object Detection Example ==" << endl;
        retval = rod::run();
        if (retval < 0) {
            cerr << "Example Rotated Object Detection occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[CLASSIFICATION_DETECTION] == true) {
        cout << "== Classification + Object Detection Example ==" << endl;
        retval = cls_det::run();
        if (retval < 0) {
            cerr << "Example Classification + Object Detection occurs an error: " << retval << endl;
        }
    }

    if (exampleSelector[CLASSIFICATION_BATCH] == true) {
        cout << "== Classification Batch Example ==" << endl;
        retval = cls::run_batch();
        if (retval < 0) {
            cerr << "Example Classification Batch occurs an error: " << retval << endl;
        }
    }

    if (exampleSelector[ANOMALY_DETECTION_BATCH] == true) {
        cout << "== Anomaly Detection Batch Example ==" << endl;
        retval = iad::run_batch();
        if (retval < 0) {
            cerr << "Example Anomaly Detection Batch occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[SEGMENTATION_BATCH] == true) {
        cout << "== Segmentation Batch Example ==" << endl;
        retval = seg::run_batch();
        if (retval < 0) {
            cerr << "Example Segmentation Batch occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[UTILITY] == true) {
        cout << "== Utility Example ==" << endl;
        retval = util::run();
        if (retval < 0) {
            cerr << "Example Utility occurs an error: " << retval << endl;
        }
    }
    if (exampleSelector[CLASSIFICATION_WITH_META] == true) {
        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 (exampleSelector[MULTI_THREAD_INFERENCE] == true) {
        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 (exampleSelector[CLASSIFICATION_MULTIPAGE] == true) {
        cout << "== Classification Multi Page Example ==" << endl;
        retval = cls::run_multipage();
        if (retval < 0) {
            cerr << "Example Classification Multi Page occurs an error: " << retval << endl;
        }
    }

    if (exampleSelector[REPEAT_CREATE_DELETE] == true) {
        cout << "== Repeat Create and Destroy Handle Example ==" << endl;
        retval = repeat::run();
        if (retval < 0) {
            cerr << "Repeat Example occurs an error: " << retval << endl;
        }
    }

    if (exampleSelector[VALIDATION_METRIC] == true) {
        cout << "== Validation Metric Example ==" << endl;
        retval = valmetric::run();
        if (retval < 0) {
            cerr << "Example Validation Metric occurs an error: " << retval << endl;
        }
    }

    // ===== [site:tlb] 사이트 전용 시작 =====
    if (runSiteExample) {
        cout << "== TLB Align + SEG Multi Page Example ==" << endl;
        retval = tlb::run();
        if (retval < 0) {
            cerr << "Example TLB occurs an error: " << retval << endl;
        }
    }
    // ===== [site:tlb] 사이트 전용 끝 =====

    std::cout << "SaigeVision successfully completed, please press enter key to terminate"
              << std::endl;
    std::cin.get();
} catch (const std::exception& e) {
    std::cerr << "Example aborted - " << e.what() << std::endl;
    return -1;
}

void UpdateExampleSelectorFromArgv(const char* arg1) {
    std::string example;
    if (arg1 == nullptr) {
        std::cout << "Please input the example name: " << std::endl;
        cin >> example;
        cin.ignore((std::numeric_limits<std::streamsize>::max)(), '\n');
    } else {
        example = arg1;
    }
    if (example.compare("cls") == 0) {
        exampleSelector[CLASSIFICATION] = true;
    } else if (example.compare("det") == 0) {
        exampleSelector[DETECTION] = true;
    } else if (example.compare("seg") == 0) {
        exampleSelector[SEGMENTATION] = true;
    } else if (example.compare("ocr") == 0) {
        exampleSelector[OCR] = true;
    } else if (example.compare("ien") == 0) {
        exampleSelector[IMAGE_ENHANCEMENT] = true;
    } else if (example.compare("rod") == 0) {
        exampleSelector[ROTATED_OBJECT_DETECTION] = true;
    } else if (example.compare("cls_det") == 0) {
        exampleSelector[CLASSIFICATION_DETECTION] = true;
    } else if (example.compare("cls_batch") == 0) {
        exampleSelector[CLASSIFICATION_BATCH] = true;
    } else if (example.compare("iad_batch") == 0) {
        exampleSelector[ANOMALY_DETECTION_BATCH] = true;
    } else if (example.compare("seg_batch") == 0) {
        exampleSelector[SEGMENTATION_BATCH] = true;
    } else if (example.compare("util") == 0) {
        exampleSelector[UTILITY] = true;
    } else if (example.compare("cls_meta") == 0) {
        exampleSelector[CLASSIFICATION_WITH_META] = true;
    } else if (example.compare("cls_multipage") == 0) {
        exampleSelector[CLASSIFICATION_MULTIPAGE] = true;
    } else if (example.compare("multi_thread") == 0) {
        exampleSelector[MULTI_THREAD_INFERENCE] = true;
    } else if (example.compare("err") == 0) {
        exampleSelector[ERROR_CODE] = true;
    } else if (example.compare("repeat") == 0) {
        exampleSelector[REPEAT_CREATE_DELETE] = true;
    } else if (example.compare("val_metric") == 0) {
        exampleSelector[VALIDATION_METRIC] = true;
    }
    // ===== [site:tlb] 사이트 전용 시작 =====
    else if (example.compare("tlb") == 0) {
        runSiteExample = true;
    }
    // ===== [site:tlb] 사이트 전용 끝 =====
}
#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

ExampleArgs& ExampleArgs::Add(const std::string& name, std::string& value,
                              const std::string& help) {
    Item item;
    item.name = name;
    item.help = help;
    item.str = &value;
    items_.push_back(item);
    return *this;
}

ExampleArgs& ExampleArgs::Add(const std::string& name, int& value, const std::string& help) {
    Item item;
    item.name = name;
    item.help = help;
    item.num = &value;
    items_.push_back(item);
    return *this;
}

ExampleArgs& ExampleArgs::Add(const std::string& name, bool& value, const std::string& help) {
    Item item;
    item.name = name;
    item.help = help;
    item.flag = &value;
    items_.push_back(item);
    return *this;
}

static std::string Trim(const std::string& s) {
    const size_t begin = s.find_first_not_of(" \t\r\n\"");
    if (begin == std::string::npos) {
        return "";
    }
    const size_t end = s.find_last_not_of(" \t\r\n\"");
    return s.substr(begin, end - begin + 1);
}

static std::string ToDisplay(const std::string* str, const int* num, const bool* flag) {
    if (num != nullptr) {
        return std::to_string(*num);
    }
    if (flag != nullptr) {
        return *flag ? "true" : "false";
    }
    if (str->find_first_of("/\\") != std::string::npos) {
        // "images/test/../../models/a.saigecls" -> "models\a.saigecls" 처럼 읽기 쉽게 표시
        return std::filesystem::path(*str).lexically_normal().make_preferred().u8string();
    }
    return *str;
}

void ExampleArgs::Prompt() {
    std::cout << "[Arguments]" << std::endl;
    for (const auto& item : items_) {
        std::cout << "  " << item.name << ": " << ToDisplay(item.str, item.num, item.flag)
                  << std::endl;
    }
    std::cout << "Default Run Mode(Y/n): ";
    std::string line;
    // 입력이 없거나(EOF) Y/엔터면 기본값으로 실행
    if (!std::getline(std::cin, line) || (Trim(line) != "n" && Trim(line) != "N")) {
        return;
    }

    std::cout << "Please Enter program arguments." << std::endl;
    for (auto& item : items_) {
        std::cout << item.name << " (default: " << ToDisplay(item.str, item.num, item.flag)
                  << (item.help.empty() ? "" : ", " + item.help) << "): ";
        if (!std::getline(std::cin, line)) {
            break;
        }
        const std::string value = Trim(line);
        if (value.empty()) {
            continue;
        }
        if (item.str != nullptr) {
            *item.str = value;
        } else if (item.num != nullptr) {
            try {
                *item.num = std::stoi(value);
            } catch (const std::exception&) {
                std::cout << "  -> invalid number, keep default" << std::endl;
            }
        } else if (item.flag != nullptr) {
            if (value == "y" || value == "Y" || value == "true" || value == "1") {
                *item.flag = true;
            } else if (value == "n" || value == "N" || value == "false" || value == "0") {
                *item.flag = false;
            } else {
                std::cout << "  -> enter y/n, keep default" << std::endl;
            }
        }
    }
    std::cout << "[Applied Arguments]" << std::endl;
    for (const auto& item : items_) {
        std::cout << "  " << item.name << ": " << ToDisplay(item.str, item.num, item.flag)
                  << std::endl;
    }
}

std::string get_runtime_base() {
    namespace fs = std::filesystem;
    std::error_code ec;
    auto has_runtime = [&](const fs::path& base) {
        return fs::is_directory(base / "python", ec) && fs::is_directory(base / "module", ec);
    };
    const fs::path sv = fs::path(get_env_var("SAIGE_HOME")) / "SaigeVision";
    const fs::path ep = fs::path(get_env_var("SAIGE_HOME")) / "Enterprise";
    // SaigeVision wins when present; use Enterprise only if the SaigeVision layout is absent.
    if (!has_runtime(sv) && has_runtime(ep)) {
        return "Enterprise";
    }
    return "SaigeVision";
}