opencv3.4.2+yolov3+opendnn+cpu+gpu

opencv 3.4.2 已经出来了,并且添加了对yolo v3模型的支持。

opencv 的changelog

changelog里面提到了这句:

Added a support of YOLOv3 and image classification models from Darknet framework.

赶紧下载opencv3.4.2然后用opencv dnn这个推理结构试一下。

结果是cpu版本可以跑通,但是gpu加速开启不了。

先上我的参考代码。github地址:

ObjectDetection-YOLO

这是代码作者写的代码分析

deep-learning-based-object-detection-using-yolov3-with-opencv-python-c

作者给出了g++编译指令,但是我用了并没有通过。

OpenCV-DNN-YOLO.cpp:9:2: error: ‘c’ does not name a type
OpenCV-DNN-YOLO.cpp:14:2: error: ‘u’ does not name a type
OpenCV-DNN-YOLO.cpp:15:2: error: ‘u’ does not name a type
OpenCV-DNN-YOLO.cpp:16:2: error: ‘u’ does not name a type
OpenCV-DNN-YOLO.cpp:19:2: error: ‘f’ does not name a type
OpenCV-DNN-YOLO.cpp:20:2: error: ‘f’ does not name a type
OpenCV-DNN-YOLO.cpp:21:2: error: ‘i’ does not name a type
OpenCV-DNN-YOLO.cpp:22:2: error: ‘i’ does not name a type
OpenCV-DNN-YOLO.cpp:23:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:25:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:28:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:30:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:32:2: error: ‘i’ does not name a type
OpenCV-DNN-YOLO.cpp:152:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:198:2: error: ‘v’ does not name a type
OpenCV-DNN-YOLO.cpp:220:2: error: ‘v’ does not name a type

这是报错信息,具体问题我忘了,但后面我是用了cmake编译的。

在之前的项目的cmakelists文件上做了稍许修改。

这里是cmakelists.

project(object_detection_yolo)

cmake_minimum_required(VERSION 2.8.10)
set(OpenCV_DIR "/usr/local/share/OpenCV")
find_package(OpenCV)#REQUIRED core highgui imgproc imgcodecs
find_package(OpenMP)
if(OpenMP_CXX_FOUND OR OPENMP_FOUND)
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

SET(CMAKE_CXX_FLAGS_DEBUG "-g -ggdb -D DEBUG")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories(
        ${OpenCV_INCLUDE_DIRS}

)
link_directories(
	/usr/local/lib
	/root/anaconda3/pkgs/icu-54.1-0/lib
	/root/anaconda3/pkgs/libpng-1.6.22-0/lib
)

add_executable(object_detection_yolo object_detection_yolo.cpp)
target_link_libraries(object_detection_yolo  ${OpenCV_LIBS} png )

然后cmake 就可以生成 object_detection_yolo。

运行指令用github上给出的:

C++:
a single image:
    ./object_detection_yolo.out --image=bird.jpg
a video file:
    ./object_detection_yolo.out --video=run.mp4

就可以看到bird_yolo_out_cpp.jpg结果啦。

这里是300多ms一张图,是cpu检测的结果。

opencv3.4.2+yolov3+opendnn+cpu+gpu_第1张图片

我想尝试GPU加速。

在cpp文件中,找到这一句

 net.setPreferableTarget(DNN_TARGET_CPU);

将括号里得内容替换为DNN_TARGET_OPENCL。

可以参考dnn 的用法介绍

但是还是没能开启。

程序自动转到了CPU。

这是命令行提示

[ WARN:0] DNN: OpenCL target is not supported with current OpenCL device (tested with Intel GPUs only), switching to CPU.

但是这个程序的作者也提到了:

Note: We ran into problems using OpenCV’s GPU implementation of the DNN. The documentation indicates that it is tested only with Intel’s GPUs, so the code would switch you back to CPU, if you do not have an Intel GPU.

在开启GPU时也遇到了问题。

这是个值得探讨的问题,在我写这篇博客时,目前opencv3.4.3也出来了,说不定可以进行再次尝试。我后面又去了解了OPEN VINO,这个将记录在下篇博客中。

你可能感兴趣的:(opencv3.4.2+yolov3+opendnn+cpu+gpu)