Ubuntu下调用YOLOv4

Ubuntu下调用YOLOv4

    • 1.下载代码:
    • 2.解压缩:
    • 3.修改Makefile文件,如下图所示,找比往常最主要的区别就是修改LIBSO = 1:
    • 4.Make,结束以后会出现libdarknet.so文件,如下图所示:
    • 5.创建工程,修改工程内cmakelist.txt,修改下方标注区域为对应的位置,分别是工程名、opencv路径以及yolov4路径。
    • 6.C++代码
    • 7.运行效果

之前有写过windows下调用YOLOv4,最近老师要求让在ubuntu下调用起来,毕竟还是ubuntu开发用的多一些,找了些资料,先总结一下,总体上来说和windows调用差别不是很大。最后还是希望opencv早日更新集成yolov4。

1.下载代码:

https://github.com/AlexeyAB/darknet

2.解压缩:

3.修改Makefile文件,如下图所示,找比往常最主要的区别就是修改LIBSO = 1:

Ubuntu下调用YOLOv4_第1张图片

4.Make,结束以后会出现libdarknet.so文件,如下图所示:

Ubuntu下调用YOLOv4_第2张图片

5.创建工程,修改工程内cmakelist.txt,修改下方标注区域为对应的位置,分别是工程名、opencv路径以及yolov4路径。

cmake_minimum_required(VERSION 2.8)
project(untitled4)
#opencv
add_definitions(-std=c++11)
ADD_DEFINITIONS(-DOPENCV)
ADD_DEFINITIONS(-DGPU)
#########         opencv   #########
set(OpenCV_DIR "/home/sun/opencv3/release")
find_package( OpenCV REQUIRED )
include_directories( ${
     OpenCV_INCLUDE_DIRS} )
#########         darknet   #########
include_directories(/home/sun/qt_learning/15/darknet-master/include/)
find_library(darknet libdarknet.so /home/sun/qt_learning/15/darknet-master/)
add_executable(${
     PROJECT_NAME} "main.cpp" )
target_link_libraries(${
     PROJECT_NAME} ${
     OpenCV_LIBS}  ${
     darknet})

6.C++代码

#include 
#include "yolo_v2_class.hpp" //引用动态链接库中的头文件
#include 
#include "opencv2/highgui/highgui.hpp"
#include 
#pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库
//#pragma comment(lib, "opencv_world340d.lib") //引入OpenCV链接库,我在vs中配置过了,如果没配置请对应修改
using namespace cv;
//以下两段代码来自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 = "voc.names";
    std::string cfg_file = "yolov4.cfg";
    std::string weights_file = "yolov4.weights";
    Detector detector(cfg_file, weights_file,0); //初始化检测器
    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::Mat frame = imread("test.jpg");
    std::cout << detector.cur_gpu_id<< std::endl;
    std::vector<bbox_t> result_vec = detector.detect(frame);
    draw_boxes(frame, result_vec, obj_names);
    cv::imshow("test", frame);
    waitKey();
    return 0;
}

7.运行效果

Ubuntu下调用YOLOv4_第3张图片

你可能感兴趣的:(YOLO学习,YOLO,c++)