【LattePanda教學】openCV環境安裝與基本臉孔瞳孔偵測 @Windows 10

本文將說明如何設定 LattePanda 拿鐵熊貓開發板上的 openCV環境,並執行一個小範例來玩玩看
延伸閱讀
- CAVEDU的 Lattepanda 相關文章
- LattePanda 拿鐵熊貓教學#1:LED 閃爍,使用Visual Studio
- LattePanda 拿鐵熊貓教學#2:讀取按鈕狀態,使用Visual Studio
- LattePanda 開機步驟與連接螢幕
- LattePanda 拿鐵熊貓教學:Arduino與Visual Studio環境設定
Step 1:安裝 Visual Studio 2015 與 OpenCV
- 請在 Lattepanda 上安裝 Visual Studio 2015
- 安裝 OpenCV (http://www.opencv.org),並下載最新版的 OpenCV (下圖)。由於 CAVEDU 所拿到的 lattepanda 的作業系統為 Windows 10 64 bit,因此本範例要下載的是 OpenCV 3.1 搭配 Visual Studio 2015 professional。下載之後一路安裝到底即可,以 Lattepanda 來說會是 F:。

Step 2: 設定環境變數
- 請開啟系統 -> 控制台,找到"進階系統設定"。To do this step, open the Control Panel and then System. Click the Advanced System Settings, last Environment Variables in turns as show in the following figure.

- 找到 PATH 這個環境變數,並加入以下內容。記得每一筆資料之間是用 ; 隔開:
F:\opencv\build\x64\vc14\bin。請注意這個路徑要根據您上一步時解壓縮的路徑而定。

Step 3: 建立 Visual Studio 2015 新專案
- 在Visual Studio 2015 中建立一個新專案,並根據下圖來完成所有步驟。

- 在 Visual C++ 選項中找到 Win32 Console Application ,接著幫專案取一個名字並設定路徑。在此取名為 faceDetect。

- 勾選 empty project 之後點選 Finish。

- 新增一個 cpp 檔,後續要在此貼入本專案的程式碼。在 Resource Files 點選 Add -> New Item...,接著選擇 C++ Files,點選 Add。

Step 4: 在Visual Studio 2015 中設定OpenCV
- 開啟本專案的 Property Manager,雙擊 Debug|Win64
- 根據下圖找到 "Include Directories" 選項,並輸入以下內容,完成之後按OK:
F:\opencv\build\includeF:\opencv\build\include\opencvF:\opencv\build\include\opencv2路徑根據 STEP 2 的實際路徑而定。

- 根據下圖找到 "Include Directories" 選項,並輸入以下內容,完成之後按OK:
- 新增 Library Directories:
F:\opencv\build\x64\vc14\lib。路徑根據 STEP 2 的實際路徑而定。
1.加入 additional dependencies
請根據下圖將以下項目加入 additional Dependences 欄位:opencv_world310d.lib
Step c++程式碼
在 Step 4 中的 .cpp 檔中貼入以下程式碼
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<stdio.h>
using namespace std;
using namespace cv;
string harrEye = "..\\..\\..\\resources\\harr\\haarcascade_eye_tree_eyeglasses.xml";
string harrFace = "..\\..\\..\\resources\\harr\\haarcascade_frontalface_alt.xml";
CascadeClassifier faceCascade;
CascadeClassifier EyeCascade;
string windownName = "Capture faces and eyes ";
void detectAndDiapley(Mat frame);
int main()
{
Mat frame;
// load the cascades
if (!EyeCascade.load(harrEye))
cout << "load harrEye failed" << endl;
if (!faceCascade.load(harrFace))
cout << "load harrFace failed" << endl;
// read the video stream
VideoCapture capture(0);
if (capture.isOpened())
{
while (true)
{
capture >> frame;
// apply the cascaders to the frame
if (!frame.empty())
{
detectAndDiapley(frame);
}
else
{
cout << "input video frame is empty" << endl;
}
if (waitKey(30) >= 0)break;
}
}
return 0;
}
void detectAndDiapley(Mat frame)
{
vector<Rect> faces;
Mat frameGray;
cvtColor(frame, frameGray, CV_BGR2GRAY);
equalizeHist(frameGray, frameGray);
//Detect faces
faceCascade.detectMultiScale(frameGray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
Point Vertex1(faces[i].x, faces[i].y);
Point Vertex2(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
rectangle(frame, Vertex1, Vertex2, Scalar(0, 0, 255), 2, 8, 0);
Mat faceROI = frameGray(faces[i]);
vector<Rect> eyes;
// detect eyes in each face
EyeCascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int j = 0; j < eyes.size(); j++)
{
Point center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
Size axes(eyes[j].width / 2, 13);
ellipse(frame, center, axes, 0, 0, 360, Scalar(255, 255, 0), 2, 8, 0);
}
}
// show the faces and eyes detected
imshow(windownName, frame);
}
Step 6: 執行專案
請如下圖設定您的專案: Debug 與 x64:
按下 F5 來執行臉部偵測專案,您的攝影機就會啟動,用您的臉或隨意的照片來測試看看吧。程式在圖檔上標出臉孔與瞳孔位置:hi, Lena,看到妳真好

參考資料
- http://www.michaelpsyllakis.com/install-opencv-on-visual-studio-2015-community-tutorial/
- http://docs.opencv.org/2.4/opencv_tutorials.pdf
- http://docs.opencv.org/2.4/opencv2refman.pdf
本文發表於lattepanda 拿鐵熊貓、OpenCV,並標記lattepanda、opencv、windows 10。




留言 💬 (0)
還沒有留言,來當第一個。