C++调用编译好的darknet来进行物体监测

前言

darknet是一个基于C写出的框架,yolov3速度快,定位准确率比较高,很适合落地到自己的工程中。那么如何调用编译好的darknet.so动态链接库呢?

正文

首先需要去darknet官网下载好darknet-master,然后进行编译,编译的时候最好选择GPU格式的,这样在后面监测的时候速度会非常快。

好啦,直接上代码吧~代码主要包含两个头文件和两个源文件。

imprcoess.h

#ifndef IMPROCESS_H
#define IMPROCESS_H

#include

void imgConvert(const cv::Mat& img, float* dst);

void imgResize(float* src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);

void resizeInner(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);

#endif // IMPROCESS_H

imprcoess.cpp

#include

void imgConvert(const cv::Mat& img, float* dst){
    uchar *data = img.data;
    int h = img.rows;
    int w = img.cols;
    int c = img.channels();

    for(int k= 0; k < c; ++k){
        for(int i = 0; i < h; ++i){
            for(int j = 0; j < w; ++j){
                dst[k*w*h+i*w+j] = data[(i*w + j)*c + k]/255.;
            }
        }
    }
}

void imgResize(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight){
    int new_w = srcWidth;
    int new_h = srcHeight;
    if (((float)dstWidth/srcWidth) < ((float)dstHeight/srcHeight)) {
        new_w = dstWidth;
        new_h = (srcHeight * dstWidth)/srcWidth;
    } else {
        new_h = dstHeight;
        new_w = (srcWidth * dstHeight)/srcHeight;
    }

    float* ImgReInner;
    size_t sizeInner=new_w*new_h*3*sizeof(float);
    ImgReInner=(float*)malloc(sizeInner);
    resizeInner(src,ImgReInner,srcWidth,srcHeight,new_w,new_h);

    for(int i=0;i

darknet.h就不再列出啦,有头文件的,直接copy到自己工程下就ok啦~~~

main.cpp,这里是使用opencv去调用usb摄像头来进行物体检测,模型是darknet官网上的模型,当然你可以换成自己训练的模型。

#include
#include
#include
#include

using namespace std;
using namespace cv;

float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };

float get_color(int c, int x, int max)
{
    float ratio = ((float)x/max)*5;
    int i = floor(ratio);
    int j = ceil(ratio);
    ratio -= i;
    float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
    return r;
}


int main()
{
    string cfgfile = "/home/oliver/darknet-master/cfg/yolov3.cfg";//读取模型文件,请自行修改相应路径
    string weightfile = "/home/oliver/darknet-master/yolov3.weights";
    float thresh=0.5;//参数设置
    float nms=0.35;
    int classes=80;

    network *net=load_network((char*)cfgfile.c_str(),(char*)weightfile.c_str(),0);//加载网络模型
    set_batch_network(net, 1);
    VideoCapture capture(0);//读取视频,请自行修改相应路径
    capture.set(CV_CAP_PROP_FRAME_WIDTH,1920);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT,1080);
    Mat frame;
    Mat rgbImg;

    vector classNamesVec;
    ifstream classNamesFile("/home/oliver/darknet-master/data/coco.names");//标签文件coco有80类

    if (classNamesFile.is_open()){
        string className = "";
        while (getline(classNamesFile, className))
            classNamesVec.push_back(className);
    }

    bool stop=false;
    while(!stop)
    {
        cout<w*net->h*3*sizeof(float);
        resizeImg=(float*)malloc(resizeSize);
        imgResize(srcImg,resizeImg,frame.cols,frame.rows,net->w,net->h);//缩放图像

        network_predict(net,resizeImg);//网络推理
        int nboxes=0;
        detection *dets=get_network_boxes(net,rgbImg.cols,rgbImg.rows,thresh,0.5,0,1,&nboxes);

        if(nms){
            do_nms_sort(dets,nboxes,classes,nms);
        }

        vectorboxes;
        boxes.clear();
        vectorclassNames;

        for (int i = 0; i < nboxes; i++){
            bool flag=0;
            int className;
            for(int j=0;jthresh){
                    if(!flag){
                        flag=1;
                        className=j;
                    }
                }
            }
            if(flag)
            {
                int left = (dets[i].bbox.x - dets[i].bbox.w / 2.)*frame.cols;
                int right = (dets[i].bbox.x + dets[i].bbox.w / 2.)*frame.cols;
                int top = (dets[i].bbox.y - dets[i].bbox.h / 2.)*frame.rows;
                int bot = (dets[i].bbox.y + dets[i].bbox.h / 2.)*frame.rows;

                if (left < 0)
                    left = 0;
                if (right > frame.cols - 1)
                    right = frame.cols - 1;
                if (top < 0)
                    top = 0;
                if (bot > frame.rows - 1)
                    bot = frame.rows - 1;

                Rect box(left, top, fabs(left - right), fabs(top - bot));
                boxes.push_back(box);
                classNames.push_back(className);
            }
        }
        free_detections(dets, nboxes);

        for(int i=0;i=0)
                  waitKey(0);

        free(srcImg);
        free(resizeImg);
    }
    free_network(net);
    capture.release();
    return 1;
}

QT下库的配置:

QT += core
QT -= gui

CONFIG += c++11

TARGET = yolov3_master
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    improcess.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

HEADERS += \
    darknet.h \
    improcess.h

INCLUDEPATH += /usr/local/include\
/usr/local/include/opencv\
/usr/local/include/opencv2

LIBS +=/usr/local/lib/libopencv_*.so\
/home/oliver/darknet-master/libdarknet.so\
/usr/local/cuda-9.0/lib64/libcudart.so.9.0\
/usr/local/cuda-9.0/lib64/libcudnn.so.7\
/usr/local/cuda-9.0/lib64/libcurand.so.9.0\
/usr/local/cuda-9.0/lib64/libcublas.so.9.0

实验结果

你可能感兴趣的:(C++调用编译好的darknet来进行物体监测)