■Pre-requisite
Microsoft Visual Studio (我用的是Visual Studio 2017 in 64-bit PC)
CMake for Windows
■環境設定
●Step1:Downloading OpenCV with extra modules
新增opencv-3.0資料夾。在其下新增兩個資料夾,如下圖:
build:放build出來的東西
source :OpenCV還有extra moudules
下載
解壓縮並放到source資料夾裡面如下圖
●Step2:Making Visual Studio Project with CMake
開啟cmake-gui。如下圖填入路徑後按Configure
會看到下面這個視窗。紅框的地方選符合你電腦現狀的
然後選Specify native compliers -> 按Next
Compiler設 {Visual Studio}/VC/bin/cl.exe ->按Finish
註:cl.exe路徑不一定如上,像我的電腦就不是(請看下圖)
若無法在上述的路徑找到cl.exe,那就用最笨的方法
在windows explorer 右上的搜尋功能去找cl.exe位置
成功之後會看到如下的Log
非常重要的步驟-> Inclue Intel Inference Engine Support
若沒做此步,到時候程式用到intel IE 的backend
會出現這個Error -> Assertion failed (haveInfEngine())
specify the extra modules
OPENCV_EXTRA_MODULES_PATH填入
{opencv-3.0}/source/opencv_contrib/modules
請注意路徑斜線不能是倒斜線!!!
像這樣{opencv-3.0}\source\opencv_contrib\modules
若你的路徑是直接打開資料夾copy
再貼上到OPENCV_EXTRA_MODULES_PATH就會是倒斜線
而這會導致下面的Error
這Error搞得我非常頭大
因為我一開始沒有去看上圖紅色那些Error Log
整個Log很長 上圖講到導致Error的Log
(Invalid character escape '\o')
是在Log的中間部分
Log最後一行告訴使用者可以去看CMakeError.log
我去看了之後得到以下根本跟root cause無關的msg
D:\opencv-3.0\build\CMakeFiles\CMakeTmp\CheckIncludeFile.c(1):
fatal error C1083: Cannot open include file: 'pthread.h':
No such file or directory [D:\opencv-3.0\build\CMakeFiles\
CMakeTmp\cmTC_96be3.vcxproj]
導致我debug方向大錯誤!
上面Step2 填入的路徑就沒有此問題
因為複製path的倒斜線貼上之後 會自動轉成斜線 如下圖
重要:按Configure
少做此步會有以下Error
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
directory
按Generate產生Visual Studio Project 如下圖
●Step3: Building OpenCV Project
開啟OpenCV.sln。用admin權限開 否則會出現下面的 build Error
file cannot create directory: C:/Program Files (x86)/Microsoft Visual
Studio/Shared/Python36_64/Lib/site-packages/cv2.
Maybe need administrative privileges.
Solution Configuration我選 Release ,
因為之後我要建立的OpenCV project也是要用Release build
會需要用到release版的opencv lib
Solution Explorer底下找CMakeTargets資料夾底下的INSTALL project
build它。build的時間會花好幾分鐘,請耐心等候
●Step4:Developing Application using OpenCV
Registering OpenCV Environment Variables
Creating OpenCV Project Property Sheet
●Step4-1:Registering OpenCV Environment Variables
在程式裡面使用OpenCV前,需要註冊OpenCV 的path到系統環境變數
Control Panel -> System -> Advanced System Settings
->Environment Variables -> 選System variables -> New ->
name填OPENCV_DIR
value填x64底下的vc資料夾位置 如下圖
加一個Path,裡面放%OPENCV_DIR%\bin
如法炮製再加一個Path,裡面放程式會需要用到的intel IE dll檔
C:\Program Files (x86)\IntelSWTools\openvino_2019.1.148\
deployment_tools\inference_engine\bin\intel64\Release
●Step4-2:Creating OpenCV Project Property SheetCreating
OpenCV Project Property Sheet
現在可以建立一個OpenCV project了 (以下以 VS2017 Professional為例)
建一個空的C++ project -> View -> Other Windows ->
Property Manager -> Release | 64資料夾上按右鍵->
Add New Project Property Sheet
指定Property Sheet name 。例:OPENCV3_RELEASE ->Add
在剛剛新增的Property Shhet上按右鍵-> Properties
在Configuration Properties->C/C++/General的Additional Include Directories
加入$(OPENCV_DIR)\..\..\include
Configuration Properties->Linker->General的Additional Library Directories
Configuration Properties->Linker->Input的Additional Dependencies
加入在%(OPENCV_DIR)\lib底下的lib
- 若建立了Debug Mode Property Sheet,則只要加檔名結尾是d的lib
比如opencv_bgsegm300d.lib, opencv_bioinspired300d.lib
- 若建立了Release Mode Property Sheet,則只要加檔名結尾沒有d的lib
比如opencv_bgsegm300.lib, opencv_bioinspired300.lib
有了Property Sheet,在新增其他OpenCV 專案的時候就不用重複以上步驟。
只要加入已經編輯好的Property Sheet.
●Step5:Sample Code
我選了一個分類的model,用這個model對input frame from video裡的object做分類
複製下面這個cpp的程式碼到你的cpp裡面
https://github.com/opencv/opencv/blob/master/samples/dnn/classification.cpp
common.hpp 放到 $(OPENCV_DIR)\..\..\include 底下
下載OpenCV_extra 然後新增環境變數 OPENCV_DNN_TEST_DATA_PATH
path 放<opencv_extra>/testdata
安裝 Intel's Inference Engine Library
https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend
若無安裝,則指定backend為Intel's Deep Learning Inference Engine時
會出現此Error Assertion failed (haveInfEngine())
指令
$(your project name).exe --backend=2 --config=D:\squeezenet1.1.prototxt
-- model=D:\squeezenet1.1.caffemodel --classes=D:\squeezenet1.1.labels
--mean="0 0 0" --rgb --width=224 --height=224
caffemodel跟caffemodel 我是從底下這個path複製到D槽的
C:\Users\???\Documents\Intel\OpenVINO\openvino_models\models\FP32\classification\squeezenet\1.1\caffe
labels 是從底下這個path複製到D槽的
C:\Users\???\Documents\Intel\OpenVINO\openvino_models\ir\FP32\classification\squeezenet\1.1\caffe
至於為什麼在上面提到的path底下有這個model
是因為我在做這件事的時候 就已經有下載model了
剛好就拿來用 這樣而已
●執行結果
結果相當噴飯
我也不曉得我哪裡跟馬桶蓋相像了XD
但我測試背景很髒也是事實
(其實我在白牆背景下測試的結果也很差就是...)
總之我目的只是要會弄出一個OpenCV DNN project with Intel IE而已
目的已經達成,要繼續下階段的研究了...
●Trouble Shooting
1. Error C1010 unexpected end of file while looking for precompiled header.
Did you forget to add '#include "pch.h"' to your source?
-> Fixed by disable this in visual studio: project property -> c++ -> all options.
2. Error LNK1127 library is corrupt
opencv- 3.0\build\install\x64\vc15\lib\opencv_core411.lib
-> Fixed by rebuild OpenCV.sln
3. opencv_core411.dll was not found。但環境變數 %OPENCV_DIR%\bin底下是有這個檔案存在的
-> Fixed by 重開機。環境變數需要重開機才會生效
4. Error C4996 'getenv': This function or variable may be unsafe.
Consider using _dupenv_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
See online help for details. $(your project name)
d:\opencv- 3.0\build\install\include\common.hpp 61