利用OpenCV + contrib + DNN 和 google的訓(xùn)練好的分類器模型宪拥,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片識(shí)別demo洲脂。
后續(xù)再詳細(xì)研究下OpenCV和DNN芒率。
參考鏈接:
OpenCV3-contrib庫(kù)使用:http://www.reibang.com/p/e5d41cab8057
OpenCV3-DNN使用:http://www.reibang.com/p/fdf9c3b70dd4
OpenCV3中contrib模塊的添加:http://blog.csdn.net/streamchuanxi/article/details/51044929
一、工具準(zhǔn)備
以下工具和源碼,本例統(tǒng)一放到F:\dnn-demo-test 目錄媒咳。
1. vs2015或者vs2013,安裝社區(qū)個(gè)人免費(fèi)版就可以种远;
2. CMake3.5,cmake-3.5.1-win32.zip 官網(wǎng)下載安裝即可
下載鏈接:
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 (訓(xùn)練好的分類器模型)
下載鏈接:
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或?qū)?yīng)的目錄膝迎。
點(diǎn)擊Congigure按鈕,CMake會(huì)自己進(jìn)行配置胰耗。在彈出的對(duì)話框中選擇Visual Studio 14 2015限次。
初步配置完成后,在OPENCV_EXTREA_MODULES_PATH選項(xiàng)中選擇F:\dnn-demo-test\opencv_contrib\modules柴灯,再次點(diǎn)擊Congigure進(jìn)行配置卖漫。
如果配置完成沒(méi)有問(wèn)題,則點(diǎn)擊Generate進(jìn)行生成赠群。
2. 編譯安裝
進(jìn)入F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild目錄羊始,打開OpenCV.sln;
選擇release和debug模式查描,分別編譯整個(gè)解決方案突委,每次編譯完成后,在CMakeTargets工程中的INSTALL上右鍵選擇Project Only->Build Only INSTALL冬三。
完成后在F:\dnn-demo-test\opencv-3.0\Dt\opencv-3.0\opencv\mybuild\install目錄下就會(huì)生成編譯好的OpenCV文件匀油。
3. 環(huán)境變量配置
增加如下環(huán)境變量:
修改Path環(huán)境變量:
三、新建caffe_googlenet工程
1. 打開vs2015-->新建console工程-->工程名caffe_goolenet勾笆。
2. 將F:\dnn-demo-test\opencv_contrib\modules\modules\dnn\samples目錄下的四個(gè)文件拷貝到項(xiàng)目文件夾內(nèi)敌蚜。
3. 配置工程
項(xiàng)目-->caffe_googlenet屬性,修改配置如下(配置里面選所有配置):
配置屬性-->VC++目錄-->可執(zhí)行文件路徑末尾添加:
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;
配置屬性-->鏈接器-->輸入-->附加依賴項(xiàng)-->編輯 在末尾加入以下庫(kù):
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. 編譯運(yùn)行
編譯工程匠襟,成功后钝侠,還不能正常運(yùn)行,會(huì)提示缺少很多文件酸舍,到編譯目標(biāo)目錄帅韧,按下圖拷貝文件到指定release或debug目錄:
運(yùn)行:
打開cmd進(jìn)入上述目錄(有個(gè)簡(jiǎn)便方法:打開release文件夾,選中路徑啃勉,敲cmd忽舟,會(huì)自動(dòng)打開cmd并進(jìn)入到release目錄):
test文件夾,拷貝幾個(gè)待測(cè)試圖片:
cmd中輸入,caffe_googlenet.exe? 圖片路徑叮阅。
會(huì)看到識(shí)別結(jié)果如圖所示: