Sample.cpp 2.24 KB
Newer Older
Your Name's avatar
Your Name committed
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
#include <Sample.h>
#include <opencv2/dnn.hpp>
#include <SimpleLog.h>
#include <Filesystem.h>
#include <DetectorYOLOV5.h>
#include <fstream>

using namespace std;
using namespace cv;
using namespace cv::dnn;
using namespace migraphx;
using namespace migraphxSamples;

void Sample_DetectorYOLOV5()
{
    // 创建YOLOV5检测器
    DetectorYOLOV5 detector;
    InitializationParameterOfDetector initParamOfDetectorYOLOV5;
    initParamOfDetectorYOLOV5.parentPath = "";
    initParamOfDetectorYOLOV5.configFilePath = CONFIG_FILE;
    initParamOfDetectorYOLOV5.logName = "";
    ErrorCode errorCode=detector.Initialize(initParamOfDetectorYOLOV5);
    if(errorCode!=SUCCESS)
    {
        LOG_ERROR(stdout, "fail to initialize detector!\n");
        exit(-1);
    }
    LOG_INFO(stdout, "succeed to initialize detector\n");

    // 读取测试图片
    Mat srcImage=imread("../Resource/Images/bus.jpg",1);

    // 推理
    std::vector<ResultOfDetection> predictions;
    double time1 = getTickCount();
    detector.Detect(srcImage,predictions);
    double time2 = getTickCount();
    double elapsedTime = (time2 - time1)*1000 / getTickFrequency();
    LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);

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

        string label = format("%.2f", result.confidence);
        label = result.className + " " + label;
        int left = predictions[i].boundingBox.x;
        int top = predictions[i].boundingBox.y;
        int baseLine;
	    Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	    top = max(top, labelSize.height);
        putText(srcImage, label, Point(left, top-10), FONT_HERSHEY_SIMPLEX, 1, 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);
    }
    imwrite("Result.jpg",srcImage);
    LOG_INFO(stdout,"Detection results have been saved to ./Result.jpg\n");
}