main.cpp 3.04 KB
Newer Older
1
2
3
#include <Filesystem.h>
#include <SimpleLog.h>
#include <YOLOX.h>
yaoht's avatar
yaoht committed
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

8
void MIGraphXSamplesUsage(char *programName) {
yaoht's avatar
yaoht committed
9
10
11
12
13
14
15
    printf("Usage : %s <index> \n", programName);
    printf("index:\n");
    printf("\t 0) YOLOX sample.\n");
}

void Sample_YOLOX();

16
17
int main(int argc, char *argv[]) {
    if (argc < 2 || argc > 2) {
yaoht's avatar
yaoht committed
18
19
20
        MIGraphXSamplesUsage(argv[0]);
        return -1;
    }
21
    if (!strncmp(argv[1], "-h", 2)) {
yaoht's avatar
yaoht committed
22
23
24
        MIGraphXSamplesUsage(argv[0]);
        return 0;
    }
25
26
27
28
29
30
31
32
33
34
35
36
    switch (*argv[1]) {
    case '0': {
        Sample_YOLOX();
        break;
    }
    case '1': {
        break;
    }
    default: {
        MIGraphXSamplesUsage(argv[0]);
        break;
    }
yaoht's avatar
yaoht committed
37
38
39
40
    }
    return 0;
}

41
void Sample_YOLOX() {
yaoht's avatar
yaoht committed
42
43
44
45
    // 创建YOLOX检测器
    migraphxSamples::DetectorYOLOX detector;
    migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOX;
    initParamOfDetectorYOLOX.configFilePath = CONFIG_FILE;
46
47
48
    migraphxSamples::ErrorCode errorCode =
        detector.Initialize(initParamOfDetectorYOLOX, false);
    if (errorCode != migraphxSamples::SUCCESS) {
yaoht's avatar
yaoht committed
49
50
51
52
53
54
        LOG_ERROR(stdout, "fail to initialize detector!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize detector\n");

    // 读取测试图片
55
    cv::Mat srcImage = cv::imread("../Resource/Images/image_test.jpg", 1);
yaoht's avatar
yaoht committed
56
57

    // 静态推理固定尺寸
58
59
    std::vector<std::size_t> inputShape = {1, 3, 640, 640};

yaoht's avatar
yaoht committed
60
61
62
    // 推理
    std::vector<migraphxSamples::ResultOfDetection> predictions;
    double time1 = cv::getTickCount();
63
    detector.Detect(srcImage, inputShape, predictions, false);
yaoht's avatar
yaoht committed
64
    double time2 = cv::getTickCount();
65
    double elapsedTime = (time2 - time1) * 1000 / cv::getTickFrequency();
yaoht's avatar
yaoht committed
66
    LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);
67

yaoht's avatar
yaoht committed
68
    // 获取推理结果
69
70
71
72
    LOG_INFO(stdout, "========== Detection Results ==========\n");
    for (int i = 0; i < predictions.size(); ++i) {
        migraphxSamples::ResultOfDetection result = predictions[i];
        cv::rectangle(srcImage, result.boundingBox, cv::Scalar(0, 255, 255), 2);
yaoht's avatar
yaoht committed
73
74
75
76
77
78

        std::string label = cv::format("%.2f", result.confidence);
        label = result.className + " " + label;
        int left = predictions[i].boundingBox.x;
        int top = predictions[i].boundingBox.y;
        int baseLine;
79
80
        cv::Size labelSize =
            cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
yaoht's avatar
yaoht committed
81
        top = max(top, labelSize.height);
82
83
84
85
86
87
88
89
        cv::putText(srcImage, label, cv::Point(left, top - 10),
                    cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 255), 2);

        LOG_INFO(stdout, "box:%d %d %d %d,label:%d,confidence:%f\n",
                 predictions[i].boundingBox.x, predictions[i].boundingBox.y,
                 predictions[i].boundingBox.width,
                 predictions[i].boundingBox.height, predictions[i].classID,
                 predictions[i].confidence);
yaoht's avatar
yaoht committed
90
    }
91
92
    cv::imwrite("Result.jpg", srcImage);
    LOG_INFO(stdout, "Detection results have been saved to ./Result.jpg\n");
yaoht's avatar
yaoht committed
93
}