利用OpenCV + contrib + DNN 和 google的训练好的分类器模型,实现一个简单的图片识别demo。
后续再详细研究下OpenCV和DNN。
参考链接:
OpenCV3-contrib库使用:http://www.jianshu.com/p/e5d41cab8057
OpenCV3-DNN使用:http://www.jianshu.com/p/fdf9c3b70dd4
OpenCV3中contrib模块的添加:http://blog.csdn.net/streamchuanxi/article/details/51044929
一、工具准备
以下工具和源码,本例统一放到F:\dnn-demo-test 目录。
1. vs2015或者vs2013,安装社区个人免费版就可以;
2. CMake3.5,cmake-3.5.1-win32.zip 官网下载安装即可
下载链接:
https://cmake.org/files/v3.5/cmake-3.5.1-win32-x86.zip
3. OpenCV3.0.0:
下载链接:
https://nchc.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.0.0/opencv-3.0.0.exe
下载完安装OpenCV到指定目录,本例安装到 F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0
4. opencv_contrib
下载链接:
https://github.com/Itseez/opencv_contrib
5. bvlc_googlenet.caffemodel (训练好的分类器模型)
下载链接:
http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel
二、编译安装OpenCV-3.0.0及opencv-contrib
1. cmake生成工程
打开CMake GUI工具。
CMake GUI
Where is the source code:选择F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv或其他的解压目录。
Where to build the binaries:选择F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild或对应的目录。
点击Congigure按钮,CMake会自己进行配置。在弹出的对话框中选择Visual Studio 14 2015。
初步配置完成后,在OPENCV_EXTREA_MODULES_PATH选项中选择F:\dnn-demo-test\opencv_contrib\modules,再次点击Congigure进行配置。
如果配置完成没有问题,则点击Generate进行生成。
2. 编译安装
进入F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild目录,打开OpenCV.sln;
选择release和debug模式,分别编译整个解决方案,每次编译完成后,在CMakeTargets工程中的INSTALL上右键选择Project Only->Build Only INSTALL。
完成后在F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install目录下就会生成编译好的OpenCV文件。
3. 环境变量配置
增加如下环境变量:
修改Path环境变量:
三、新建caffe_googlenet工程
1. 打开vs2015-->新建console工程-->工程名caffe_goolenet。
2. 将F:\dnn-demo-test\opencv_contrib\modules\modules\dnn\samples目录下的四个文件拷贝到项目文件夹内。
3. 配置工程
项目-->caffe_googlenet属性,修改配置如下(配置里面选所有配置):
配置属性-->VC++目录-->可执行文件路径末尾添加:
F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install\bin;
配置属性-->VC++目录-->包含目录末尾添加:
F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install\include;
F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install\include\opencv;
F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install\include\opencv2;
配置属性-->链接器-->输入-->附加依赖项-->编辑 在末尾加入以下库:
opencv_bgsegm300d.lib
opencv_bioinspired300d.lib
opencv_calib3d300d.lib
opencv_ccalib300d.lib
opencv_core300d.lib
opencv_datasets300d.lib
opencv_dnn300d.lib
opencv_dpm300d.lib
opencv_face300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_hal300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_line_descriptor300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_reg300d.lib
opencv_rgbd300d.lib
opencv_saliency300d.lib
opencv_shape300d.lib
opencv_stereo300d.lib
opencv_structured_light300d.lib
opencv_superres300d.lib
opencv_surface_matching300d.lib
opencv_text300d.lib
opencv_tracking300d.lib
opencv_ts300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib
opencv_xobjdetect300d.lib
4. 编译运行
编译工程,成功后,还不能正常运行,会提示缺少很多文件,到编译目标目录,按下图拷贝文件到指定release或debug目录:
运行:
打开cmd进入上述目录(有个简便方法:打开release文件夹,选中路径,敲cmd,会自动打开cmd并进入到release目录):
test文件夹,拷贝几个待测试图片:
cmd中输入,caffe_googlenet.exe 图片路径。
会看到识别结果如图所示: