Ubuntu18.04 + Yolov3 + OpenCV

一、YoloV3安装

1.进入官网

根据说明:下载Darknet:

在自己喜欢的位置解压Darknet,进入Darknet目录并编译:

cd darknet
make

等待完成即可:
Ubuntu18.04 + Yolov3 + OpenCV_第1张图片
下载权重文件:wget https://pjreddie.com/media/files/yolov3.weights特别慢
Ubuntu18.04 + Yolov3 + OpenCV_第2张图片
下载完成后,将权重文件yolov3.weights拷贝到Darknet根目录,执行:./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

简单说明:

配置文件路径:cfg/yolov3.cfg

权重文件路径:yolov3.weights

图片问价路径:data/dog.jpg

运行结果:

zhx@zhx:~/Yolo/darknet-master$ ./darknet detect cfg/yolov3.cfg data/yolov3.weights data/dog.jpg
layer     filters    size              input                output
    0 conv     32  3 x 3 / 1   608 x 608 x   3   ->   608 x 608 x  32  0.639 BFLOPs
    1 conv     64  3 x 3 / 2   608 x 608 x  32   ->   304 x 304 x  64  3.407 BFLOPs
    2 conv     32  1 x 1 / 1   304 x 304 x  64   ->   304 x 304 x  32  0.379 BFLOPs
    3 conv     64  3 x 3 / 1   304 x 304 x  32   ->   304 x 304 x  64  3.407 BFLOPs
    4 res    1                 304 x 304 x  64   ->   304 x 304 x  64
    5 conv    128  3 x 3 / 2   304 x 304 x  64   ->   152 x 152 x 128  3.407 BFLOPs
    6 conv     64  1 x 1 / 1   152 x 152 x 128   ->   152 x 152 x  64  0.379 BFLOPs
    7 conv    128  3 x 3 / 1   152 x 152 x  64   ->   152 x 152 x 128  3.407 BFLOPs
    8 res    5                 152 x 152 x 128   ->   152 x 152 x 128
    9 conv     64  1 x 1 / 1   152 x 152 x 128   ->   152 x 152 x  64  0.379 BFLOPs
   10 conv    128  3 x 3 / 1   152 x 152 x  64   ->   152 x 152 x 128  3.407 BFLOPs
   11 res    8                 152 x 152 x 128   ->   152 x 152 x 128
   12 conv    256  3 x 3 / 2   152 x 152 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   13 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   14 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   15 res   12                  76 x  76 x 256   ->    76 x  76 x 256
   16 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   17 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   18 res   15                  76 x  76 x 256   ->    76 x  76 x 256
   19 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   20 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   21 res   18                  76 x  76 x 256   ->    76 x  76 x 256
   22 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   23 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   24 res   21                  76 x  76 x 256   ->    76 x  76 x 256
   25 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   26 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   27 res   24                  76 x  76 x 256   ->    76 x  76 x 256
   28 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   29 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   30 res   27                  76 x  76 x 256   ->    76 x  76 x 256
   31 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   32 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   33 res   30                  76 x  76 x 256   ->    76 x  76 x 256
   34 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
   35 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
   36 res   33                  76 x  76 x 256   ->    76 x  76 x 256
   37 conv    512  3 x 3 / 2    76 x  76 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   38 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   39 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   40 res   37                  38 x  38 x 512   ->    38 x  38 x 512
   41 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   42 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   43 res   40                  38 x  38 x 512   ->    38 x  38 x 512
   44 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   45 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   46 res   43                  38 x  38 x 512   ->    38 x  38 x 512
   47 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   48 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   49 res   46                  38 x  38 x 512   ->    38 x  38 x 512
   50 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   51 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   52 res   49                  38 x  38 x 512   ->    38 x  38 x 512
   53 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   54 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   55 res   52                  38 x  38 x 512   ->    38 x  38 x 512
   56 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   57 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   58 res   55                  38 x  38 x 512   ->    38 x  38 x 512
   59 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   60 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   61 res   58                  38 x  38 x 512   ->    38 x  38 x 512
   62 conv   1024  3 x 3 / 2    38 x  38 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   63 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   64 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   65 res   62                  19 x  19 x1024   ->    19 x  19 x1024
   66 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   67 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   68 res   65                  19 x  19 x1024   ->    19 x  19 x1024
   69 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   70 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   71 res   68                  19 x  19 x1024   ->    19 x  19 x1024
   72 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   73 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   74 res   71                  19 x  19 x1024   ->    19 x  19 x1024
   75 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   76 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   77 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   78 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   79 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512  0.379 BFLOPs
   80 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024  3.407 BFLOPs
   81 conv    255  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 255  0.189 BFLOPs
   82 yolo
   83 route  79
   84 conv    256  1 x 1 / 1    19 x  19 x 512   ->    19 x  19 x 256  0.095 BFLOPs
   85 upsample            2x    19 x  19 x 256   ->    38 x  38 x 256
   86 route  85 61
   87 conv    256  1 x 1 / 1    38 x  38 x 768   ->    38 x  38 x 256  0.568 BFLOPs
   88 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   89 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   90 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   91 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256  0.379 BFLOPs
   92 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512  3.407 BFLOPs
   93 conv    255  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 255  0.377 BFLOPs
   94 yolo
   95 route  91
   96 conv    128  1 x 1 / 1    38 x  38 x 256   ->    38 x  38 x 128  0.095 BFLOPs
   97 upsample            2x    38 x  38 x 128   ->    76 x  76 x 128
   98 route  97 36
   99 conv    128  1 x 1 / 1    76 x  76 x 384   ->    76 x  76 x 128  0.568 BFLOPs
  100 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
  101 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
  102 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
  103 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
  104 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
  105 conv    255  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 255  0.754 BFLOPs
  106 yolo
