main.cpp 6.04 KB
Newer Older
Your Name's avatar
Your Name committed
1
2
3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
liucong's avatar
liucong committed
4
5
6
#include <SimpleLog.h>
#include <Filesystem.h>
#include <YOLOV5.h>
Your Name's avatar
Your Name committed
7

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

void Sample_YOLOV5();
void Sample_YOLOV5_Dynamic();

liucong's avatar
liucong committed
19
int main(int argc, char* argv[])
shizhm's avatar
shizhm committed
20
{
liucong's avatar
liucong committed
21
    if(argc < 2 || argc > 2)
shizhm's avatar
shizhm committed
22
23
24
25
    {
        MIGraphXSamplesUsage(argv[0]);
        return -1;
    }
liucong's avatar
liucong committed
26
    if(!strncmp(argv[1], "-h", 2))
shizhm's avatar
shizhm committed
27
28
29
30
    {
        MIGraphXSamplesUsage(argv[0]);
        return 0;
    }
liucong's avatar
liucong committed
31
    switch(*argv[1])
shizhm's avatar
shizhm committed
32
    {
liucong's avatar
liucong committed
33
34
35
36
37
38
39
40
41
42
43
44
    case '0': {
        Sample_YOLOV5();
        break;
    }
    case '1': {
        Sample_YOLOV5_Dynamic();
        break;
    }
    default: {
        MIGraphXSamplesUsage(argv[0]);
        break;
    }
shizhm's avatar
shizhm committed
45
46
47
48
49
    }
    return 0;
}

void Sample_YOLOV5()
Your Name's avatar
Your Name committed
50
{
liucong's avatar
liucong committed
51
52
53
54
    // 创建YOLOV5检测器
    migraphxSamples::DetectorYOLOV5 detector;
    migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOV5;
    initParamOfDetectorYOLOV5.configFilePath = CONFIG_FILE;
liucong's avatar
liucong committed
55
56
    migraphxSamples::ErrorCode errorCode = detector.Initialize(initParamOfDetectorYOLOV5, false);
    if(errorCode != migraphxSamples::SUCCESS)
shizhm's avatar
shizhm committed
57
58
59
60
61
62
63
    {
        LOG_ERROR(stdout, "fail to initialize detector!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize detector\n");

    // 读取测试图片
liucong's avatar
liucong committed
64
    cv::Mat srcImage = cv::imread("../Resource/Images/DynamicPics/image1.jpg", 1);
shizhm's avatar
shizhm committed
65
66

    // 静态推理固定尺寸
liucong's avatar
liucong committed
67
68
    std::vector<std::size_t> inputShape = {1, 3, 608, 608};

shizhm's avatar
shizhm committed
69
70
    // 推理
    std::vector<migraphxSamples::ResultOfDetection> predictions;
liucong's avatar
liucong committed
71
72
    detector.Detect(srcImage, inputShape, predictions, false);

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

        std::string label = cv::format("%.2f", result.confidence);
liucong's avatar
liucong committed
81
82
83
        label             = result.className + " " + label;
        int left          = predictions[i].boundingBox.x;
        int top           = predictions[i].boundingBox.y;
shizhm's avatar
shizhm committed
84
85
        int baseLine;
        cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
liucong's avatar
liucong committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        top                = max(top, labelSize.height);
        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);
shizhm's avatar
shizhm committed
103
    }
liucong's avatar
liucong committed
104
105
    cv::imwrite("Result.jpg", srcImage);
    LOG_INFO(stdout, "Detection results have been saved to ./Result.jpg\n");
shizhm's avatar
shizhm committed
106
107
108
109
110
111
112
113
}

void Sample_YOLOV5_Dynamic()
{
    // 创建YOLOV5检测器
    migraphxSamples::DetectorYOLOV5 detector;
    migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOV5;
    initParamOfDetectorYOLOV5.configFilePath = CONFIG_FILE;
liucong's avatar
liucong committed
114
115
    migraphxSamples::ErrorCode errorCode     = detector.Initialize(initParamOfDetectorYOLOV5, true);
    if(errorCode != migraphxSamples::SUCCESS)
Your Name's avatar
Your Name committed
116
    {
liucong's avatar
liucong committed
117
118
        LOG_ERROR(stdout, "fail to initialize detector!\n");
        exit(-1);
Your Name's avatar
Your Name committed
119
    }
liucong's avatar
liucong committed
120
121
    LOG_INFO(stdout, "succeed to initialize detector\n");

122
123
124
125
    // 读取测试图像
    std::vector<cv::Mat> srcImages;
    cv::String folder = "../Resource/Images/DynamicPics";
    std::vector<cv::String> imagePathList;
liucong's avatar
liucong committed
126
127
    cv::glob(folder, imagePathList);
    for(int i = 0; i < imagePathList.size(); ++i)
Your Name's avatar
Your Name committed
128
    {
liucong's avatar
liucong committed
129
130
        cv::Mat srcImage = cv::imread(imagePathList[i], 1);
        srcImages.push_back(srcImage);
131
132
133
134
    }

    // 设置动态推理shape
    std::vector<std::vector<std::size_t>> inputShapes;
liucong's avatar
liucong committed
135
136
    inputShapes.push_back({1, 3, 416, 416});
    inputShapes.push_back({1, 3, 608, 608});
137

liucong's avatar
liucong committed
138
    for(int i = 0; i < srcImages.size(); ++i)
139
140
141
    {
        // 推理
        std::vector<migraphxSamples::ResultOfDetection> predictions;
shizhm's avatar
shizhm committed
142
        detector.Detect(srcImages[i], inputShapes[i], predictions, true);
143
144

        // 获取推理结果
liucong's avatar
liucong committed
145
146
        LOG_INFO(stdout, "========== Detection Image%d Results ==========\n", i);
        for(int j = 0; j < predictions.size(); ++j)
147
        {
liucong's avatar
liucong committed
148
149
            migraphxSamples::ResultOfDetection result = predictions[j];
            cv::rectangle(srcImages[i], result.boundingBox, cv::Scalar(0, 255, 255), 2);
150
151

            std::string label = cv::format("%.2f", result.confidence);
liucong's avatar
liucong committed
152
153
154
            label             = result.className + " " + label;
            int left          = predictions[j].boundingBox.x;
            int top           = predictions[j].boundingBox.y;
155
            int baseLine;
liucong's avatar
liucong committed
156
157
            cv::Size labelSize =
                cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
158
            top = max(top, labelSize.height);
liucong's avatar
liucong committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            cv::putText(srcImages[i],
                        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[j].boundingBox.x,
                     predictions[j].boundingBox.y,
                     predictions[j].boundingBox.width,
                     predictions[j].boundingBox.height,
                     predictions[j].classID,
                     predictions[j].confidence);
175
176
177
        }
        std::string imgName = cv::format("Result%d.jpg", i);
        cv::imwrite(imgName, srcImages[i]);
liucong's avatar
liucong committed
178
        LOG_INFO(stdout, "Detection results have been saved to ./Result%d.jpg\n", i);
Your Name's avatar
Your Name committed
179
180
    }
}