溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用OpenCV 和MFC怎么實現(xiàn)一個人臉識別功能

發(fā)布時間:2021-04-19 17:41:58 來源:億速云 閱讀:186 作者:Leah 欄目:編程語言

使用OpenCV 和MFC怎么實現(xiàn)一個人臉識別功能?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1 設(shè)置控件

首先新建一個基于Dialog的MFC程序的工程,工程名為FaceDetect ;
然后在IDD_FACEDETECT_DIALOG對話框中添加一個Picture 控件,ID命名為:IDC_PICTURE;添加一個Button控件,Caption命名為 “檢測”,ID命名為IDC_START,將原來自動生成的的OK按鈕的Caption改為“退出”;
刪除原來的Text控件和“Cancel”控件。

2 定義變量

在FaceDetectDlg.h開頭添加以下幾行代碼

#pragma once
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp”
using namespace std;
using namespace cv;

然后在CFaceDetectDlg類定義一下幾個變量

public:
 String face_cascade_name; 
 String eyes_cascade_name; 
 CascadeClassifier face_cascade;
 CascadeClassifier eyes_cascade;
 VideoCapture capture;

3 對定義的變量初始化

CFaceDetectDlg::CFaceDetectDlg(CWnd* pParent /*=NULL*/)
 : CDialogEx(CFaceDetectDlg::IDD, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 string face_cascade_name = "";
 string eyes_cascade_name = "";
}
BOOL CFaceDetectDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
 BOOL bNameValid;
 CString strAboutMenu;
 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
 ASSERT(bNameValid);
 if (!strAboutMenu.IsEmpty())
 {
 pSysMenu->AppendMenu(MF_SEPARATOR);
 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
 }
 }

 // Set the icon for this dialog. The framework does this automatically
 // when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE); // Set big icon
 SetIcon(m_hIcon, FALSE); // Set small icon

 // TODO: Add extra initialization here
 string face_cascade_name = "..\\debug\\haarcascade_frontalface_alt.xml";
 string eyes_cascade_name = "..\\debug\\haarcascade_eye_tree_eyeglasses.xml";
 if( !face_cascade.load( face_cascade_name ) )
 {
 MessageBox(_T("haarcascade_frontalface_alt.xml Error loading")); 
 return -1;
 };

 if( !eyes_cascade.load( eyes_cascade_name ) )
 {
 MessageBox(_T(" haarcascade_eye_tree_eyeglasses.xmlError loading"));
 return -1;
 };

 return TRUE; // return TRUE unless you set the focus to a control
}

4 檢測函數(shù)的編寫

思路是這樣的:

1.首先打開攝像頭
2.然后將攝像托獲取的圖像傳遞給人臉識別的函數(shù)
3.將識別后處理過的圖像在Picture控件中顯示出來

雙擊IDD_FACEDETECT_DIALOG對話框上的上的“檢測”按鈕控件,進入控件函數(shù)編寫的地方,該函數(shù)如下所示:

void CFaceDetectDlg::OnBnClickedStart()
{
 // TODO: Add your control notification handler code here
 capture.open(0);//捕獲外部攝像頭,如果只有一個攝像頭,就填0
 Mat frame;
 namedWindow("view", WINDOW_AUTOSIZE);

 HWND hWnd = (HWND)cvGetWindowHandle("view");
 HWND hParent = ::GetParent(hWnd);

 ::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
 ::ShowWindow(hParent, SW_HIDE);//隱藏運行程序框,并且把它“畫”到MFC上

 if (capture.isOpened())
 {
 for (;;)//循環(huán)以達到視頻的效果
 {
 capture >> frame;

 if (!frame.empty())
 {
 detectAndDisplay(frame);//識別的函數(shù)

 imshow("view", frame);
 UpdateData(FALSE);
 }
 else
 {
 //::AfxMessageBox(" --(!) No captured frame -- Break!");

 continue;
 //break;
 }

 waitKey(10);
 }

 }

}

以上代碼中 detectAndDisplay(frame)語句表示調(diào)用了 detectAndDisplay(Mat frame)函數(shù),因此我們得聲明和定義該函數(shù)。

在CFaceDetectDlg類的頭文件FaceDetectDlg.h中聲明該函數(shù):

void detectAndDisplay(Mat frame);//聲明函數(shù)

在FaceDetectDlg.cpp中定義該函數(shù):

void CFaceDetectDlg::detectAndDisplay( Mat frame )
{
 std::vector<Rect> faces;
 Mat frame_gray;

 cvtColor( frame, frame_gray, CV_BGR2GRAY );
 equalizeHist( frame_gray, frame_gray );

 //-- 多尺寸檢測人臉
 face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

 for( int i = 0; i < faces.size(); i++ )
 {
 Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
 ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

 Mat faceROI = frame_gray( faces[i] );
 std::vector<Rect> eyes;

 //-- 在每張人臉上檢測雙眼
 eyes_cascade.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*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
 int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
 circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
 }
 }

}

編譯運行

編譯工程,然后將
haarcascade_frontalface_alt.xml 和 haarcascade_eye_tree_eyeglasses.xml拷貝到工程目錄文件下Debug文件夾里,也就是可執(zhí)行文件所在的那個文件夾。

以上基本上可以實現(xiàn)預(yù)期的人臉識別功能,可是我們可以發(fā)現(xiàn)此時點擊“退出”按鈕時,攝像頭的燈還亮著,那是因為攝像頭在程序退出后沒有關(guān)閉掉,因此還得添加代碼關(guān)閉攝像頭。

雙擊“退出”按鈕,編輯代碼如下

void CFaceDetectDlg::OnBnClickedOk()
{
 // TODO: Add your control notification handler code here
 capture.release(); //關(guān)閉攝像頭
 CDialogEx::OnOK();
}

關(guān)于使用OpenCV 和MFC怎么實現(xiàn)一個人臉識別功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI