c++项目中使用YOLOv4模型简单案例

主要是使用yolo_v2_class.hpp文件

1、hpp文件

#ifndef DEMO_HPP
#define DEMO_HPP
#ifndef OPENCV
#define OPENCV
#endif
#include
#include
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> detect(Mat imag);
    ~yoloDetector();


private:
    vector> bbox_to_vector(vector booxs);
    vector 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> yoloDetector::detect(Mat imag)
 {
     vector boxs= dector->detect(imag,thresh);
     if(boxs.size()>0)
     {
         return bbox_to_vector(boxs);
     }
 }
 vector> yoloDetector::bbox_to_vector(vector booxs)
 {
     vector> im;
     for (vector::iterator iter = booxs.begin(); iter != booxs.end(); iter++)
         {
            if((*iter).prob < prob)
                continue;
             map 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

2、main文件

#include
#include
 Mat huatu(Mat img,vector> boox,string classname[])
 {
     for(vector>::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 "<> boox;
     if(type=="img")
     {
         Mat img = cv::imread(path);
 Mat img2;
         if(img.data==nullptr)
         {
             cout<<"path:"<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"<>fram;
                
		//resize(fram,img,Size(640.0,480.0));
		//imshow("demo",fram);

		if(fram.data!=nullptr)
         {
             boox = yd.detect(fram);
         }
		cout<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"<>fram;
                
		//resize(fram,img,Size(640.0,480.0));
		//imshow("demo",fram);

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

            waitKey(deply);
             
         }
     }
     return 0;

 }

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"
                     
                     )

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

你可能感兴趣的:(C/C++相关学习,机器学习)