Loading weights from data/yolov3.weights...Done!
data/dog.jpg: Predicted in 13.315695 seconds.
dog: 100%
truck: 92%
bicycle: 99%
zhx@zhx:~/Yolo/darknet-master$ ./darknet detect cfg/yolov3.cfg data/yolov3.weights data/dog.jpg

二、安装OpenCV

1.安装cmake(编译器)和依赖库

$ sudo apt-get install cmake #如果已经安装过cmake,则该步骤省略
$ sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
$ sudo apt-get install libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev

Ubuntu18.04 + Yolov3 + OpenCV_第3张图片Ubuntu18.04 + Yolov3 + OpenCV_第4张图片
2.添加Python3的支持,在终端执行相关命令即可,此处不在截图

# python3支持
$ sudo apt install python3-dev python3-numpy
# streamer支持
$ sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
# 可选的依赖
$ sudo apt install libpng-dev libopenexr-dev libtiff-dev libwebp-dev

3.下载 OpenCV 源文件Sources,并解压缩

Ubuntu18.04 + Yolov3 + OpenCV_第5张图片创建一个build文件夹,并编译

$ mkdir build
$ cd build/

编译时,OpenCV4默认不使用pkg-config,可以再编译命令中开启生成opencv4.pc文件,支持pkg-config功能,自定义安装位置,方便管理OpenCV(不要遗漏命令末尾的点)

$ cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..

开始纠错
如果出现以下情况,下载失败,可以手动下载ippicv_2019_lnx_intel64_general_20180723.tgz:

其他版本下载

-- IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz
-- Try 1 failed

Ubuntu18.04 + Yolov3 + OpenCV_第6张图片
下载完成后,修改ippicv.cmake文件,先进入OpenCV根目录在执行:gedit /3rdparty/ippicv/ippicv.cmake

