win10在自己工程里使用YOLOv3

环境:win10+vs2015

之前是搭建过YOLOv2,现在是准备在自己项目里使用YOLOv3。

在GitHub上的大神写的很全面,之前已经安装过vs2015 cuda 和 cuddn,所以YOLOv3也很顺利。

https://github.com/AlexeyAB/darknet#how-to-use-yolo-as-dll-and-so-libraries

接下来就是怎么样在自己项目中使用:

1.安装上面链接的内容,编译build\darknet\yolo_cpp_dll.sln,会生成许多新的文件:

这里主要用到的是dll和lib文件。

2.在vs里建立一个新的空工程,新建一个main.cpp,准备以下文件与main文件放在同一路径下。

(1)这几个文件是可以在.\darknet-master\build\darknet\x64下找到,注意要包括OpenCV的两个dll。

(2)刚才生成的dll和lib文件。

(3)cfg文件、weights文件及coco.name文件。

cfg文件在.\darknet-master\build\darknet\x64\cfg目录下,我用的是yolov3.cfg;

weights文件需要在官网上下载,上面链接里有;

coco.name在.\darknet-master\build\darknet\x64\data目录下。

3.main.cpp代码:

参照:https://blog.csdn.net/stjuliet/article/details/87884976

要包括#include <fstream> 头文件,不然进行文件操作部分的代码会报错。

#include <iostream>
#include <fstream> 

#ifdef _WIN32
#define OPENCV
#define GPU
#endif

#include "yolo_v2_class.hpp" //引用动态链接库中的头文件
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"

#pragma comment(lib, "opencv_world310.lib") //引入OpenCV链接库
#pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库

//以下两段代码来自yolo_console_dll.sln
void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
    int current_det_fps = -1, int current_cap_fps = -1)
{
    int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };

    for (auto &i : result_vec) {
        cv::Scalar color = obj_id_to_color(i.obj_id);
        cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), color, 2);
        if (obj_names.size() > i.obj_id) {
            std::string obj_name = obj_names[i.obj_id];
            if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id);
            cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0);
            int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
            cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 30, 0)),
                cv::Point2f(std::min((int)i.x + max_width, mat_img.cols - 1), std::min((int)i.y, mat_img.rows - 1)),
                color, CV_FILLED, 8, 0);
            putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2);
        }
    }
    if (current_det_fps >= 0 && current_cap_fps >= 0) {
        std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) + "   FPS capture: " + std::to_string(current_cap_fps);
        putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2);
    }
}

std::vector<std::string> objects_names_from_file(std::string const filename) {
    std::ifstream file(filename);
    std::vector<std::string> file_lines;
    if (!file.is_open()) return file_lines;
    for (std::string line; getline(file, line);) file_lines.push_back(line);
    std::cout << "object names loaded \n";
    return file_lines;
}

int main()
{
    std::string names_file = "coco.names";
    std::string cfg_file = "yolov3.cfg";
    std::string weights_file = "yolov3.weights";
    Detector detector(cfg_file, weights_file, 0); //初始化检测器
                                                  //std::vector<std::string> obj_names = objects_names_from_file(names_file); //调用获得分类对象名称
                                                  //或者使用以下四行代码也可实现读入分类对象文件
    std::vector<std::string> obj_names;
    std::ifstream ifs(names_file.c_str());
    std::string line;
    while (getline(ifs, line)) obj_names.push_back(line);
    //测试是否成功读入分类对象文件
    for (size_t i = 0; i < obj_names.size(); i++)
    {
        std::cout << obj_names[i] << std::endl;
    }

    cv::VideoCapture capture;
    capture.open("test.mp4");
    if (!capture.isOpened())
    {
        printf("文件打开失败");
    }
    cv::Mat frame;
    while (true)
    {
        capture >> frame;
        std::vector<bbox_t> result_vec = detector.detect(frame);
        draw_boxes(frame, result_vec, obj_names);
        cv::namedWindow("test", CV_WINDOW_NORMAL);
        cv::imshow("test", frame);
        cv::waitKey(3);
    }
    return 0;
}

 

 

由于笔记本性能的原因,最终跑出来的视频为2.5FPS。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值