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*/// HMMDemo.cpp : Defines the class behaviors for the application.
46 #include "HMMDemoDoc.h"
47 #include "HMMDemoView.h"
55 static char THIS_FILE[] = __FILE__;
58 /////////////////////////////////////////////////////////////////////////////
61 BEGIN_MESSAGE_MAP(CHMMDemoApp, CWinApp)
62 //{{AFX_MSG_MAP(CHMMDemoApp)
63 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
64 ON_COMMAND(ID_SAVE_CONFIG, OnSaveConfig)
65 ON_COMMAND(ID_LOAD_CONFIG, OnLoadConfig)
67 // Standard file based document commands
68 ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
69 ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
72 /////////////////////////////////////////////////////////////////////////////
73 // CHMMDemoApp construction
75 CHMMDemoApp::CHMMDemoApp()
79 /////////////////////////////////////////////////////////////////////////////
80 // The one and only CHMMDemoApp object
84 /////////////////////////////////////////////////////////////////////////////
85 // CHMMDemoApp initialization
87 BOOL CHMMDemoApp::InitInstance()
89 AfxEnableControlContainer();
91 // Standard initialization
92 // If you are not using these features and wish to reduce the size
93 // of your final executable, you should remove from the following
94 // the specific initialization routines you do not need.
97 Enable3dControls(); // Call this when using MFC in a shared DLL
99 Enable3dControlsStatic(); // Call this when linking to MFC statically
102 // Change the registry key under which our settings are stored.
103 // TODO: You should modify this string to be something appropriate
104 // such as the name of your company or organization.
105 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
107 LoadStdProfileSettings(); // Load standard INI file options (including MRU)
109 // Register the application's document templates. Document templates
110 // serve as the connection between documents, frame windows and views.
112 CSingleDocTemplate* pDocTemplate;
113 pDocTemplate = new CSingleDocTemplate(
115 RUNTIME_CLASS(CHMMDemoDoc),
116 RUNTIME_CLASS(CMainFrame), // main SDI frame window
117 RUNTIME_CLASS(CHMMDemoView));
118 AddDocTemplate(pDocTemplate);
120 // Parse command line for standard shell commands, DDE, file open
121 CCommandLineInfo cmdInfo;
122 ParseCommandLine(cmdInfo);
124 // Dispatch commands specified on the command line
125 if (!ProcessShellCommand(cmdInfo))
128 // The one and only window has been initialized, so show and update it.
129 m_pMainWnd->ShowWindow(SW_SHOW);
130 m_pMainWnd->UpdateWindow();
132 m_dlgHMMParams = new CHMMParams;
133 m_dlgSamplingParams = new CSamplingParams;
134 m_dlgMiscParams = new CMiscParams;
136 //****************Load Settings (stored in registry)*************************************/
137 CString states_string;
139 int numbers[8]; //maximum superstates is 7
141 //read number of HMM states
142 states_string = GetProfileString( "Settings\\HMM", "NumStates", "5 3 6 6 6 3" );
145 sscanf( states_string, "%d %d %d %d %d %d %d %d",
146 &numbers[0], &numbers[1], &numbers[2], &numbers[3],
147 &numbers[4], &numbers[5], &numbers[6], &numbers[7] );
150 m_dlgHMMParams->m_States[0] = numbers[0];
151 for( int i = 0 ; i < m_dlgHMMParams->m_States[0] ; i++ )
153 m_dlgHMMParams->m_States[ i + 1 ] = numbers[i+1] ;
156 //read number of mixtures
157 m_dlgHMMParams->m_NumMix = GetProfileInt("Settings\\HMM", "NumMix", 3 );
159 //read sampling parameters (dctSize, obsSize, delta)
161 m_dlgSamplingParams->m_dctSize.width = GetProfileInt("Settings\\Sampling", "WindowWidth", 12 );
162 m_dlgSamplingParams->m_dctSize.height = GetProfileInt("Settings\\Sampling", "WindowHeight", 12 );
164 m_dlgSamplingParams->m_obsSize.width = GetProfileInt("Settings\\Sampling", "DCTCoeffX", 3 );
165 m_dlgSamplingParams->m_obsSize.height = GetProfileInt("Settings\\Sampling", "DCTCoeffY", 3 );
167 m_dlgSamplingParams->m_delta.width = GetProfileInt("Settings\\Sampling", "DeltaX", 4 );
168 m_dlgSamplingParams->m_delta.height = GetProfileInt("Settings\\Sampling", "DeltaY", 4 );
170 //read scaling params
171 m_dlgMiscParams->m_useWidth = GetProfileInt("Settings\\Scaling", "UseFixedWidth", 0 );
172 m_dlgMiscParams->m_FixedWidth = GetProfileInt("Settings\\Scaling", "FixedWidth", 0 );
173 m_dlgMiscParams->m_useHeight = GetProfileInt("Settings\\Scaling", "UseFixedHeight", 0 );
174 m_dlgMiscParams->m_FixedHeight = GetProfileInt("Settings\\Scaling", "FixedHeight", 0 );
176 m_dlgMiscParams->m_SuppressIntensity = GetProfileInt("Settings\\Scaling", "SuppressIntensity", 1 );
178 //*******************************End Loading Settings *************************************/
183 void CHMMDemoApp::SaveSettings()
185 //****************Save Settings into registry)*************************************/
186 CString states_string;
188 //write number of states
189 states_string.Format("%d", m_dlgHMMParams->m_States[0] );
190 for( int i = 0 ; i < m_dlgHMMParams->m_States[0] ; i++ )
193 add.Format(" %d", m_dlgHMMParams->m_States[ i + 1 ] );
196 //write number of HMM states
197 WriteProfileString( "Settings\\HMM", "NumStates", states_string );
199 //write number of mixtures
200 WriteProfileInt("Settings\\HMM", "NumMix", m_dlgHMMParams->m_NumMix );
202 //write sampling parameters (dctSize, obsSize, delta)
203 WriteProfileInt("Settings\\Sampling", "WindowWidth" , m_dlgSamplingParams->m_dctSize.width );
204 WriteProfileInt("Settings\\Sampling", "WindowHeight", m_dlgSamplingParams->m_dctSize.height );
206 WriteProfileInt("Settings\\Sampling", "DCTCoeffX", m_dlgSamplingParams->m_obsSize.width );
207 WriteProfileInt("Settings\\Sampling", "DCTCoeffY", m_dlgSamplingParams->m_obsSize.height );
209 WriteProfileInt("Settings\\Sampling", "DeltaX", m_dlgSamplingParams->m_delta.width );
210 WriteProfileInt("Settings\\Sampling", "DeltaY", m_dlgSamplingParams->m_delta.height );
211 //*******************************End saving Settings *************************************/
213 //write scaling params
214 WriteProfileInt("Settings\\Scaling", "UseFixedWidth" , m_dlgMiscParams->m_useWidth );
215 WriteProfileInt("Settings\\Scaling", "FixedWidth", m_dlgMiscParams->m_FixedWidth );
217 WriteProfileInt("Settings\\Scaling", "UseFixedHeight", m_dlgMiscParams->m_useHeight);
218 WriteProfileInt("Settings\\Scaling", "FixedHeight", m_dlgMiscParams->m_FixedHeight );
220 WriteProfileInt("Settings\\Scaling", "SuppressIntensity", m_dlgMiscParams->m_SuppressIntensity );
227 /////////////////////////////////////////////////////////////////////////////
228 // CAboutDlg dialog used for App About
230 class CAboutDlg : public CDialog
236 //{{AFX_DATA(CAboutDlg)
237 enum { IDD = IDD_ABOUTBOX };
240 // ClassWizard generated virtual function overrides
241 //{{AFX_VIRTUAL(CAboutDlg)
243 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
248 //{{AFX_MSG(CAboutDlg)
249 // No message handlers
251 DECLARE_MESSAGE_MAP()
254 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
256 //{{AFX_DATA_INIT(CAboutDlg)
260 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
262 CDialog::DoDataExchange(pDX);
263 //{{AFX_DATA_MAP(CAboutDlg)
267 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
268 //{{AFX_MSG_MAP(CAboutDlg)
269 // No message handlers
273 // App command to run the dialog
274 void CHMMDemoApp::OnAppAbout()
280 /////////////////////////////////////////////////////////////////////////////
281 // CHMMDemoApp message handlers
282 // App command to run the dialog
283 void CHMMDemoApp::OnFileOpen()
285 CFileDialog dlg( TRUE, 0, 0, OFN_ALLOWMULTISELECT | OFN_ENABLESIZING |
286 OFN_EXPLORER | OFN_FILEMUSTEXIST,
287 "Face Base Files (*.txt)|*.txt|"
288 "Image Files (*.bmp;*.jpg;*.jpeg)|*.bmp;*.jpg;*.jpeg|"
289 "All Files (*.*)|*.*||", 0 );
290 int buf_size = 1 << 15;
291 char* buffer = (char*)malloc(buf_size + 100);
293 dlg.m_ofn.lpstrFile = buffer;
295 dlg.m_ofn.nMaxFile = buf_size;
296 int result = dlg.DoModal();
300 POSITION pos = dlg.GetStartPosition();
302 if( pos ) /* at least one image in the list */
304 CHMMDemoView* view = GetCameraView();
306 view->SetImageList(0);
308 CStringList* imageList = new CStringList;
312 imageList->AddTail( dlg.GetNextPathName(pos));
315 view->SetImageList( imageList );
317 CWinApp::OpenDocumentFile( imageList->GetHead() );
325 void CHMMDemoApp::OnSettings()
327 CPropertySheet sheet;
328 sheet.AddPage( m_dlgHMMParams );
329 sheet.AddPage( m_dlgSamplingParams );
330 sheet.AddPage( m_dlgMiscParams );
332 if(sheet.DoModal() == IDOK)
334 CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();
335 CHMMDemoDoc* doc = mainframe->GetCameraView()->GetDocument();
336 CFaceBase& base = doc->GetFaceBase();
338 base.SetParams( m_dlgSamplingParams->m_dctSize,
339 m_dlgSamplingParams->m_obsSize,
340 m_dlgSamplingParams->m_delta,
341 m_dlgHMMParams->m_States,
342 m_dlgHMMParams->m_NumMix,
343 m_dlgMiscParams->m_useWidth,
344 m_dlgMiscParams->m_FixedWidth,
345 m_dlgMiscParams->m_useHeight,
346 m_dlgMiscParams->m_FixedHeight,
347 m_dlgMiscParams->m_SuppressIntensity);
353 void CHMMDemoApp::OnSaveConfig()
355 CFileDialog dlg( FALSE, 0, 0, /*OFN_ALLOWMULTISELECT | */OFN_ENABLESIZING |
357 "Config file (*.txt) |*.txt|", 0 );
360 if ( dlg.DoModal() == IDOK )
362 int res = SaveConfig( dlg.GetFileName() );
366 void CHMMDemoApp::OnLoadConfig()
368 CFileDialog dlg( TRUE, 0, 0, /*OFN_ALLOWMULTISELECT | */OFN_ENABLESIZING |
370 "Config file (*.txt) |*.txt|", 0 );
373 if ( dlg.DoModal() == IDOK )
375 int res = LoadConfig( dlg.GetFileName(), true );
381 int CHMMDemoApp::ExitInstance()
384 return CWinApp::ExitInstance();
387 //loads config file without clearing base info
388 int CHMMDemoApp::LoadConfig(const char *filename, bool synchronize_base)
390 CvSize dctSize, delta, obsSize;
394 FILE* file = fopen( filename, "r" );
395 if ( !file ) return 0;
398 m_dlgMiscParams->m_useWidth = FALSE;
399 m_dlgMiscParams->m_FixedWidth = 0;
400 m_dlgMiscParams->m_useHeight = FALSE;
401 m_dlgMiscParams->m_FixedHeight = 0;
402 m_dlgMiscParams->m_SuppressIntensity = FALSE;
404 while ( !feof(file) )
406 fscanf( file, "%s %d", str, &value );
408 //here we allow config file to be not hard defined
409 //Is it useful? - I don't know
410 if ( strcmp( "WindowWidth", str ) == 0 )
412 dctSize.width = value;
414 else if ( strcmp( "WindowHeight", str ) == 0 )
416 dctSize.height = value;
418 else if ( strcmp( "DeltaX", str ) == 0 )
422 else if ( strcmp( "DeltaY", str ) == 0 )
424 delta.height = value;
426 else if ( strcmp( "DCTcoeffX", str ) == 0 )
428 obsSize.width = value;
430 else if ( strcmp( "DCTcoeffY", str ) == 0 )
432 obsSize.height = value;
434 else if ( strcmp( "SuperStates", str ) == 0 )
436 m_dlgHMMParams->m_States[0] = value;
438 else if ( strcmp( "States", str ) == 0 )
441 ASSERT( m_dlgHMMParams->m_States[0] );//superstates must be already loaded
442 m_dlgHMMParams->m_States[1] = value;
443 for(int i = 1; i < m_dlgHMMParams->m_States[0]; i++ )
445 fscanf(file, "%d", &m_dlgHMMParams->m_States[i+1] );
448 else if ( strcmp( "NumMixtures", str ) == 0 )
450 m_dlgHMMParams->m_NumMix = value;
452 else if ( strcmp( "FIXED_WIDTH", str ) == 0 )
454 m_dlgMiscParams->m_useWidth = TRUE;
455 m_dlgMiscParams->m_FixedWidth = value;
457 else if ( strcmp( "FIXED_HEIGHT", str ) == 0 )
459 m_dlgMiscParams->m_useHeight = TRUE;
460 m_dlgMiscParams->m_FixedHeight = value;
462 else if ( strcmp( "SUPPRESS_INTENSITY", str ) == 0 )
464 m_dlgMiscParams->m_SuppressIntensity = TRUE;
472 //check correctness of loaded file
473 if ( (dctSize.width < obsSize.width)||
474 (dctSize.height < obsSize.height) )
477 DoMessageBox( "Wrong parameters in config file\nNew parameters were not loaded", MB_OK|MB_ICONSTOP, 0 );
480 //initialize application variables
481 m_dlgSamplingParams->m_dctSize = dctSize;
482 m_dlgSamplingParams->m_obsSize = obsSize;
483 m_dlgSamplingParams->m_delta = delta;
487 if (synchronize_base)
489 CMainFrame* mainframe = (CMainFrame*)AfxGetMainWnd();
490 CHMMDemoDoc* doc = mainframe->GetCameraView()->GetDocument();
491 CFaceBase& base = doc->GetFaceBase();
493 return base.SetParams( //sampling params
494 m_dlgSamplingParams->m_dctSize,
495 m_dlgSamplingParams->m_obsSize,
496 m_dlgSamplingParams->m_delta,
498 m_dlgHMMParams->m_States,
499 m_dlgHMMParams->m_NumMix,
500 //image scaling params
501 m_dlgMiscParams->m_useWidth,
502 m_dlgMiscParams->m_FixedWidth,
503 m_dlgMiscParams->m_useHeight,
504 m_dlgMiscParams->m_FixedHeight,
505 //intensity suppression
506 m_dlgMiscParams->m_SuppressIntensity, 1 );
510 int CHMMDemoApp::SaveConfig(const char* filename)
512 FILE* file = fopen( filename, "wt" );
513 if (!file) { ASSERT(0); return 0; }
515 fprintf( file, "***********Sampling params************\n" );
516 fprintf( file, "WindowWidth %d\n" , m_dlgSamplingParams->m_dctSize.width );
517 fprintf( file, "WindowHeight %d\n", m_dlgSamplingParams->m_dctSize.height );
518 fprintf( file, "DeltaX %d\n" , m_dlgSamplingParams->m_delta.width );
519 fprintf( file, "DeltaY %d\n", m_dlgSamplingParams->m_delta.height );
520 fprintf( file, "DCTcoeffX %d\n" , m_dlgSamplingParams->m_obsSize.width );
521 fprintf( file, "DCTcoeffY %d\n", m_dlgSamplingParams->m_obsSize.height );
523 fprintf( file, "***************HMM params*************\n" );
524 fprintf( file, "SuperStates %d\n", m_dlgHMMParams->m_States[0] );
525 fprintf( file, "States");
526 for( int i = 0; i < m_dlgHMMParams->m_States[0]; i++ )
528 fprintf( file, " %d", m_dlgHMMParams->m_States[i+1] );
530 fprintf( file, "\n" );
531 fprintf( file, "NumMixtures %d\n", m_dlgHMMParams->m_NumMix );
533 fprintf( file, "***************Misc. params*************\n" );
534 if( m_dlgMiscParams->m_useWidth )
535 fprintf( file, "FIXED_WIDTH %d\n", m_dlgMiscParams->m_FixedWidth );
536 if( m_dlgMiscParams->m_useHeight )
537 fprintf( file, "FIXED_HEIGHT %d\n", m_dlgMiscParams->m_FixedHeight );
538 if( m_dlgMiscParams->m_SuppressIntensity )
539 fprintf( file, "SUPPRESS_INTENSITY %d\n", m_dlgMiscParams->m_SuppressIntensity );