opencv4.1.0+darknet安装配置

opencv4.1.0+darknet安装配置

之前由于各种原因没有安装好opencv,无法配置darknet。今天各种google终于解决了问题,遂小记一手

opencv安装

在官网上下载opencv的发行版,这里以4.1.0为例
首先安装依赖项

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
下载安装包

```cpp
git clone https://github.com/opencv/opencv.git
cd opencv 
mkdir build
cd build

接下来就是编译,这时坑来了,一定注意camke的选项,按照以下代码运行

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=ON ..

最后一项会生成opencv.pc,很关键
然后

make -j6
sudo make install 

此时opencv已经安装完成,在/usr/local/lib/pkgconfig文件夹下会生成opencv4.pc文件,将其克隆到/usr/lib/pkgconfig

cp /usr/local/lib/pkgconfig/opencv4.pc /usr/lib/pkgconfig
//重命名为opencv.pc
mv opencv4.pc opencv.pc

opencv的配置就完成了

darknet安装

yolo官网下载darknet

git clone https://github.com/pjreddie/darknet
cd darknet

打开makefile,修改前几行为

GPU=1
CUDNN=1//如果已经安装
OPENCV=1

然后make,如果之前opencv.pc没有配置好的话,会出现‘fatal error: opencv2/opencv.hpp: 没有那个文件或目录‘的错误。此时会出现
./src/image_opencv.cpp:12:1: error: ‘IplImage’ does not name a type; did you mean ‘image’?
各方google后终于找到解决方案

cd darknet/src

在src文件夹下查找错误里的image_opencv.cpp,修改以下两个函数,注释里的是原始代码,确保你的文件里的这两个函数函数和以下一致

Mat image_to_mat(image im)
{
    image copy = copy_image(im);
    constrain_image(copy);
    if(im.c == 3) rgbgr_image(copy);

    Mat m(cv::Size(im.w,im.h), CV_8UC(im.c));
    int x,y,c;

    int step = m.step;
    for(y = 0; y < im.h; ++y){
        for(x = 0; x < im.w; ++x){
            for(c= 0; c < im.c; ++c){
                float val = im.data[c*im.h*im.w + y*im.w + x];
                m.data[y*step + x*im.c + c] = (unsigned char)(val*255);
            }
        }
    }

    free_image(copy);
    return m;

// free_image(copy);
// return m;
//     IplImage *ipl = image_to_ipl(copy);
//     Mat m = cvarrToMat(ipl, true);
//     cvReleaseImage(&ipl);
//     free_image(copy);
//     return m;
}

image mat_to_image(Mat m)
{
    int h = m.rows;
    int w = m.cols;
    int c = m.channels();
    image im = make_image(w, h, c);
    unsigned char *data = (unsigned char *)m.data;
    int step = m.step;
    int i, j, k;

    for(i = 0; i < h; ++i){
        for(k= 0; k < c; ++k){
            for(j = 0; j < w; ++j){
                im.data[k*w*h + i*w + j] = data[i*step + j*c + k]/255.;
            }
        }
    }
    rgbgr_image(im);
    return im;
    // IplImage ipl = m;
    // image im = ipl_to_image(&ipl);
    // rgbgr_image(im);
    // return im;
}

同时删除所有含有IplImage的函数,保存,重新make,会出现
error: ‘CV_CAP_PROP_FRAME_WIDTH’ was not declared in this scope
这是由于opencv的版本问题,现在已经没有CV_的前缀了,所以将image_opencv.cpp里所有大写的,含有CV_前缀的变量的前缀删掉,如上面的变量修改后为CAP_PROP_FRAME_WIDTH。保存,重新make,成功!
下面附上我的image_opencv.cpp文件全部代码,如果觉得修改麻烦可以直接复制

#ifdef OPENCV

#include "stdio.h"
#include "stdlib.h"
#include "opencv2/opencv.hpp"
#include "image.h"

using namespace cv;

extern "C" {

Mat image_to_mat(image im)
{
    image copy = copy_image(im);
    constrain_image(copy);
    if(im.c == 3) rgbgr_image(copy);

    Mat m(cv::Size(im.w,im.h), CV_8UC(im.c));
    int x,y,c;

    int step = m.step;
    for(y = 0; y < im.h; ++y){
        for(x = 0; x < im.w; ++x){
            for(c= 0; c < im.c; ++c){
                float val = im.data[c*im.h*im.w + y*im.w + x];
                m.data[y*step + x*im.c + c] = (unsigned char)(val*255);
            }
        }
    }

    free_image(copy);
    return m;
}

image mat_to_image(Mat m)
{
    int h = m.rows;
    int w = m.cols;
    int c = m.channels();
    image im = make_image(w, h, c);
    unsigned char *data = (unsigned char *)m.data;
    int step = m.step;
    int i, j, k;

    for(i = 0; i < h; ++i){
        for(k= 0; k < c; ++k){
            for(j = 0; j < w; ++j){
                im.data[k*w*h + i*w + j] = data[i*step + j*c + k]/255.;
            }
        }
    }
    rgbgr_image(im);
    return im;
}

void *open_video_stream(const char *f, int c, int w, int h, int fps)
{
    VideoCapture *cap;
    if(f) cap = new VideoCapture(f);
    else cap = new VideoCapture(c);
    if(!cap->isOpened()) return 0;
    if(w) cap->set(CAP_PROP_FRAME_WIDTH, w);
    if(h) cap->set(CAP_PROP_FRAME_HEIGHT, w);
    if(fps) cap->set(CAP_PROP_FPS, w);
    return (void *) cap;
}

image get_image_from_stream(void *p)
{
    VideoCapture *cap = (VideoCapture *)p;
    Mat m;
    *cap >> m;
    if(m.empty()) return make_empty_image(0,0,0);
    return mat_to_image(m);
}

image load_image_cv(char *filename, int channels)
{
    int flag = -1;
    if (channels == 0) flag = -1;
    else if (channels == 1) flag = 0;
    else if (channels == 3) flag = 1;
    else {
        fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
    }
    Mat m;
    m = imread(filename, flag);
    if(!m.data){
        fprintf(stderr, "Cannot load image \"%s\"\n", filename);
        char buff[256];
        sprintf(buff, "echo %s >> bad.list", filename);
        system(buff);
        return make_image(10,10,3);
        //exit(0);
    }
    image im = mat_to_image(m);
    return im;
}

int show_image_cv(image im, const char* name, int ms)
{
    Mat m = image_to_mat(im);
    imshow(name, m);
    int c = waitKey(ms);
    if (c != -1) c = c%256;
    return c;
}

void make_window(char *name, int w, int h, int fullscreen)
{
    namedWindow(name, WINDOW_NORMAL); 
    if (fullscreen) {
        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
    } else {
        resizeWindow(name, w, h);
        if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
    }
}

}

#endif

大功告成!
使用以下命令测试yolo,前提你已经在官网下载了yolov3.weight放在darknet文件夹中

wget https://pjreddie.com/media/files/yolov3.weights

打开cfg文件夹下的yolov3.cfg,修改前几行,如下

# Testing
batch=1
subdivisions=1
# Training
#batch=64
#subdivisions=16

保存退出,运行

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

摄像头实时检测

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights -c 2

使用-c来设置使用的摄像头的编号
enjoy it!

你可能感兴趣的:(opencv4.1.0+darknet安装配置)