YOLO:darknet model 推理 备忘

4 篇文章 0 订阅
3 篇文章 0 订阅
#include <iostream>
#include <string>

#ifdef linux
#define OPENCV
#define GPU
#endif

#include "yolo_v2_class.hpp"	// imported functions from DLL
#include "opencv2/opencv.hpp"	// C++
#include <opencv2/highgui/highgui.hpp>

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);
        }
    }
}

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 = "/home/.../documents/yoloPerson-2021-10-26/person.names";
    std::string  cfg_file = "/home/.../documents/yoloPerson-2021-10-26/yolov4-tiny.cfg";
    std::string  weights_file = "/home/.../documents/yoloPerson-2021-10-26/backup_tiny-2021-10-27-map0.86/yolov4-tiny_best.weights";

    Detector detector(cfg_file, weights_file);//初始化检测器
    auto obj_names = objects_names_from_file(names_file);//获得分类对象名称
    cv::VideoCapture capture;
    //std::string videoPath="/home/.../images/drums02.mp4";
    std::string videoPath="/home/.../extra/images/0001.mp4";
    capture.open(videoPath);
    if (!capture.isOpened())
    {
        printf("文件打开失败");
    }
    cv::Mat frame;
    while(1)
    {
        capture>> frame;
        std::vector<bbox_t> filter_result(0);
//        cv::resize(frame,frame,cv::Size(416,416),cv::INTER_LINEAR);
        std::vector<bbox_t> result_vec = detector.detect(frame,0.25);
        for(size_t i =0;i<result_vec.size();i++)
        {
            std::cout<<"The width and height of Bounding Box: "<<result_vec[i].w<<" "<<result_vec[i].h<<std::endl;
            if (result_vec[i].w*result_vec[i].h>8000 && 2.0<=result_vec[i].h/result_vec[i].w<=3.20)
            {
                filter_result.push_back(result_vec[i]);
            }
        }
        draw_boxes(frame, filter_result, obj_names);
        cv::imshow("Person Detector", frame);
        if (cv::waitKey(30)==27)
        {
            break;
        }

    }
    return 0;
}

CmakeLists:

cmake_minimum_required(VERSION 3.0)
project(personDetectorTest)

set(CMAKE_CXX_STANDARD 11)
set(OpenCV_DIR /home/.../documents/opencv3.4.12/opencv-3.4.12/build)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS}
        /home/.../darknet-4-master/include
        )
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}
        /home/.../darknet-4-master/libdarknet.so)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值