PageRenderTime 113ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/ facerecog --username xjed09/frtest/RecogDlg.cpp

http://facerecog.googlecode.com/
C++ | 287 lines | 223 code | 46 blank | 18 comment | 37 complexity | 59cecf0dce92bd76e16eed849579bbdc MD5 | raw file
  1. // RecogDlg.cpp : ????
  2. //
  3. #include "stdafx.h"
  4. #include "frtest.h"
  5. #include "RecogDlg.h"
  6. #include "WndUtils.h"
  7. #include "ProjDefs.h"
  8. // CRecogDlg ???
  9. IMPLEMENT_DYNAMIC(CRecogDlg, CDialog)
  10. CRecogDlg::CRecogDlg(CWnd* pParent /*=NULL*/)
  11. : CDialog(CRecogDlg::IDD, pParent)
  12. , m_bUseBuf(FALSE)
  13. {
  14. m_thrdMatch = NULL;
  15. noface = cvLoadImage("noface.bmp");
  16. resss = cvLoadImage("result.bmp");
  17. }
  18. CRecogDlg::~CRecogDlg()
  19. {
  20. cvReleaseImage(&noface);
  21. cvReleaseImage(&resss);
  22. }
  23. void CRecogDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. DDX_Check(pDX, IDC_USEBUF, m_bUseBuf);
  27. }
  28. BEGIN_MESSAGE_MAP(CRecogDlg, CDialog)
  29. ON_BN_CLICKED(IDCANCEL, &CRecogDlg::OnBnClickedCancel)
  30. //ON_NOTIFY(NM_CLICK, IDC_INFO, &CRecogDlg::OnNMClickFilepath)
  31. ON_BN_CLICKED(IDC_PIC_RECOG, &CRecogDlg::OnBnClickedPicRecog)
  32. ON_BN_CLICKED(IDC_USEBUF, &CRecogDlg::OnBnClickedUsebuf)
  33. END_MESSAGE_MAP()
  34. BOOL CRecogDlg::OnInitDialog()
  35. {
  36. CDialog::OnInitDialog();
  37. faceSz = g_faceMngr->m_faceSz;
  38. showSz = faceSz;
  39. if (showSz.height != 160) // (160*130)??????????????????????
  40. showSz *= (160.0/showSz.height);
  41. if (showSz.width > 130)
  42. showSz *= (130.0/showSz.width);
  43. if (g_bHasWebcam)
  44. {
  45. m_bFoundFace = false;
  46. SetDlgItemText(IDC_INFO, " ????\"no face found\"????????????");
  47. m_frame = cvCreateImage(g_webcam.GetFrameSize(), IPL_DEPTH_8U, 3);
  48. m_bMatchStarted = true;
  49. m_thrdMatch = ::AfxBeginThread(ShowMatchProc, this);
  50. GetDlgItem(IDC_USEBUF)->ShowWindow(SW_SHOW);
  51. }
  52. else
  53. {
  54. SetDlgItemText(IDC_INFO, "");
  55. GetDlgItem(IDC_USEBUF)->EnableWindow(FALSE);
  56. }
  57. InitWindows();
  58. RECT rc;
  59. GetWindowRect(&rc);
  60. SetWindowPos(NULL, rc.left + 30, rc.top + 300, 0,0, SWP_NOSIZE | SWP_NOZORDER);
  61. ShowWindow(SW_SHOWNORMAL);
  62. return TRUE;
  63. }
  64. void CRecogDlg::InitWindows()
  65. {
  66. CWnd *pWnd = GetDlgItem(IDC_QUERYIMG);
  67. EmbedCvWindow(pWnd->m_hWnd, "query", showSz.width, showSz.height);
  68. if (noface) cvShowImage("query", noface);
  69. CWnd *pWnd1 = GetDlgItem(IDC_MATCHIMG);
  70. EmbedCvWindow(pWnd1->m_hWnd, "match", showSz.width, showSz.height);
  71. if (resss) cvShowImage("match", resss);
  72. }
  73. UINT ShowMatchProc( LPVOID pParam )
  74. {
  75. CRecogDlg *pDlg = (CRecogDlg *)pParam;
  76. return pDlg->ShowMatch();
  77. }
  78. void CRecogDlg::ShowMatchRun()
  79. {
  80. // TODO: ??????????????
  81. bool res = g_faceMngr->PicRecognize(faceImg8, FM_DO_NOT_NORM, &matchInfo);
  82. if (res)
  83. {
  84. CString msg, fn, distname;
  85. int i;
  86. strFd = matchInfo.picPath;
  87. i = strFd.ReverseFind('\\');
  88. fn = strFd.Right(strFd.GetLength() - i - 1);
  89. strFd = strFd.Left(i+1);
  90. if (! m_bMatchStarted) return; // ???????????????Exit?
  91. double score = matchInfo.dist;
  92. if (g_faceMngr->m_modelSz > 1)
  93. {
  94. score = -score;
  95. distname = "???(-1~1)";
  96. }
  97. else distname = "????(0~)";
  98. msg.Format("?????%s?%s?%f\n???????%s%s",
  99. ::FindName(fn), distname, score, strFd, fn);
  100. SetDlgItemText(IDC_INFO, msg);
  101. IplImage *ming = cvLoadImage(matchInfo.picPath);
  102. if (!ming) ming = cvLoadImage("noimage.bmp"); // ????????
  103. if (!ming)
  104. {
  105. ming = cvCreateImage(faceSz, 8, 1);
  106. cvSet(ming, cvScalar(255));
  107. }
  108. CvSize msz = cvGetSize(ming); // ??????
  109. float showscale = (float)max(msz.width, msz.height)/300;
  110. if (showscale < .3) msz *= 2;
  111. else if (showscale > 1) msz /= showscale;
  112. if (! m_bMatchStarted) return;
  113. CWnd *pWnd = GetDlgItem(IDC_MATCHIMG); // ?????????
  114. pWnd->SetWindowPos(NULL, 0,0, msz.width, msz.height, SWP_NOZORDER | SWP_NOMOVE);
  115. SetWindowPos(NULL, 0, 0, faceSz.width + 150 + msz.width,
  116. msz.height + 130, SWP_NOZORDER | SWP_NOMOVE);
  117. cvResizeWindow("match", msz.width, msz.height);
  118. cvShowImage("match", ming);
  119. cvReleaseImage(&ming);
  120. }
  121. }
  122. void CRecogDlg::OnBnClickedCancel()
  123. {
  124. // TODO: ??????????????
  125. Release();
  126. OnCancel();
  127. }
  128. void CRecogDlg::Release()
  129. {
  130. if (g_bHasWebcam)
  131. {
  132. m_bMatchStarted = false;
  133. if (m_thrdMatch) ::WaitForSingleObject(m_thrdMatch->m_hThread, INFINITE);
  134. cvReleaseImage(&m_frame);
  135. }
  136. cvDestroyWindow("query");
  137. cvDestroyWindow("match");
  138. cvDestroyWindow("face");
  139. }
  140. void CRecogDlg::OnBnClickedPicRecog()
  141. {
  142. // ????
  143. if (g_bHasWebcam)
  144. {
  145. m_bMatchStarted = false;
  146. if (m_thrdMatch) ::WaitForSingleObject(m_thrdMatch->m_hThread, INFINITE);
  147. }
  148. // ????
  149. CFileDialog fileDlg(TRUE, 0, 0, OFN_NOCHANGEDIR | OFN_HIDEREADONLY);
  150. int ret = fileDlg.DoModal();
  151. if (ret != IDOK) return;
  152. CString path = fileDlg.GetPathName();
  153. IplImage *queryimg = cvLoadImage(path, CV_LOAD_IMAGE_COLOR);
  154. if (!queryimg) return;
  155. // ??query img??
  156. CvSize qsz = cvGetSize(queryimg);
  157. float showscale = (float)max(qsz.width, qsz.height)/300;
  158. if (showscale < .3) qsz *= 2;
  159. else if (showscale > 1) qsz /= showscale;
  160. // ??query img
  161. CWnd *pWndq = GetDlgItem(IDC_QUERYIMG);
  162. pWndq->SetWindowPos(NULL, 0,0, qsz.width, qsz.height, SWP_NOZORDER | SWP_NOMOVE);
  163. cvResizeWindow("query", qsz.width, qsz.height);
  164. cvShowImage("query", queryimg);
  165. // ??
  166. bool res = g_faceMngr->PicRecognize(queryimg, FM_DO_NORM, &matchInfo);
  167. cvReleaseImage(&queryimg);
  168. if (!res) return;
  169. // ??query img??????
  170. cvNamedWindow("face", 0);
  171. cvResizeWindow("face", showSz.width, showSz.height);
  172. cvShowImage("face", g_faceMngr->tfaceImg8);
  173. // ????
  174. CString msg, fn, distname;
  175. int i;
  176. strFd = matchInfo.picPath;
  177. i = strFd.ReverseFind('\\');
  178. fn = strFd.Right(strFd.GetLength() - i - 1);
  179. strFd = strFd.Left(i+1);
  180. double score = matchInfo.dist;
  181. if (g_faceMngr->m_modelSz > 1)
  182. {
  183. score = -score;
  184. distname = "???(-1~1)";
  185. }
  186. else distname = "????(0~)";
  187. msg.Format("?????%s?%s?%f\n???????%s%s",
  188. ::FindName(fn), distname, score, strFd, fn);
  189. SetDlgItemText(IDC_INFO, msg);
  190. // ??????
  191. IplImage *mimg = cvLoadImage(matchInfo.picPath);
  192. if (!mimg) mimg = cvLoadImage("noimage.bmp");
  193. if (!mimg)
  194. {
  195. mimg = cvCreateImage(qsz, 8, 1);
  196. cvSet(mimg, cvScalar(255));
  197. }
  198. // ????????
  199. CvSize msz = cvGetSize(mimg); // ??????
  200. showscale = (float)max(msz.width, msz.height)/300;
  201. if (showscale < .3) msz *= 2;
  202. else if (showscale > 1) msz /= showscale;
  203. // ?????????????
  204. CWnd *pWndm = GetDlgItem(IDC_MATCHIMG);
  205. pWndm->SetWindowPos(NULL, qsz.width + 50, 79, msz.width, msz.height, SWP_NOZORDER);
  206. SetWindowPos(NULL, 0, 0, qsz.width + 100 + msz.width,
  207. max(qsz.height, msz.height) + 130, SWP_NOZORDER | SWP_NOMOVE);
  208. GetDlgItem(IDC_INFO)->
  209. SetWindowPos(NULL, 0,0, qsz.width + 20 + msz.width, 30, SWP_NOZORDER | SWP_NOMOVE);
  210. cvResizeWindow("match", msz.width, msz.height);
  211. cvShowImage("match", mimg);
  212. cvReleaseImage(&mimg);
  213. }
  214. UINT CRecogDlg::ShowMatch()
  215. {
  216. faceImg8 = cvCreateMat(faceSz.height, faceSz.width, CV_8UC1);
  217. m_bFoundFace = false;
  218. while(m_bMatchStarted)
  219. {
  220. g_webcam.GetFrame(m_frame);
  221. // ???????????????????????????????????????
  222. DWORD flag = FM_DO_NORM;
  223. if (m_bUseBuf) flag |= FM_ALIGN_USE_BUF;
  224. m_bFoundFace = g_faceMngr->Pic2NormFace(m_frame, faceImg8, flag);
  225. if (m_bFoundFace) // ???????????????? ShowMatchRun ????
  226. {
  227. cvShowImage("query", faceImg8);
  228. m_bFoundFace = true;
  229. ShowMatchRun();
  230. }
  231. else cvShowImage("query", noface);
  232. Sleep(100);
  233. }
  234. cvReleaseMat(&faceImg8);
  235. return 0;
  236. }
  237. void CRecogDlg::OnBnClickedUsebuf()
  238. {
  239. // TODO: ??????????????
  240. UpdateData();
  241. }