⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 load.cpp

📁 face recognition test source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"

#include "CAAM.h"
#include "CMatPtr.h"
#include "utilcpp.h"
#include "str.h"
#include "str2.h"
#include "def.h"
#include "error.h"
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>

/*File State definitions*/
#define	FS_UNKNOWN				0
#define	FS_COMMENT				1
#define  FS_SECTION				100
#define  FSS_TRAINING			FS_SECTION + 1
#define  FSS_RECONS				FS_SECTION + 2
#define  FSS_RECOG				FS_SECTION + 3
#define  FSS_CATEG_LVQ			FS_SECTION + 4
#define  FSS_MEET					FS_SECTION + 6
#define 	FS_COMMAND				200
#define  FSC_LEIGEN				FS_COMMAND + 1
#define  FSC_LMEET				FS_COMMAND + 2
#define  FSC_SEIGENDB			FS_COMMAND + 3
#define  FSC_SEIGEN				FS_COMMAND + 4
#define  FSC_SCMP_MEET			FS_COMMAND + 5
#define  FSC_SCMP_RECONS		FS_COMMAND + 6
#define  FSC_SCMP_RECOG			FS_COMMAND + 7
#define  FSC_SREPORT				FS_COMMAND + 8

#define	FS2_BEGIN				1
#define	FS2_END					2
#define	FS2_PROCESSING			3
#define	FS2_CMD					4

PFILECMD AppendCmd(PFILESTATE fs);
void FreeCmd(PFILESTATE fs);

/**************************************************************************************************
* Read the project file. It has the following structure:
* 
* [TRAINING #eigenvect]
* file name 1				name of the face's represented by the file 1
* file name 2 etc.
* [END]
*
* where file name is a mac full path name such as : 
* 			Quadra 950:Development:Sami Romdhani MSc:Face DataBase:Training:agj-01.ppm
*		#eigenvect is the number of eigenvector that you want the PCA to compute
*
* each line beginning by ';' is a comment
*
* it returns 0 if no failure occured, otherwize a negative value.
**************************************************************************************************/

int CAAM::Load(char *file)
	{
	CException *excpt;
	int ret;
	FILE *pfh;
  	size_t filelen;											
  	char str_line[255];
  	FILESTATE fs;
  	
  	Msg(1, str_PROJECT_LOADING, file);
   	
	pfh = fopen(file, "r");									/*Open the file*/
	if (pfh == NULL)
		{
		Msg(-1, str_ERR_FILE_OPEN, errno);
		return ERR_FILE_OPEN;
		}
	fseek(pfh, 0, SEEK_END);								/*Calculates the file lenght*/
	filelen = ftell(pfh);
	fseek(pfh, 0, SEEK_SET);
	
	TRY
		{
		fs.state = FS_UNKNOWN;
	  	fs.num_line = 0;
	  	fs.args[0] = 0;
	  	fs.data[0] = 0;
	  	/*fs.pnext = NULL;*/
	  	fs.num_cmd = 0;
		while (fgets(str_line, sizeof(str_line), pfh) != NULL)
			{                                            /*loop for all lines of the file*/
			fs.num_line++;
			
			ret = read_line(str_line, &fs);
			if (ret < 0)
				break;
			
			ret = process_line(&fs);
			if (ret < 0)
				break;
			}	/*end of while (processing of a line of the file)*/
		if (feof(pfh))
			{	/*end of file reached*/
			Msg(-1, str_LOADED, fs.num_line);
			}
		if (ferror(pfh))
			{
			Msg(-1, "Error reading at line %i", fs.num_line+1);
			}
		}
	CATCH(CException, excpt)
		{
		return -1;
		}
	END_CATCH
	
   fclose(pfh);
   
	return 0; 
	}
		
