当前位置:   article > 正文

c++项目中使用YOLOv4模型简单案例_#include "yolo_v2_class.hpp

#include "yolo_v2_class.hpp

主要是使用yolo_v2_class.hpp文件

1、hpp文件
#ifndef DEMO_HPP
#define DEMO_HPP
#ifndef OPENCV
#define OPENCV
#endif
#include<yolo_v2_class.hpp>
#include<darknet.h>
using namespace cv;
using namespace std;
class yoloDetector
{
public:
    yoloDetector(string cfgfile,string weightfile,float thresh = 0.2, bool use_mean = false,int gpu_id = 0);
    vector<map<string,float>> detect(Mat imag);
    ~yoloDetector();


private:
    vector<map<string,float>> bbox_to_vector(vector<bbox_t> booxs);
    vector<bbox_t> detectionObjetc;
    float prob;
    float thresh;
    bool use_mean;
    Detector* dector;



};
yoloDetector::yoloDetector(string cfgfile,string weightfile,float thresh, bool use_mean,int gpu_id ):
    prob(prob),thresh(thresh),use_mean(use_mean)
{
    dector = new Detector(cfgfile,weightfile,gpu_id);
}

 vector<map<string,float>> yoloDetector::detect(Mat imag)
 {
     vector<bbox_t> boxs= dector->detect(imag,thresh);
     if(boxs.size()>0)
     {
         return bbox_to_vector(boxs);
     }
 }
 vector<map<string,float>> yoloDetector::bbox_to_vector(vector<bbox_t> booxs)
 {
     vector<map<string,float>> im;
     for (vector<bbox_t>::iterator iter = booxs.begin(); iter != booxs.end(); iter++)
         {
            if((*iter).prob < prob)
                continue;
             map<string, float> ma;
             ma["x"]=(*iter).x;
             ma["y"]=(*iter).y;
             ma["w"]=(*iter).w;
             ma["h"]=(*iter).h;
             ma["prob"]=(*iter).prob;
             ma["obj_id"]=(*iter).obj_id;
             ma["frames_counter"]=(*iter).frames_counter;
             ma["x_3d"]=(*iter).x_3d;
             ma["y_3d"]=(*iter).y_3d;
             ma["z_3d"]=(*iter).z_3d;
             im.push_back(ma);

         }
     return im;

 }
 yoloDetector::~yoloDetector()
 {
     delete(dector);
 }

#endif // DEMO_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
2、main文件
#include<demo.hpp>
#include<iostream>
 Mat huatu(Mat img,vector<map<string,float>> boox,string classname[])
 {
     for(vector<map<string,float>>::iterator iter=boox.begin();iter != boox.end(); iter++)
     {
         float x = (*iter).at("x");
         float y = (*iter).at("y");
         float w = (*iter).at("w");
         float h = (*iter).at("h");
         float prob = (*iter).at("prob");
         float obj_id = (*iter).at("obj_id");
         if(prob>0.8)
         {
             Rect rect(x, y,w, h);
             cv::rectangle(img,rect, cv::Scalar(255, 0, 0), 1,LINE_8,0);
             string pr = to_string(prob);
             string labe=classname[(int)obj_id] + ' ' + pr;
             cv::putText(img,labe ,cv::Point(x, y - 13),cv::FONT_HERSHEY_SIMPLEX,0.5,cv::Scalar(0, 255, 0),2);
         }


     }
     //cv::imshow("MyWindow", img);
     //waitKey(0);
     return img;
 }

int main(int argc, char const *argv[])
 {
     if(argc<5)
     {
         cout<<"input cfg/weight/(igm/video/cam)/path "<<endl;
	// ../yolov4Dect ../cfg/yolov4-head-test.cfg ../backup/yolov4-head_best.weights  cam o

     }
    string  classname[]={"head"};
     string cfgfile = argv[1];
     string weightfile = argv[2];
     string type=argv[3];
     string path=argv[4];
     yoloDetector yd(cfgfile,weightfile);
     vector<map<string,float>> boox;
     if(type=="img")
     {
         Mat img = cv::imread(path);
 Mat img2;
         if(img.data==nullptr)
         {
             cout<<"path:"<<path<<"error"<<endl;
             return -1;
         }else
         {
             boox = yd.detect(img);
             if(boox.size()>0)
             {
                
                img2 = huatu(img,boox,classname);
             }
	    imshow("demo",img2);
             waitKey(0);
         }
     }
     else if(type=="video")
     {
        VideoCapture capture;
	capture.open(path);
	double rate = capture.get(CV_CAP_PROP_FPS);
	int deply = cvRound(1000.000/rate);
	if(!capture.isOpened())
	{
		cout<<"no opend"<<endl;
		return -1;
	}
	
	
         
         while(1)
         {
	   Mat fram;
            
	    capture>>fram;
                
		//resize(fram,img,Size(640.0,480.0));
		//imshow("demo",fram);

		if(fram.data!=nullptr)
         {
             boox = yd.detect(fram);
         }
		cout<<boox.size()<<endl;
		Mat img;
		     if(boox.size()>0)
		     {
			
		         //Mat img2=fram.clone();
		        img=huatu(fram,boox,classname);
			imshow("demo",img);
		     }
            waitKey(deply);
             
         }
     }
    else if(type=="cam")
     {
        VideoCapture capture(0);
	double rate = capture.get(CV_CAP_PROP_FPS);
	int deply = cvRound(1000.000/rate);
	if(!capture.isOpened())
	{
		cout<<"no opend"<<endl;
		return -1;
	}
	
	
         
         while(1)
         {
	   Mat fram;
            
	    capture>>fram;
                
		//resize(fram,img,Size(640.0,480.0));
		//imshow("demo",fram);

		if(fram.data!=nullptr)
         {
             boox = yd.detect(fram);
         }
		cout<<boox.size()<<endl;
		Mat img;
		     if(boox.size()>0)
		     {
			
		         //Mat img2=fram.clone();
		        img=huatu(fram,boox,classname);
			imshow("demo",img);
		     }
                  

            waitKey(deply);
             
         }
     }
     return 0;

 }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
3、CMakeLists
cmake_minimum_required(VERSION 2.8.3)
project(yolov4Dect)
set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV)

set(SOURCE_FILES demo3.cpp demo.hpp)

add_executable(yolov4Dect ${SOURCE_FILES})

include_directories(${OpenCV_INCLUDE_DIRS} "./" "/usr/include" "/headless/docker_mapping/YOLOv4/darknet/include/")

link_directories("/headless/docker_mapping/YOLOv4/darknet/" "/usr/local/cuda-10.1/lib64")

target_link_libraries(yolov4Dect  ${OpenCV_LIBS}
                     
                      "/headless/docker_mapping/YOLOv4/darknet/libdarknet.so"
                     
                     )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

简单案例写的比较粗糙,回头有时间在进一步完善

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/390841?site
推荐阅读
相关标签
  

闽ICP备14008679号