main.cpp 2.18 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 <YOLOV3.h>
#include <SimpleLog.h>
#include <Filesystem.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
    // 创建YOLOV3检测器
    migraphxSamples::DetectorYOLOV3 detector;
    migraphxSamples::InitializationParameterOfDetector initParamOfDetectorYOLOV3;
    initParamOfDetectorYOLOV3.configFilePath = CONFIG_FILE;
    migraphxSamples::ErrorCode errorCode=detector.Initialize(initParamOfDetectorYOLOV3);
    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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    LOG_INFO(stdout, "succeed to initialize detector\n");

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

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

    // 获取推理结果
    LOG_INFO(stdout,"========== Detection Results ==========\n");
    for(int i=0;i<predictions.size();++i)
Your Name's avatar
Your Name committed
36
    {
liucong's avatar
liucong committed
37
38
39
        migraphxSamples::ResultOfDetection result=predictions[i];
        cv::rectangle(srcImage,result.boundingBox,cv::Scalar(0,255,255),2);

shizhm's avatar
shizhm committed
40
41
42
43
44
45
46
47
48
        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;
	    cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
	    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);

liucong's avatar
liucong committed
49
50
        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);
Your Name's avatar
Your Name committed
51
    }
liucong's avatar
liucong committed
52
53
54
    cv::imwrite("Result.jpg",srcImage);
    LOG_INFO(stdout,"Detection results have been saved to ./Result.jpg\n");
    
Your Name's avatar
Your Name committed
55
56
    return 0;
}