int CAAM::read_line(char *line, PFILESTATE fs)
	{
  	int ret;
  	char *str_1, *str_2, *str_line;

	if (fs->state2 == FS2_BEGIN || fs->state2 == FS2_CMD)
		fs->state2 = FS2_PROCESSING;

	str_line = line;
	while (str_line[0] == ' ' || str_line[0] == '\t')
		str_line++;
	
	if (str_line[0] == chr_PROJ_COMMENT || str_line[0] == '\n')
		{
		fs->state = FS_COMMENT;
		return 0;
		}
	
	if (str_line[0] == '[')
		{															/*just encountered a new section, or its end*/
	   str_1 = strtok(&(str_line[1]), " ]");			/*Copy the section name*/
	   if (str_1 == NULL)
	   	{
	   	Msg(-1, "Undefined command at line %i", fs->num_line);
	   	return -1;
	   	}

		if (strncmp(str_1, str_END, strlen(str_END)) == 0)
			{														/*Actually, it's its end*/
	   	fs->state2 = FS2_END;
		  	/*initializes the params for the next section*/
		  	fs->data[0] = 0;
		  	//FreeCmd(fs);
			return 0;
			}
			
	   /*It is not an END, so the arguments can be put into fs->args*/
	   str_2 = str_1 + (long)strlen(str_1) + 1L; 
	   if (str_2[0] != 0)
	   	{
	   	while((str_2[0] == '\t' || str_2[0] == ' ') && str_2 [0] != 0) 
	   		str_2++;
		   while (str_2[strlen(str_2)-1] == '\n' || str_2[strlen(str_2)-1] == ' ' || 
		   	str_2[strlen(str_2)-1] == '\t')
		   	str_2[strlen(str_2)-1] = 0;
	   	while (str_2[strlen(str_2)-1] != ']' && strlen(str_2) > 0)
	   		str_2[strlen(str_2)-1] = 0;
	   	str_2[strlen(str_2)-1] = 0;
	   	strcpy(fs->args, str_2);
	   	}
		
		fs->state2 = FS2_BEGIN;								/*Actually, it's its beginning*/
		if (strncmp(str_1, str_TRAINING, strlen(str_TRAINING)) == 0)
			{
	   	fs->state = FSS_TRAINING;
			return 0;
			}
			
		if (strncmp(str_1, str_RECONSTRUCTION, strlen(str_RECONSTRUCTION)) == 0)
			{
	   	fs->state = FSS_RECONS;
			return 0;
			}
	   	
		if (strncmp(str_1, str_RECOGNIZE, strlen(str_RECOGNIZE)) == 0)
			{
	   	fs->state = FSS_RECOG;
			return 0;
			}
	   	
		if (strncmp(str_1, str_MEET, strlen(str_MEET)) == 0)
			{
	   	fs->state = FSS_MEET;
			return 0;
			}
	   	
		if (strncmp(str_1, str_CATEGORIZE_LVQ, strlen(str_CATEGORIZE_LVQ)) == 0)
			{
	   	fs->state = FSS_CATEG_LVQ;
			return 0;
			}
	   
   	Msg(-1, "Undefined section name : %s at line %i", str_1, fs->num_line);
   	fs->state = FS_UNKNOWN;
   	return -1;
	   }
	
	if (str_line[0] == '.' && fs->state2 != FS2_PROCESSING)
		{															/*just encountered a command line*/
	   str_1 = strtok(&(str_line[1]), " \t\n");
	   if (str_1 == NULL)
	   	{
	   	Msg(-1, "Undefined command at line %i", fs->num_line);
	   	return -1;
	   	}
	   str_2 = str_1 + (long)strlen(str_1) + 1L; 
	   if (str_2[0] != 0)
	   	{
	   	while((str_2[0] == '\t' || str_2[0] == ' ') && str_2 [0] != 0) 
	   		str_2++;
		   while (str_2[strlen(str_2)-1] == '\n' || str_2[strlen(str_2)-1] == ' ' || 
		   	str_2[strlen(str_2)-1] == '\t')
		   	str_2[strlen(str_2)-1] = 0;
	   	strcpy(fs->args, str_2);
	   	}
		
		if (strncmp(str_1, str_LOAD_EIGEN, strlen(str_LOAD_EIGEN)) == 0)
			{
	   	fs->state = FSC_LEIGEN;
			return 0;
			}

		if (strncmp(str_1, str_LOAD_MEET, strlen(str_LOAD_MEET)) == 0)
			{
	   	fs->state = FSC_LMEET;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_EIGEN_DB, strlen(str_SAVE_EIGEN_DB)) == 0)
			{
	   	fs->state = FSC_SEIGENDB;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_EIGEN, strlen(str_SAVE_EIGEN)) == 0)
			{
	   	fs->state = FSC_SEIGEN;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_CMP_MEET, strlen(str_SAVE_CMP_MEET)) == 0)
			{
	   	fs->state = FSC_SCMP_MEET;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_CMP_RECONSTRUCTION, strlen(str_SAVE_CMP_RECONSTRUCTION)) == 0)
			{
	   	fs->state = FSC_SCMP_RECONS;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_CMP_RECOGNIZE, strlen(str_SAVE_CMP_RECOGNIZE)) == 0)
			{
	   	fs->state = FSC_SCMP_RECOG;
			return 0;
			}

		if (strncmp(str_1, str_SAVE_REPORT, strlen(str_SAVE_REPORT)) == 0)
			{
	   	fs->state = FSC_SREPORT;
			return 0;
			}

   	Msg(-1, "Undefined command name at line %i", fs->num_line);
   	fs->state = FS_UNKNOWN;
   	return -1;
		}
	
	/*The line is not a new section line neither a command line, neither a comment*/
	/*so it must be IN a section*/
	if (fs->state > FS_SECTION && fs->state < FS_COMMAND && fs->state2 == FS2_PROCESSING)
		{															/*The line is IN a section*/
		if (str_line[0] == '.')
			{															/*just encountered a command line IN a section*/
		   PFILECMD pnext;
		   str_1 = strtok(&(str_line[1]), " \t\n");
		   if (str_1 == NULL)
		   	{
		   	Msg(-1, "Undefined command at line %i", fs->num_line);
		   	return -1;
		   	}
		   str_2 = str_1 + (long)strlen(str_1) + 1L; 
		   if (str_2[0] != 0)
		   	{
		   	while((str_2[0] == '\t' || str_2[0] == ' ') && str_2 [0] != 0) 
		   		str_2++;
		   	}
		   while (str_2[strlen(str_2)-1] == '\n' || str_2[strlen(str_2)-1] == ' ' || 
		   	str_2[strlen(str_2)-1] == '\t')
		   	str_2[strlen(str_2)-1] = 0;
		   /*pnext = AppendCmd(fs);
		   strcpy(pnext->cmd, str_1);
		   strcpy(pnext->args, str_2);*/
		   strcpy(fs->pnext[fs->num_cmd].cmd, str_1);
		   strcpy(fs->pnext[fs->num_cmd].args, str_2);
		   fs->num_cmd++;
		   strcpy(fs->data, "");
		   fs->state2 = FS2_CMD;
		   return 0;
		   }
			
		strcpy(fs->data, str_line);
		return 0;
		}
	
  	Msg(-1, "Error at line %i", fs->num_line);
  	fs->state = FS_UNKNOWN;
	return -1;
	}
	
