人臉識別實(shí)戰(zhàn)筆記——程序設(shè)計(jì)

新建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);
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 單純美好的我們 直到懸河千言匯入那句——“遇見你是我的壯舉”是否成為“愛你是我的壯舉” 01 想必你的青春里也有過...
    安筱淼閱讀 350評論 0 1
  • 連續(xù)幾天,除了疫苗,屏幕被教育界、新聞界、公益圈等各色知名人士性騷擾和強(qiáng)奸女性的新聞報(bào)導(dǎo),攪得社會惶惶不安。 有人...
    高浩容閱讀 2,586評論 12 18
  • 記得你開心時(shí) 守紅 今天的風(fēng),吹不去昨夜的霾地球突然變得好沉重太陽想要吸引它,也變得好難,好難 那對在門口戀愛的小...
    孫守紅閱讀 182評論 0 1
  • 由于空氣等環(huán)境的污染,小兒哮喘發(fā)病越來越多,給許多父母、家庭來煩惱。 孩子一發(fā)病,急得家長們團(tuán)團(tuán)轉(zhuǎn),抱...
    中醫(yī)張閱讀 896評論 2 3

友情鏈接更多精彩內(nèi)容