新建MFC工程項(xiàng)目
設(shè)計(jì)圖形界面

圖形界面
打開/關(guān)閉攝像頭
打開攝像頭
void CFaceSigninDlg::OnBnClickedOpenvedio()
{
m_flag = 1;
CRect rect;
CWnd *pWnd = GetDlgItem(LeftPicture); //獲取左側(cè)圖片控件
pWnd->GetClientRect(&rect);
int x = rect.Width();
int y = rect.Height();
//避免連續(xù)打開兩次
m_capture.release();
//初始化攝像頭
m_capture = VideoCapture(0);
//若無法打開攝像頭
if (!m_capture.isOpened()) {
fprintf(stderr, "Can not open camera.\n");
return;
}
while (m_flag) {
Mat frame;
m_capture >> frame; // >>操作符:讀取下一幀
resize(frame, m_dst, Size(x, y), 0, 0, 1);
imshow("view", m_dst);
waitKey(25);
}
}
關(guān)閉攝像頭
void CFaceSigninDlg::OnBnClickedVedioclose()
{
m_flag = 0;
}
獲取opencv窗口,并將其父窗口設(shè)置為圖形控件
//視頻預(yù)覽窗口
namedWindow("view", WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle("view");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(LeftPicture)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
抓拍人臉
void CFaceSigninDlg::OnBnClickedGetimage()
{
//保存某一幀圖片
String picpath = "F:\Homeworks\Python_xiaoxueqi\FaceSignin\FaceSignin\FaceSignin\faces";
imwrite(picpath, m_dst);
//讀取圖片
Mat image = imread(picpath);
Mat imagedst;
CWnd *pWnd = GetDlgItem(RightPicture); //獲取右邊圖片控件
CDC *pDC = NULL;
CString strPath;
//設(shè)置靜態(tài)控件樣式,使其可以現(xiàn)實(shí)位圖,并設(shè)置居中
((CStatic*)GetDlgItem(RightPicture)->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE));
pDC = GetDlgItem(RightPicture)->GetDC();
ShowImage(pDC, picpath, 0, 0);
ReleaseDC(pDC);
}
//顯示圖片
BOOL CFaceSigninDlg::ShowImage(CDC* pDC, String strPath, int x, int y)
{
IPicture *pPic = NULL;
OleLoadPicturePath(CComBSTR(strPath.c_str()), (LPUNKNOWN)NULL, 0, 0, IID_IPicture, (LPVOID*)&pPic);
if (NULL == pPic)
{
return FALSE;
}
// 寬度+高度
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//寬度和高度
RECT rtWnd;
pDC->GetWindow()->GetWindowRect(&rtWnd);
int iWndWidth = rtWnd.right - rtWnd.left;
int iWndHeight = rtWnd.bottom - rtWnd.top;
if (FAILED(pPic->Render(*pDC, x, y, iWndWidth, iWndHeight, 0, hmHeight, hmWidth, -hmHeight, NULL)))
{
pPic->Release();
return false;
}
//釋放資源
pPic->Release();
return true;
}
人臉檢測
//人臉檢測
std::vector<dlib::rectangle> faces = detector(cimg); //預(yù)測人臉
std::vector<full_object_detection> shapes; //此對象表示圖像中對象的位置及其每個(gè)組成部分的位置。
for (unsigned long i = 0; i < faces.size(); ++i) { //每一個(gè)人臉
cv::rectangle(frame, Rect(faces[i].left(), faces[i].top(), faces[i].width(), faces[i].height()), Scalar(0, 0, 255), 1, 1, 0);//繪制方框
shapes.push_back(pose_model(cimg, faces[i]));//將人臉加入shape
}
//特征點(diǎn)標(biāo)定
if (!shapes.empty()) {
//繪制特征點(diǎn)
for (int i = 0; i < 68; i++) {
cv::circle(frame, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
//顯示特征點(diǎn)的數(shù)字
putText(frame, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0), 1, 4);
}
//眨眼檢測
//左邊兩點(diǎn)
int y37 = shapes[0].part(37).y();
int y38 = shapes[0].part(38).y();
//右邊兩點(diǎn)
int y40 = shapes[0].part(40).y();
int y41 = shapes[0].part(41).y();
//中間兩點(diǎn)
int x36 = shapes[0].part(36).x();
int x39 = shapes[0].part(39).x();
int y1 = abs(y37 - y41);
int y2 = abs(y38 - y40);
int x1 = abs(x39 - x36);
//長寬比
float flg = (y1 + y2) / (2.0 * x1);
//顯示
//CString str;
//str.Format(_T("EAR:%.2s"), flg);
//m_regResult.SetWindowText(str);
//張嘴檢測
int y50 = shapes[0].part(50).y();
int y52 = shapes[0].part(52).y();
int y56 = shapes[0].part(56).y();
int y58 = shapes[0].part(58).y();
int x48 = shapes[0].part(48).x();
int x54 = shapes[0].part(54).x();
int my1 = abs(y50 - y58);
int my2 = abs(y52 - y56);
int mx1 = abs(x48 - x54);
//長寬比
float flgMouse = (my1 + my2) / (2.0 * mx1);
}