Opencv3.4.2调用yolov2进行物体检测源代码

源代码

#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include <iostream>
#include<string>

using namespace std;
using namespace cv;
using namespace dnn;


int main()
{
    String modelConfiguration = "/home/oliver/darknet-master/cfg/yolov2.cfg";
    String modelBinary = "/home/oliver/darknet-master/yolov2.weights";
    dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
    if (net.empty())
    {
        printf("Could not load net...\n");
        return 0;
    }
    vector<string> classNamesVec;
    ifstream classNamesFile("/home/oliver/darknet-master/data/coco.names");
    if (classNamesFile.is_open())
    {
        string className = "";
        while (std::getline(classNamesFile, className))
            classNamesVec.push_back(className);
    }

    // 加载图像
    VideoCapture capture(2);// VideoCapture:OENCV中新增的类,捕获视频并显示出来
    while (1)
    {
    Mat frame;
    capture >> frame;
    Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(608, 608), Scalar(), true, false);
    net.setInput(inputBlob, "data");

    // 检测
    Mat detectionMat = net.forward("detection_out");
    vector<double> layersTimings;
    double freq = getTickFrequency() / 1000;
    double time = net.getPerfProfile(layersTimings) / freq;
    ostringstream ss;
    ss << "detection time: " << time << " ms";
    putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(0, 0, 255));
    // 输出结果
    for (int i = 0; i < detectionMat.rows; i++)
    {
        const int probability_index = 5;
        const int probability_size = detectionMat.cols - probability_index;
        float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);
        size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
        float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);
        if (confidence > 0.5)
        {
            float x = detectionMat.at<float>(i, 0);
            float y = detectionMat.at<float>(i, 1);
            float width = detectionMat.at<float>(i, 2);
            float height = detectionMat.at<float>(i, 3);
            int xLeftBottom = static_cast<int>((x - width / 2) * frame.cols);
            int yLeftBottom = static_cast<int>((y - height / 2) * frame.rows);
            int xRightTop = static_cast<int>((x + width / 2) * frame.cols);
            int yRightTop = static_cast<int>((y + height / 2) * frame.rows);
            Rect object(xLeftBottom, yLeftBottom,
                xRightTop - xLeftBottom,
                yRightTop - yLeftBottom);
            rectangle(frame, object, Scalar(0, 0, 255), 2, 8);
            if (objectClass < classNamesVec.size())
            {
                ss.str("");
                ss << confidence;
                String conf(ss.str());
                String label = String(classNamesVec[objectClass]) + ": " + conf;
                int baseLine = 0;
                Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
                rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom),
                    Size(labelSize.width, labelSize.height + baseLine)),
                    Scalar(255, 255, 255), CV_FILLED);
                putText(frame, label, Point(xLeftBottom, yLeftBottom + labelSize.height),
                    FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
            }
        }
    }
    cv::namedWindow("YOLO-Detections",0);
    cv::imshow("YOLO-Detections", frame);
    waitKey(30);
    }
    return 0;
}

实验结果

在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值