1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
40 //M*/// MainFrm.cpp : implementation of the CMainFrame class
47 #include "HMMDemoDoc.h"
52 static char THIS_FILE[] = __FILE__;
55 /////////////////////////////////////////////////////////////////////////////
58 IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
60 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
61 //{{AFX_MSG_MAP(CMainFrame)
63 ON_UPDATE_COMMAND_UI(ID_CAPTURE, OnUpdateCapture)
64 ON_COMMAND(ID_CAPTURE, OnCapture)
65 ON_UPDATE_COMMAND_UI(ID_CAPOPTIONS, OnUpdateCapOptions)
66 ON_COMMAND(ID_CAPOPTIONS, OnCapOptions)
67 ON_UPDATE_COMMAND_UI(ID_CAPFORMAT, OnUpdateCapFormat)
68 ON_COMMAND(ID_CAPFORMAT, OnCapFormat)
69 ON_COMMAND(ID_ADD_OBJ, OnAddObj)
70 ON_UPDATE_COMMAND_UI(ID_ADD_OBJ, OnUpdateAddObj)
71 ON_COMMAND(ID_REMOVE_OBJ, OnRemoveObj)
72 ON_UPDATE_COMMAND_UI(ID_REMOVE_OBJ, OnUpdateRemoveObj)
73 ON_COMMAND(ID_ZOOMIN, OnZoomIn)
74 ON_COMMAND(ID_ZOOMOUT, OnZoomOut)
75 ON_COMMAND(ID_SETINFO, OnChangeBaseParams)
76 ON_COMMAND(ID_TRAIN, OnTrain)
77 ON_UPDATE_COMMAND_UI(ID_TRAIN, OnUpdateTrain)
78 ON_COMMAND(ID_RECOG, OnRecognize)
79 ON_UPDATE_COMMAND_UI(ID_RECOG, OnUpdateRecog)
80 ON_COMMAND(ID_SELECTALL, OnSelectAll)
81 ON_COMMAND(ID_DEL_HMM, OnDelHmm)
82 ON_COMMAND(ID_ADD_TEST, OnAddTest)
83 ON_UPDATE_COMMAND_UI(ID_ADD_TEST, OnUpdateAddTest)
84 ON_COMMAND(ID_TEST_FOLDER, OnTestFolder)
85 ON_COMMAND(ID_RECOBASE, OnRecobase)
86 ON_UPDATE_COMMAND_UI(ID_RECOBASE, OnUpdateRecobase)
87 ON_COMMAND(ID_SETTINGS, OnSettings)
91 static UINT indicators[] =
93 ID_SEPARATOR, // status line indicator
99 /////////////////////////////////////////////////////////////////////////////
100 // CMainFrame construction/destruction
102 CMainFrame::CMainFrame()
107 CMainFrame::~CMainFrame()
111 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
113 if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
116 if( !m_wndToolBar.CreateEx(this) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
119 if( !m_wndReBar.Create(this) ||
120 !m_wndReBar.AddBar(&m_wndToolBar))
123 if (!m_wndStatusBar.Create(this) ||
124 !m_wndStatusBar.SetIndicators(indicators,
125 sizeof(indicators)/sizeof(UINT)))
128 m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
129 CBRS_TOOLTIPS | CBRS_FLYBY);
135 BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
136 CCreateContext* pContext)
138 m_wndSplitter.CreateStatic(this,1,2);
140 m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CImageBaseView),CSize(300,100),pContext);
141 m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CHMMDemoView),CSize(300,100),pContext);
142 // m_wndSplitter.CreateView(0,2,RUNTIME_CLASS(CTestImageBaseView),CSize(300,100),pContext);
147 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
149 if( !CFrameWnd::PreCreateWindow(cs) )
155 CImageBaseView* CMainFrame::GetImageBaseView()
157 return (CImageBaseView*)m_wndSplitter.GetPane(0,0);
160 CHMMDemoView* CMainFrame::GetCameraView()
162 return (CHMMDemoView*)m_wndSplitter.GetPane(0,1);
166 void CMainFrame::OnUpdateCapture(CCmdUI* pCmdUI)
168 CHMMDemoView* view = GetCameraView();
174 enable = view->Camera().IsInitialized();
175 press = view->Camera().IsRunning();
178 pCmdUI->Enable( enable );
179 pCmdUI->SetCheck( press );
182 void CMainFrame::OnCapture()
184 CHMMDemoView* view = GetCameraView();
186 if( view && view->Camera().IsInitialized())
188 if( view->Camera().IsRunning())
190 view->Camera().Stop();
194 view->SetImageList(0);
195 view->Camera().Start();
200 void CMainFrame::OnUpdateCapOptions(CCmdUI* pCmdUI)
202 CHMMDemoView* view = GetCameraView();
203 pCmdUI->Enable( view != 0 );
206 void CMainFrame::OnCapOptions()
208 CHMMDemoView* view = GetCameraView();
212 view->Camera().VideoSourceDlg();
213 view->InvalidateRect(0);
217 void CMainFrame::OnUpdateCapFormat(CCmdUI* pCmdUI)
219 CHMMDemoView* view = GetCameraView();
220 pCmdUI->Enable( view != 0 );
223 void CMainFrame::OnCapFormat()
225 CHMMDemoView* view = GetCameraView();
229 view->Camera().VideoFormatDlg();
230 view->InvalidateRect(0);
235 void CMainFrame::OnUpdateAddObj(CCmdUI* pCmdUI)
237 CHMMDemoView* view = GetCameraView();
242 enable = !view->GetSelection().IsRectEmpty();
244 pCmdUI->Enable( enable );
248 void CMainFrame::OnAddObj()
250 CHMMDemoView* view = GetCameraView();
251 CHMMDemoDoc* doc = GetHMMDoc();
255 CCamera& camera = view->Camera();
257 doc->AddObj( camera.GetFrame(), view->GetSelection(), view->GetImageList() );
262 //try to write new file to directory
263 CString GetFreeFilename( CString folder, CString base_name )
271 for( ; i < 10000; i++ )
274 //GetPersonFullImageName( m_folder, m_folder.GetLength(), filename, path );
275 full_name.Format( "%s\\%s%04d.bmp", folder, base_name, i );
276 f = fopen( full_name, "rb" );
282 ASSERT(0); //so many images already exist
286 // try to open for writing. If success, output name
287 f = fopen( full_name, "wb" );
299 void CMainFrame::OnAddTest()
301 CHMMDemoView* camera_view = GetCameraView();
302 CImageBaseView* view = GetImageBaseView();
303 CHMMDemoDoc* doc = GetHMMDoc();
307 CRect m_sel = camera_view->GetSelection();
308 CCamera& camera = camera_view->Camera();
312 //get selected person name
313 CPerson* person = doc->GetFaceBase().GetPerson( view->GetPersonIndex() );
314 CString name = person->GetName();
316 //add image which is in view
317 CString filename = GetFreeFilename( camera_view->GetTestPath(), name );
319 new_img.CopyOf( camera.GetFrame() );
320 IplImage* iplimage = new_img.GetImage();
324 MessageBox("No image was selected!");
328 cvSetImageROI( iplimage, RectToCvRect( m_sel ));
330 tofile.CopyOf( iplimage );
331 tofile.Save( filename );
334 CStringList* imageList = camera_view->GetImageList();
336 if ( imageList && (imageList->GetCount() > 1) )
339 POSITION pos = imageList->GetHeadPosition();
340 imageList->GetNext( pos );
345 img.Load( imageList->GetNext( pos ), 1 );
348 img.Save( GetFreeFilename( camera_view->GetTestPath(), name ) );
351 MessageBox("Images were added to test base");
361 void CMainFrame::OnUpdateRemoveObj(CCmdUI* pCmdUI)
363 CImageBaseView* view = GetImageBaseView();
368 enable = view->GetActive() >= 0;
370 pCmdUI->Enable( enable );
373 void CMainFrame::OnRemoveObj()
375 CImageBaseView* view = GetImageBaseView();
376 CHMMDemoDoc* doc = GetHMMDoc();
380 int active = view->GetActive();
381 int person_index = view->GetPersonIndex();
385 if( doc->RemoveObj( person_index, active ))
395 void CMainFrame::OnZoomIn()
397 CImageBaseView* view = GetImageBaseView();
402 void CMainFrame::OnZoomOut()
404 CImageBaseView* view = GetImageBaseView();
409 void CMainFrame::OnChangeBaseParams()
411 CHMMDemoDoc* doc = GetHMMDoc();
415 doc->ChangeBaseParams();
419 void CMainFrame::OnTrain()
422 //if 1 person selected - train its HMM
423 //if all base in view - train all untrained persons
424 CHMMDemoDoc* doc = GetHMMDoc();
425 CImageBaseView* base_view = GetImageBaseView();
427 if( doc && base_view )
429 //int view_mode = base_view->GetMode();
430 CFaceBase& base = doc->GetFaceBase();
432 if( base_view->GetPersonIndex() >= 0 )
434 base.TrainPerson( base_view->GetPersonIndex(), true );
437 base.TrainAll( TRAIN_UNTRAINED );
441 void CMainFrame::OnRecognize()
444 CHMMDemoView* view = GetCameraView();
445 CHMMDemoDoc* doc = GetHMMDoc();
446 CImageBaseView* baseview = GetImageBaseView();
448 if( doc && view && baseview )
450 CFaceBase& base = doc->GetFaceBase();
451 CCamera& camera = view->Camera();
454 if (view->GetSelection().IsRectEmpty() )
456 MessageBox( "Specify face region", NULL, MB_ICONERROR );
460 //decide if single recognition or batch mode
461 CStringList* image_list = view->GetImageList();
462 if ( image_list && (image_list->GetCount() > 1) )
464 //perform batch recognition
465 int result = base.RecognizeBatch( image_list );
470 int result = base.RecognizePerson( camera.GetFrame(), view->GetSelection(),
475 MessageBox( "Not all persons are trained", NULL, MB_ICONERROR );
479 CString message = "";
480 for( int i = 0 ; i < result; i++ )
482 CPerson* person = base.GetPerson( ranged[i]);
483 message += person->GetName() + "\n";
486 baseview->SwitchMode(ranged[0], false);
487 MessageBox( (LPCTSTR)message, NULL, MB_ICONEXCLAMATION );
494 void CMainFrame::OnUpdateRecog(CCmdUI* pCmdUI)
496 CHMMDemoView* view = GetCameraView();
497 CRect rect = view->GetSelection();
499 pCmdUI->Enable( rect.Width() && rect.Height() );
503 void CMainFrame::OnUpdateTrain(CCmdUI* pCmdUI)
505 CImageBaseView* view = GetImageBaseView();
510 //int person_index = view->GetPersonIndex();
511 CHMMDemoDoc* doc = GetHMMDoc();
512 int num_person = doc->GetFaceBase().GetPersonList().GetCount();
513 if( doc && num_person >= 0 )
516 //CFaceBase& base = doc->GetFaceBase();
517 //CPerson* person = base.GetPerson(person_index);
518 //enable = person && person->GetImgList().GetCount() > 0;
522 pCmdUI->Enable( enable );
526 void CMainFrame::OnSelectAll()
528 CHMMDemoView* view = GetCameraView();
532 view->SetSelection(0);
536 void CMainFrame::OnDelHmm()
538 CHMMDemoDoc* doc = GetHMMDoc();
539 CImageBaseView* view = GetImageBaseView();
543 int person_index = view->GetPersonIndex();
544 int result = MessageBox( person_index >= 0 ? "Delete HMM info for current person?" :
545 "Delete HMM info for the whole base?", "",
546 MB_YESNO | MB_ICONQUESTION );
547 if( result == IDYES ) doc->DeleteHMMInfo( person_index );
552 void CMainFrame::OnUpdateAddTest(CCmdUI* pCmdUI)
554 CImageBaseView* view = GetImageBaseView();
555 CHMMDemoView* camera_view = GetCameraView();
559 int person_index = view->GetPersonIndex();
560 CString path = camera_view->GetTestPath();
562 pCmdUI->Enable( (person_index >= 0) && (!path.IsEmpty()) );
566 void CMainFrame::OnTestFolder()
568 CHMMDemoView* view = GetCameraView();
575 bi.pszDisplayName = buffer;
576 bi.lpszTitle = "Choose directory for test images" ;
581 LPITEMIDLIST il = SHBrowseForFolder( &bi );
585 if ( SHGetPathFromIDList( il, path ) )
588 view->SetTestPath( path );
592 void CMainFrame::OnRecobase()
594 CImageBaseView* base_view = GetImageBaseView();
595 CFaceBase& original_base = base_view->GetDocument()->GetFaceBase();
597 CFileDialog dlg( TRUE, 0, 0, OFN_ENABLESIZING |
598 OFN_EXPLORER | OFN_FILEMUSTEXIST,
599 "Face Base Files (*.txt)|*.txt|", 0 );
600 int buf_size = 1 << 15;
601 char* buffer = (char*)malloc(buf_size + 100);
603 dlg.m_ofn.lpstrFile = buffer;
605 dlg.m_ofn.nMaxFile = buf_size;
606 int result = dlg.DoModal();
612 base.SetFileName( dlg.GetFileName() );
617 original_base.RecognizeOtherBase( &base );
621 void CMainFrame::OnUpdateRecobase(CCmdUI* pCmdUI)
623 // TODO: Add your command update UI handler code here
624 CImageBaseView* base_view = GetImageBaseView();
625 CFaceBase& base = base_view->GetDocument()->GetFaceBase();
626 POSITION pos = base.GetPersonList().GetHeadPosition();
629 CPerson* pers = base.GetPersonList().GetNext(pos);
630 if( !pers->IsTrained() ) { pCmdUI->Enable(FALSE); return; }
632 pCmdUI->Enable(TRUE);
635 void CMainFrame::OnSettings()
637 CHMMDemoApp* app = (CHMMDemoApp*)AfxGetApp();
638 app->OnSettings();// TODO: Add your command handler code here