在ocv_download(FILENAME ${OPENCV_ICV_NAME}上面加入:“set(OPENCV_IPPICV_URL “file:///home/zhx/下载/”)”

替换为本地文件路径:
Ubuntu18.04 + Yolov3 + OpenCV_第7张图片
重新执行cmake命令:
在这里插入图片描述
出现:-- Looking for ccache - not found
解决办法:sudo apt-get install ccache
结果:
在这里插入图片描述

出现:-- Could NOT find Jasper (missing: JASPER_LIBRARIES JASPER_INCLUDE_DIR)
解决办法:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

在这里插入图片描述
出现:
--Could not find OpenBLAS include. Turning OpenBLAS_FOUND off -- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off -- Could NOT find Atlas (missing: Atlas_CBLAS_INCLUDE_DIR Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY Atlas_LAPACK_LIBRARY)
解决办法,依次运行如下命令:

  git clone git://github.com/xianyi/OpenBLAS
  cd OpenBLAS
  sudo apt-get install gfortran
  sudo make FC=gfortran
  sudo make install

创建同步文件链接:sudo ln -s /opt/OpenBLAS/lib/libopenblas.so.0 /usr/lib/libopenblas.so.0
在这里插入图片描述
如果出现:--Could NOT find JNI
解决办法:安装Java,并配置环境变量即可,可以参考我另一篇博客
在这里插入图片描述
如果出现:VTK is not found.
解决办法:

sudo apt install libvtk6-dev python-vtk6
sudo update-alternatives --install /usr/bin/vtk vtk /usr/bin/vtk6 10
sudo ln -s /usr/lib/python2.7/dist-packages/vtk/libvtkRenderingPythonTkWidgets.x86_64-linux-gnu.so /usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so

在这里插入图片描述
如果出现:-- Checking for module 'libavresample'-- No package 'libavresample' found
解决办法:sudo apt-get install libavresample-dev
在这里插入图片描述

如果出现:No package 'libdc1394-2' found
解决办法:

sudo apt-get install libdc1394-22
sudo apt-get install libdc1394-22-dev

在这里插入图片描述
纠错结束

4.进行make编译

在OpenCV根目录下执行:$ sudo make -j6 # runs 6 jobs in parallel

Ubuntu18.04 + Yolov3 + OpenCV_第8张图片
5.进行安装:$ sudo make install
Ubuntu18.04 + Yolov3 + OpenCV_第9张图片
6.配置OpenCV 的 pgk-config环境
找到:$ sudo find / -iname opencv4.pc
在这里插入图片描述
将/usr/local/OpenCV4/lib/pkgconfig/路径加入PKG_CONFIG_PATH:$ sudo gedit /etc/profile.d/pkgconfig.sh
写入以下内容:export PKG_CONFIG_PATH=/usr/local/OpenCV4/lib/pkgconfig:$PKG_CONFIG_PATH
Ubuntu18.04 + Yolov3 + OpenCV_第10张图片
更新:$ source /etc/profile

验证是否配置成功:$ pkg-config --libs opencv4

结果如下,配置成功:
Ubuntu18.04 + Yolov3 + OpenCV_第11张图片7.配置动态库环境

配置OpenCV动态库环境——程序执行时加载动态库*.so的路径:$ sudo gedit /etc/ld.so.conf.d/opencv4.conf

在该文件(可能是空文件)末尾加上:/usr/local/OpenCV4/lib

使配置的路径生效:$ sudo ldconfig

8.Python-opencv环境

找到编译好的python cv库:$sudo find / -iname cv2*.so,结果如下:

/home/zhx/OpenCV/opencv-4.2.0/build/lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so
/usr/local/OpenCV4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so

Ubuntu18.04 + Yolov3 + OpenCV_第12张图片
cv2.cpython-35m-x86_64-linux-gnu.so就是编译好的python3的opencv库
我们把它复制到对应python解释器的/path/to/dist-packages/(系统自带的python解释器)
和/path/to/site-packages(用户安装的python解释器)目录下,之后就能在该python解释器中使用python-opencv库

链接到系统自带的python3解释器中:$ sudo ln -s /usr/local/OpenCV4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.so

链接到Anaconda创建的虚拟环境python3解释器中:ln -s /usr/local/OpenCV4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so ~/anaconda3/lib/python3.7/site-packages/cv2.so

9.测试:

环境配置完成,对我们安装的OpenCV进行测试:

cd 到/opencv-4.0.0/samples/cpp/example_cmake目录下,查看当前目录内容:
Ubuntu18.04 + Yolov3 + OpenCV_第13张图片
通过Makefile测试

因为OpenCV 4.0需要C++11支持,且生成的pkg-config文件名为opencv4.pc,所以需要对当前目录下的Malefile文件进行修改:

$ gedit Makefile

原文如下:

CXX ?= g++

CXXFLAGS += -c -Wall $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs --static opencv)

all: opencv_example

opencv_example: example.o; $(CXX) $< -o $@ $(LDFLAGS)

%.o: %.cpp; $(CXX) $< -o $@ $(CXXFLAGS)

clean: ; rm -f example.o opencv_example

修改为:

CXX ?= g++
 
CXXFLAGS += -c -std=c++11 -Wall $(shell pkg-config --cflags opencv4)
LDFLAGS += $(shell pkg-config --libs --static opencv4)

all: opencv_example

opencv_example: example.o; $(CXX) $< -o $@ $(LDFLAGS)

%.o: %.cpp; $(CXX) $< -o $@ $(CXXFLAGS)

clean: ; rm -f example.o opencv_example

当前路径,执行make命令生成可执行文件opencv_example:

$ make

结果如下:

g++ example.cpp -o example.o -c -std=c++11 -Wall -I/usr/local/opencv4/include/opencv4/opencv -I/usr/local/opencv4/include/opencv4
g++ example.o -o opencv_example -L/usr/local/opencv4/lib -lopencv_ml -lopencv_dnn -lopencv_video -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_photo -lopencv_gapi -lopencv_imgproc -lopencv_core -ldl -lm -lpthread -lrt

运行生成的文件opencv_example:$ ./opencv_example

会打开计算机摄像头,并带有Hello OpenCV字样

通过Cmake测试
因为我们更改了opencv的安装路径(/usr/local/OpenCV4),所以测试前我们需要在CMakeLists.txt文件的14行find_package(OpenCV REQUIRED)前面加上:set(OpenCV_DIR /usr/local/OpenCV4/lib/cmake/opencv4)

修改后文件内容:

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
set(OpenCV_DIR /usr/local/OpenCV4/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example PRIVATE ${
     OpenCV_LIBS})

然后当前路径下执行:

$ mkdir build && cd build
$ cmake ..
$ make
$ ./opencv_example

Ubuntu18.04 + Yolov3 + OpenCV_第14张图片弹出和之前一样效果的视频框

10.在Python里引用:

$ python3
Python 3.5.2 (default, Oct  8 2019, 13:06:37) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.2.0

如果报错,缺少numpy,就执行pin3 install numpy,如果没有pip3,根据提示执行:sudo apt install python3-pip
Ubuntu18.04 + Yolov3 + OpenCV_第15张图片

三、YoloV3在GPU运行

1.进入Darknet路径,修改MakeFile文件:sudo gedit Makefile

GPU/CUDNN/OPENCV的值改为1

修改前:
Ubuntu18.04 + Yolov3 + OpenCV_第16张图片修改后,如下并保存:
Ubuntu18.04 + Yolov3 + OpenCV_第17张图片
2.找到:

LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv` 

改为:

LDFLAGS+= `pkg-config --libs opencv4` -lstdc++
COMMON+= `pkg-config --cflags opencv4` 

Ubuntu18.04 + Yolov3 + OpenCV_第18张图片
3.重新编译:

make clean
make

此时出现这样一个错误:

./src/image_opencv.cpp:12:1: error: ‘IplImage’ does not name a type; did you mean ‘image’?
 IplImage *image_to_ipl(image im)
 ^~~~~~~~
 image
compilation terminated due to -Wfatal-errors.
Makefile:86: recipe for target 'obj/image_opencv.o' failed
make: *** [obj/image_opencv.o] Error 1

Ubuntu18.04 + Yolov3 + OpenCV_第19张图片开始着手解决吧,先找到报错位置:sudo gedit ./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);



    //IplImage *ipl = image_to_ipl(copy);
    //Mat m = cvarrToMat(ipl, true);
    //cvReleaseImage(&ipl);
    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)
{
     
    //IplImage ipl = m;
    //image im = ipl_to_image(&ipl);
    
    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的函数

在把以:CV_ 开头的变量,去掉CV_,新版本不带CV_

最终修改后的image_opencv.cpp代码如下:

#ifdef OPENCV

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

using namespace cv;

extern "C" {
     

/*
IplImage *image_to_ipl(image im)
{
     
    int x,y,c;
    IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
    int step = disp->widthStep;
    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];
                disp->imageData[y*step + x*im.c + c] = (unsigned char)(val*255);
            }
        }
    }
    return disp;
}


image ipl_to_image(IplImage* src)
{
     
    int h = src->height;
    int w = src->width;
    int c = src->nChannels;
    image im = make_image(w, h, c);
    unsigned char *data = (unsigned char *)src->imageData;
    int step = src->widthStep;
    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.;
            }
        }
    }
    return im;
}
*/

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



    //IplImage *ipl = image_to_ipl(copy);
    //Mat m = cvarrToMat(ipl, true);
    //cvReleaseImage(&ipl);
    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)
{
     
    //IplImage ipl = m;
    //image im = ipl_to_image(&ipl);
    
    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(CV_CAP_PROP_FRAME_WIDTH, w);
    //if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
    //if(fps) cap->set(CV_CAP_PROP_FPS, w);
    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, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
    } else {
     
        resizeWindow(name, w, h);
        if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
    }
}

}

#endif

再次编译:

make clean
make

通过:

Ubuntu18.04 + Yolov3 + OpenCV_第20张图片进行YoloV3测试:./darknet detect cfg/yolov3.cfg data/yolov3.weights data/dog.jpg

报错:
CUDA Error: out of memory darknet: ./src/cuda.c:36: check_error: Assertion 0 failed. 已放弃 (核心已转储)

Ubuntu18.04 + Yolov3 + OpenCV_第21张图片
出现这问题的原因就是GPU内存不够大,解决方法就是减小内存的使用:

修改cfg/yolov3.cfg如下:

[net]
# Testing
batch=1                   //取消注释
subdivisions=1            //取消注释
# Training
batch=64			     //可以添加注释
subdivisions=16			 //可以添加注释
width=608                //可以适当减小width
height=608               //可以适当减小height

修改后:
Ubuntu18.04 + Yolov3 + OpenCV_第22张图片

运行通过:

Ubuntu18.04 + Yolov3 + OpenCV_第23张图片

至此,结束结束结束,如有错误,欢迎指正

你可能感兴趣的:(ubuntu,Ubuntu18.04,OpenCV4.1,Yolo3)