/ facerecog --username xjed09/frtest/EnrollDlg.cpp
C++ | 358 lines | 289 code | 50 blank | 19 comment | 37 complexity | c826fade042a2f5ad98494f4c83e655f MD5 | raw file
- // EnrollDlg.cpp : ????
- //
-
- #include "stdafx.h"
- #include "frtest.h"
- #include "EnrollDlg.h"
- #include "WndUtils.h"
- #include "ProjDefs.h"
-
-
- // CEnrollDlg ???
-
- IMPLEMENT_DYNAMIC(CEnrollDlg, CDialog)
-
- CEnrollDlg::CEnrollDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CEnrollDlg::IDD, pParent)
- , m_strName(_T(""))
- , m_bSaveSmallImg(true)
- {
- m_thrdFace = NULL;
- noface = cvLoadImage("noface.bmp");
- }
-
- CEnrollDlg::~CEnrollDlg()
- {
- cvReleaseImage(&noface);
- }
-
- void CEnrollDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_NAME, m_strName);
- //DDX_Text(pDX, IDC_CLASSID, m_strClassId);
- DDX_Check(pDX, IDC_SAVE_SMALL, m_bSaveSmallImg);
- }
-
- BOOL CEnrollDlg::OnInitDialog() // ??DoModal??????
- {
- CDialog::OnInitDialog();
-
- faceSz = g_faceMngr->m_faceSz;
- showSz = faceSz;
-
- if (showSz.height != 160) // (160*130)??????????????????????
- showSz *= (160.0/showSz.height);
- if (showSz.width > 130)
- showSz *= (130.0/showSz.width);
-
- EmbedCvWindow(GetDlgItem(IDC_FACE)->m_hWnd, "face", showSz.width, showSz.height);
- if (noface) cvShowImage("face", noface);
- RECT rc;
- GetWindowRect(&rc);
- SetWindowPos(NULL, rc.left + 15, rc.top + 300, 0,0, SWP_NOSIZE | SWP_NOZORDER);
-
- m_nShootNum = 0;
-
- m_strName = "";
- UpdateData(FALSE);
- CWnd *pBtn = GetDlgItem(IDC_SHOOT);
- RECT rc1;
- pBtn->GetClientRect(&rc1);
- pBtn->SetWindowText("??");
- pBtn->SetWindowPos(NULL, 0,0, 35, rc1.bottom-rc1.top, SWP_NOMOVE | SWP_NOZORDER);
- GetDlgItem(IDC_DEL_SHOOT)->ShowWindow(SW_HIDE);
- GetDlgItem(IDC_ENROLL_PIC)->ShowWindow(SW_SHOW);
- GetDlgItem(IDC_IMPORT_MODELS)->ShowWindow(SW_SHOW);
- if (g_bHasWebcam)
- {
- m_frame = cvCreateImage(g_webcam.GetFrameSize(), IPL_DEPTH_8U, 3);
- SetDlgItemText(IDC_SHOOT_GUIDE,
- "????????????????????\n"
- "??????,??????????????????????\n"
- "????????????????3~4??");
- m_bFaceStarted = true;
- m_thrdFace = ::AfxBeginThread(ShowFaceProc, this);
- }
- else
- {
- m_frame = NULL;
- SetDlgItemText(IDC_SHOOT_GUIDE, "");
- CWnd *pWnd = GetDlgItem(IDC_SHOOT);
- pWnd->EnableWindow(FALSE);
- }
-
- return TRUE;
- }
-
- BEGIN_MESSAGE_MAP(CEnrollDlg, CDialog)
- ON_BN_CLICKED(IDC_SHOOT, &CEnrollDlg::OnBnClickedShoot)
- ON_BN_CLICKED(IDCANCEL, &CEnrollDlg::OnBnClickedCancel)
- ON_BN_CLICKED(IDOK, &CEnrollDlg::OnBnClickedOk)
- ON_BN_CLICKED(IDC_ENROLL_PIC, &CEnrollDlg::OnBnClickedEnrollPic)
- ON_BN_CLICKED(IDC_IMPORT_MODELS, &CEnrollDlg::OnBnClickedImportModels)
- ON_BN_CLICKED(IDC_ADV, &CEnrollDlg::OnBnClickedAdv)
- ON_BN_CLICKED(IDC_DEL_SHOOT, &CEnrollDlg::OnBnClickedDelShoot)
- END_MESSAGE_MAP()
-
-
- // CEnrollDlg ??????
-
- void CEnrollDlg::OnBnClickedShoot()
- {
- // TODO: ??????????????
- if (!m_bFoundFace) return;
- //CFaceAlign* &al = g_faceMngr->align;
- #ifndef COMPILE_ALIGN_COORD
-
- CvRect rc = g_faceMngr->align->m_rcCurFace;
- CvSize fsz = g_webcam.GetFrameSize();
-
- // ???????????????
- if (rc.x < fsz.width * 5 / 100 ||
- rc.x + rc.width > fsz.width * 98 / 100 ||
- rc.y < fsz.height * 5 / 100 ||
- rc.y + g_faceMngr->align->m_dis*16/6 > fsz.height*95/100||
- rc.width < fsz.width / 4 ||
- rc.width > fsz.width * .7 ||
- rc.height < fsz.height *.4 ||
- rc.height > fsz.height*9/10)
- {
- ::AfxMessageBox("??????????????????????");
- return;
- }
-
- #endif
-
- CWnd *pBtn = GetDlgItem(IDC_SHOOT);
- RECT rc1;
- pBtn->GetClientRect(&rc1);
- pBtn->SetWindowText("?????");
- pBtn->SetWindowPos(NULL, 0,0, 70, rc1.bottom-rc1.top, SWP_NOMOVE | SWP_NOZORDER);
- GetDlgItem(IDC_DEL_SHOOT)->ShowWindow(SW_SHOW);
- GetDlgItem(IDC_ENROLL_PIC)->ShowWindow(SW_HIDE);
- GetDlgItem(IDC_IMPORT_MODELS)->ShowWindow(SW_HIDE);
-
- CString title;
- title.Format("pic %d", m_nShootNum++);
- cvNamedWindow(title, 0);
- cvResizeWindow(title, showSz.width, showSz.height);
- cvShowImage(title, faceImg8);
-
- IplImage *pic1 = cvCloneImage(m_frame);
- m_lstPic.AddTail(pic1);
- CvMat *face = cvCloneMat(faceImg8);
- m_lstFace.AddTail(face);
- }
-
- UINT CEnrollDlg::ShowFace()
- {
- faceImg8 = cvCreateMat(faceSz.height, faceSz.width, CV_8UC1);
- m_bFoundFace = false;
-
- while(m_bFaceStarted)
- {
- g_webcam.GetFrame(m_frame);
- m_bFoundFace = g_faceMngr->Pic2NormFace(m_frame, faceImg8, FM_DO_NORM | FM_ALIGN_USE_BUF);
- if (m_bFoundFace)
- {
- cvShowImage("face", faceImg8);
- //m_bFoundFace = true;
- }
- else cvShowImage("face", noface);
- cvWaitKey(1);
- Sleep(100);
- }
-
- cvReleaseMat(&faceImg8);
- // ????cvDestroyWindow
- return 0;
- }
-
- UINT ShowFaceProc( LPVOID pParam )
- {
- CEnrollDlg *pDlg = (CEnrollDlg *)pParam;
- return pDlg->ShowFace();
- }
-
- void CEnrollDlg::OnBnClickedCancel()
- {
- // TODO: ??????????????
- //m_nClassId--; // ????
- Release();
- OnCancel();
- }
-
- void CEnrollDlg::OnBnClickedOk()
- {
- // TODO: ??????????????
- UpdateData(TRUE);
- if (m_nShootNum > 0)
- {
- if (m_strName.IsEmpty())
- {
- ::AfxMessageBox("????????");
- return;
- }
- if (!g_faceMngr->HasTrained())
- {
- CString msg;
- /*msg.Format("?????????????? %s ,?????????????? %s , ?????????\n"
- "????????????????????", g_strPicPath, FACE_REL_PATH);
- if (::AfxMessageBox(msg, MB_YESNO) == IDYES)*/ SaveShootPics(false);
- }
- else SaveShootPics(true);
- }
-
- Release();
- OnOK();
- }
-
- void CEnrollDlg::SaveShootPics( bool bSave2Model )
- {
- CString fn;
- ::CreateDirectory(g_strPicPath, NULL);
- POSITION pos = m_lstPic.GetHeadPosition(), pos1 = m_lstFace.GetHeadPosition();
-
- for (int i = 0; i < m_nShootNum; i++)
- {
- fn.Format("%s_%d.bmp", m_strName, i);
- cvSaveImage(g_strPicPath+fn, m_lstPic.GetNext(pos)); // ?????????????
- if (m_bSaveSmallImg>0)
- {
- ::CreateDirectory(g_strFacePath, NULL);
- cvSaveImage(g_strFacePath+fn, m_lstFace.GetNext(pos1));
- }
-
- if (bSave2Model)
- if (m_bSaveSmallImg>0)
- g_faceMngr->SavePicToModel(g_strFacePath+fn, FM_UNKNOWN_CLASS_ID, FM_DO_NOT_NORM);
- else g_faceMngr->SavePicToModel(g_strPicPath+fn, FM_UNKNOWN_CLASS_ID, FM_DO_NORM);
- }
-
- CString msg;
- msg.Format("???%d????????????%d????", m_nShootNum, g_faceMngr->GetModelCount());
- ::AfxMessageBox(msg, MB_OK | MB_ICONINFORMATION);
- }
-
- void CEnrollDlg::Release()
- {
- if (g_bHasWebcam)
- {
- m_bFaceStarted = false;
- if (m_thrdFace) ::WaitForSingleObject(m_thrdFace->m_hThread, INFINITE); // ??????
- }
-
- CString title;
- POSITION pos = m_lstPic.GetHeadPosition(), pos1 = m_lstFace.GetHeadPosition();
- for (int i = 0; i < m_nShootNum; i++)
- {
- title.Format("pic %d", i);
- cvDestroyWindow(title);
- cvReleaseImage(& m_lstPic.GetNext(pos));
- cvReleaseMat(& m_lstFace.GetNext(pos1));
- }
- m_lstPic.RemoveAll();
- m_lstFace.RemoveAll();
-
- cvReleaseImage(&m_frame);
- cvDestroyWindow("face");
-
- }
-
- void CEnrollDlg::OnBnClickedEnrollPic()
- {
- // TODO: ??????????????
- if (! g_faceMngr->HasTrained())
- {
- ::AfxMessageBox("?????");
- return;
- }
- if (g_bHasWebcam)
- {
- m_bFaceStarted = false;
- if (m_thrdFace) ::WaitForSingleObject(m_thrdFace->m_hThread, INFINITE);
- }
-
- UpdateData(TRUE);
- CFileDialog dlgFile(TRUE, 0, 0, OFN_ALLOWMULTISELECT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY);
- if (dlgFile.DoModal() == IDOK)
- {
- POSITION pos = dlgFile.GetStartPosition();
- int enrollNum = 0;
- while (pos)
- {
- CString path = dlgFile.GetNextPathName(pos);
- DWORD flag = FM_DO_NORM;
- if (m_bSaveSmallImg) flag |= FM_SAVE_NORM_FACE;
- if (g_faceMngr->SavePicToModel(path, FM_UNKNOWN_CLASS_ID, flag)) enrollNum++;
- }
- CString msg;
- msg.Format("???%d????????????%d????", enrollNum, g_faceMngr->GetModelCount());
- ::AfxMessageBox(msg, MB_OK | MB_ICONINFORMATION);
- }
-
- if (g_bHasWebcam)
- {
- m_bFaceStarted = true;
- m_thrdFace = ::AfxBeginThread(ShowFaceProc, this);
- }
- }
-
- void CEnrollDlg::OnBnClickedImportModels()
- {
- // TODO: ??????????????
- CFileDialog dlgFile(TRUE, "frmodel", 0, OFN_NOCHANGEDIR | OFN_HIDEREADONLY,
- "?????? (*.frmodel)|*.frmodel||");
- if (dlgFile.DoModal() == IDOK)
- {
- CString path = dlgFile.GetPathName();
- setlocale(LC_ALL, "Chinese-simplified"); //??????
- ifstream is(path, ios::binary);
- if (!is)
- {
- ::AfxMessageBox(path+" : ?????");
- return;
- }
-
- int readNum = g_faceMngr->ReadModelFromFile(is);
- CString msg;
- msg.Format("???%d??????%d????\n?????????????????",
- readNum, g_faceMngr->GetModelCount());
- ::AfxMessageBox(msg, MB_OK | MB_ICONINFORMATION);
-
- is.close();
- setlocale(LC_ALL, "C");// ??
-
- if (m_nShootNum == 0) OnBnClickedCancel();
- }
- }
-
- void CEnrollDlg::OnBnClickedAdv()
- {
- // TODO: ??????????????
- m_dlgAdv.DoModal();
- }
-
- void CEnrollDlg::OnBnClickedDelShoot()
- {
- // TODO: ??????????????
- CString title;
- title.Format("pic %d", --m_nShootNum);
- cvDestroyWindow(title);
-
- m_lstPic.RemoveTail();
- m_lstFace.RemoveTail();
-
- if(m_nShootNum == 0)
- {
- CWnd *pBtn = GetDlgItem(IDC_SHOOT);
- RECT rc1;
- pBtn->GetClientRect(&rc1);
- pBtn->SetWindowText("??");
- pBtn->SetWindowPos(NULL, 0,0, 35, rc1.bottom-rc1.top, SWP_NOMOVE | SWP_NOZORDER);
- GetDlgItem(IDC_DEL_SHOOT)->ShowWindow(SW_HIDE);
- GetDlgItem(IDC_ENROLL_PIC)->ShowWindow(SW_SHOW);
- GetDlgItem(IDC_IMPORT_MODELS)->ShowWindow(SW_SHOW);
- }
- }