int CAAM::process_line(PFILESTATE fs)
	{
	int ret;
	char EValName[255];
	char EVectName[255];
	
	switch(fs->state)
		{
		case FS_COMMENT:
		case FS_UNKNOWN:
			return 0;
		case FSS_CATEG_LVQ:
			switch (fs->state2)
				{
				case FS2_BEGIN:
					if (IsCommand(CMD_LOAD_EIGEN) == FALSE && IsCommand(CMD_TRAINING) == FALSE)
						{
						Msg(0, str_NOTYET_EIGEN, fs->num_line);
						return -1;
						}
					if (IsCommand(CMD_TRAINING) == FALSE && IsCommand(CMD_MEET) == FALSE)
						{
						Msg(0, str_NOTYET_MEET, fs->num_line);
						return -1;
						}
					Msg(+1, str_PROJECT_CATEGORIZE);
					break;
				case FS2_PROCESSING:
					ret = GetCategorizeDB()->AddFace(fs->data);
					if (ret < 0)
						return ret;
					break;
				case FS2_END:
						Msg(-1, str_PROJECT_CATEGORIZE_DONE, GetCategorizeDB()->GetNumFace());
						if (fs->state == FSS_CATEG_LVQ)
							ret = Categorize(fs/*->pnext*/);
						fs->num_cmd = 0;
						//FreeCmd(fs);
						if (ret < 0)
							return ret;
					break;
				}
			return 0;
		
		case FSS_TRAINING:
			switch (fs->state2)
				{
				case FS2_BEGIN:
					if (IsCommand(CMD_LOAD_EIGEN) == TRUE)
						{
						Msg(0, str_ALREADY_LOAD_EIGEN, fs->num_line);
						return -1;
						}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -