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

源代码

#include
#include
#include 
#include

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 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 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(i, probability_index);
        size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
        float confidence = detectionMat.at(i, (int)objectClass + probability_index);
        if (confidence > 0.5)
        {
            float x = detectionMat.at(i, 0);
            float y = detectionMat.at(i, 1);
            float width = detectionMat.at(i, 2);
            float height = detectionMat.at(i, 3);
            int xLeftBottom = static_cast((x - width / 2) * frame.cols);
            int yLeftBottom = static_cast((y - height / 2) * frame.rows);
            int xRightTop = static_cast((x + width / 2) * frame.cols);
            int yRightTop = static_cast((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;
}

实验结果

Opencv3.4.2调用yolov2进行物体检测源代码_第1张图片Opencv3.4.2调用yolov2进行物体检测源代码_第2张图片

你可能感兴趣的:(深度学习)