main.cpp 2.89 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

liucong's avatar
liucong committed
8
int main()
Your Name's avatar
Your Name committed
9
{
liucong's avatar
liucong committed
10
11
12
13
14
15
    // 创建YOLOV5检测器
    migraphxSamples::DetectorYOLOV5 detector;
    migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOV5;
    initParamOfDetectorYOLOV5.configFilePath = CONFIG_FILE;
    migraphxSamples::ErrorCode errorCode=detector.Initialize(initParamOfDetectorYOLOV5);
    if(errorCode!=migraphxSamples::SUCCESS)
Your Name's avatar
Your Name committed
16
    {
liucong's avatar
liucong committed
17
18
        LOG_ERROR(stdout, "fail to initialize detector!\n");
        exit(-1);
Your Name's avatar
Your Name committed
19
    }
liucong's avatar
liucong committed
20
21
    LOG_INFO(stdout, "succeed to initialize detector\n");

22
23
24
25
26
27
    // 读取测试图像
    std::vector<cv::Mat> srcImages;
    cv::String folder = "../Resource/Images/DynamicPics";
    std::vector<cv::String> imagePathList;
    cv::glob(folder,imagePathList);
    for (int i = 0; i < imagePathList.size(); ++i)
Your Name's avatar
Your Name committed
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
        cv:: Mat srcImage=cv::imread(imagePathList[i], 1);
        srcImages.push_back(srcImage); 
    }

    // 设置动态推理shape
    std::vector<std::vector<std::size_t>> inputShapes;
    inputShapes.push_back({1,3,416,416});
    inputShapes.push_back({1,3,608,608});

    for (int i = 0; i < srcImages.size(); ++i)
    {
        // 推理
        std::vector<migraphxSamples::ResultOfDetection> predictions;
        double time1 = cv::getTickCount();
        detector.Detect(srcImages[i], inputShapes[i], predictions);
        double time2 = cv::getTickCount();
        double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
        LOG_INFO(stdout, "inference image%d time:%f ms\n", i, elapsedTime);

        // 获取推理结果
        LOG_INFO(stdout,"========== Detection Image%d Results ==========\n", i);
        for(int j=0;j<predictions.size();++j)
        {
            migraphxSamples::ResultOfDetection result=predictions[j];
            cv::rectangle(srcImages[i],result.boundingBox,cv::Scalar(0,255,255),2);

            std::string label = cv::format("%.2f", result.confidence);
            label = result.className + " " + label;
            int left = predictions[j].boundingBox.x;
            int top = predictions[j].boundingBox.y;
            int baseLine;
            cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
            top = max(top, labelSize.height);
            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);
        }
        std::string imgName = cv::format("Result%d.jpg", i);
        cv::imwrite(imgName, srcImages[i]);
        LOG_INFO(stdout,"Detection results have been saved to ./Result%d.jpg\n", i);
Your Name's avatar
Your Name committed
70
    }
liucong's avatar
liucong committed
71

Your Name's avatar
Your Name committed
72
73
    return 0;
}