Ubuntu18.04 + Yolov3 + OpenCV

一、YoloV3安裝

1.進入官網

根據說明:下載Darknet

在自己喜歡的位置解壓Darknet,進入Darknet目錄並編譯:

cd darknet
make

等待完成即可:
在這裏插入圖片描述
下載權重文件:wget https://pjreddie.com/media/files/yolov3.weights特別慢
在這裏插入圖片描述
下載完成後,將權重文件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

在這裏插入圖片描述在這裏插入圖片描述
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,並解壓縮

在這裏插入圖片描述創建一個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

在這裏插入圖片描述
下載完成後,修改ippicv.cmake文件,先進入OpenCV根目錄在執行:gedit /3rdparty/ippicv/ippicv.cmake

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

替換爲本地文件路徑:
在這裏插入圖片描述
重新執行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

在這裏插入圖片描述
5.進行安裝:$ sudo make install
在這裏插入圖片描述
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
在這裏插入圖片描述
更新:$ source /etc/profile

驗證是否配置成功:$ pkg-config --libs opencv4

結果如下,配置成功:
在這裏插入圖片描述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

在這裏插入圖片描述
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目錄下,查看當前目錄內容:
在這裏插入圖片描述
通過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

在這裏插入圖片描述彈出和之前一樣效果的視頻框

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
在這裏插入圖片描述

三、YoloV3在GPU運行

1.進入Darknet路徑,修改MakeFile文件:sudo gedit Makefile

GPU/CUDNN/OPENCV的值改爲1

修改前:
在這裏插入圖片描述修改後,如下並保存:
在這裏插入圖片描述
2.找到:

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

改爲:

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

在這裏插入圖片描述
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

在這裏插入圖片描述開始着手解決吧,先找到報錯位置: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

通過:

在這裏插入圖片描述進行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. 已放棄 (核心已轉儲)

在這裏插入圖片描述
出現這問題的原因就是GPU內存不夠大,解決方法就是減小內存的使用:

修改cfg/yolov3.cfg如下:

[net]
# Testing
batch=1                   //取消註釋
subdivisions=1            //取消註釋
# Training
batch=64			     //可以添加註釋
subdivisions=16			 //可以添加註釋
width=608                //可以適當減小width
height=608               //可以適當減小height

修改後:
在這裏插入圖片描述

運行通過:

在這裏插入圖片描述

至此,結束結束結束,如有錯誤,歡迎指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章