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

📄 feret_neighbor.c

📁 feret人脸图象数据库处理代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	  (float) count/no_files, count, no_files);  fprintf(fout, "\n\nThe recognition rate is  %f  (%d/%d)\n", 	  (float) count/no_files, count, no_files);    fclose(fout);  } /* main */void read_pcoeffs(char dir[],char line[], Test artest, int no_points, int f_flag){  /* Requires  : The directory, dir contain four sub-directories f, q, h and p                 containing coefficients and having the files line. Line 		 should be a file in the standard format.     effects   : Reads in the coefficients of the face line from each of                  the sub-directories f, q, h and p and store them in the 		 structure artest.      modifies  : The structure artest                                       */  int point = 0;  float a_point;  FILE *ftest;  char tfile[MAX_CHARS];      /* Allocate the necessary memory space for artest which is just      a structure Test which will be used for storing the coeffs for     each test file */      sprintf(tfile, "%s/f/%s", dir, line);  if ( (ftest = fopen(tfile, "r")) == NULL){    fprintf(stderr, "\nERROR: Input file %s cannot be opened in the function read_pcoeffs\n\n", tfile);    fprintf(stderr, "This is a probe file\n");    exit(2);  }  while ( (fscanf(ftest, "%f", &a_point)) != EOF){    artest.fcoeffs[point] = a_point;    point++;  }  fclose(ftest);    if (!f_flag){    point = 0;    sprintf(tfile, "%s/q/%s", dir, line);    if ( (ftest = fopen(tfile, "r")) == NULL){      fprintf(stderr, "\nERROR: Input file %s cannot be opened in the function read_pcoeffs\n\n", tfile);      fprintf(stderr, "This is a probe file\n");      exit(2);    }    while ( (fscanf(ftest, "%f", &a_point)) != EOF){      artest.qcoeffs[point] = a_point;      point++;    }    fclose(ftest);        point = 0;    sprintf(tfile, "%s/h/%s", dir, line);    if ( (ftest = fopen(tfile, "r")) == NULL){      fprintf(stderr, "\nERROR: Input file %s cannot be opened in the function read_pcoeffs\n\n", tfile);      fprintf(stderr, "This is a probe file\n");      exit(2);    }    while ( (fscanf(ftest, "%f", &a_point)) != EOF){      artest.hcoeffs[point] = a_point;      point++;    }    fclose(ftest);        point = 0;    sprintf(tfile, "%s/p/%s", dir, line);    if ( (ftest = fopen(tfile, "r")) == NULL){      fprintf(stderr, "\nERROR: Input file %s cannot be opened in the function read_pcoeffs\n\n", tfile);      fprintf(stderr, "This is a probe file\n");      exit(2);    }    while ( (fscanf(ftest, "%f", &a_point)) != EOF){      artest.pcoeffs[point] = a_point;      point++;    }    fclose(ftest);  }} /* void read_pcoeffs..... */int number_points(char descriptor[]){  /* requires : The descriptor be a file of the standard format     effects  : extracts the no. of points info in the file and returns     modifies : nothing                                         */  int i;  char line[MAX_CHARS], junk[MAX_CHARS];  FILE *fp;  if ( (fp=fopen(descriptor, "r")) == NULL) {    fprintf(stderr, "\nERROR: Input file %s cannot be opened\n", descriptor);    fprintf(stderr, "This is a descriptor file\n");    exit(2);  }  fgets(line, MAX_CHARS, fp);  fgets(line, MAX_CHARS, fp);  fscanf(fp, "%s %i %s", line, &i, junk);   fclose(fp);  return(i);}  int number_files(char list[]){  /* requires: list be a list of filenames of standard format     effects : It counts and returns the number of filenames in list     modifies: nothing                                                 */  int no_files;  char line[MAX_CHARS];  FILE *flist;      /* Loop over input list file and calculate total no. of      test files and create the array of tfaces */  if ( (flist = fopen(list, "r")) == NULL){    fprintf(stderr, "\nERROR: Input file %s cannot be opened \n\n", list);    fprintf(stderr, "This is a list file and can be either probes or Gallery\n");    exit(2);  }  no_files = 0;  while (fscanf(flist, "%s", line) != EOF){    if ( (strncmp(line, "#", 1) != 0) && (strlen(line) > 1))      no_files++;  }    fclose(flist);  return (no_files);}void read_gcoeffs(char dir[], char list[], int no_points, Kface *kfaces){  /* requires: The dir consists of four directories called f, q, h and p.                These directories should have the files specified in list. 	       All four files have the same name. Memory should have been 	       allocated to the array kfaces.     effects : The function reads in the coefficients from each directory in                the corresponding member in tface     modifies: kfaces */  int file_no = 0;  int point = 0;  FILE *fklist, *fknown;  char line[MAX_CHARS], kfile[MAX_CHARS];  float a_point;  if ( (fklist = fopen(list, "r")) == NULL){    fprintf(stderr, "\nERROR: the file %s cannot be opened in the function read_kcoeffs\n\n", list);    exit(2);    fprintf(stderr, "This is a list file for Gallery files\n");  }  /* Open the list file and read in all the coeffs */  while (fscanf(fklist, "%s", line) != EOF){    if ( (strncmp(line, "#", 1) != 0) || (strlen(line) >1)){           /* Allocate the necessary memory space */      /* ------- Baback's hack --------------------      fprintf(stdout,"1: %s(%d)\n", line, file_no);      fprintf(stdout,"2: %s\n", kfaces[file_no].name);      ------------------------------------------------- */      kfaces[file_no].name = strdup(line);      kfaces[file_no].fcoeffs = (float *) calloc(no_points, sizeof(float));      kfaces[file_no].qcoeffs = (float *) calloc(no_points, sizeof(float));      kfaces[file_no].hcoeffs = (float *) calloc(no_points, sizeof(float));      kfaces[file_no].pcoeffs = (float *) calloc(no_points, sizeof(float));           if ( (kfaces[file_no].fcoeffs == NULL) || (kfaces[file_no].qcoeffs == NULL) || (kfaces[file_no].hcoeffs == NULL) || (kfaces[file_no].pcoeffs == NULL)){	fprintf(stderr, "\nERROR: Memory cannot be allocated in the function read_kcoeffs\n\n");	exit(3);      }      /* Read the f coeffs */      sprintf(kfile, "%s/f/%s", dir, line);      point = 0;      if ( (fknown = fopen(kfile, "r")) == NULL){	fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", kfile);	fprintf(stderr, "This is a known/Gallery file\n");	exit(2);      }      while ( (fscanf(fknown, "%f", &a_point) != EOF)){	kfaces[file_no].fcoeffs[point] = a_point;	point++;      }      fclose(fknown);            /* Read i all the q coeffs */      sprintf(kfile, "%s/q/%s", dir, line);      point = 0;      if ( (fknown = fopen(kfile, "r")) == NULL){	fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", kfile);	fprintf(stderr, "This is a known/Gallery file\n");	exit(2);      }      while ( (fscanf(fknown, "%f", &a_point) != EOF)){	kfaces[file_no].qcoeffs[point] = a_point;	point++;      }      fclose(fknown);      /*Read all the h coeffs */      sprintf(kfile, "%s/h/%s", dir, line);      point = 0;      if ( (fknown = fopen(kfile, "r")) == NULL){	fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", kfile);	fprintf(stderr, "This is a known/Gallery file\n");	exit(2);      }      while ( (fscanf(fknown, "%f", &a_point) != EOF)){	kfaces[file_no].hcoeffs[point] = a_point;	point++;      }      fclose(fknown);            /* Read all the p coeffs */      sprintf(kfile, "%s/p/%s", dir, line);      point = 0;      if ( (fknown = fopen(kfile, "r")) == NULL){	fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", kfile);	fprintf(stderr, "This is a known/Gallery file\n");	exit(2);      }      while ( (fscanf(fknown, "%f", &a_point)) != EOF){	kfaces[file_no].pcoeffs[point] = a_point;	point++;      }      fclose(fknown);      file_no++;          } /* if ( (strncmp... */  }   /* while (fscanf... */} /* void read_kcoeffs */void read_weights(char weight_file[], float *weights){  /* Requires  : The weight file be of the standard format. A weight file                  consists of an ASCII matrix as follows:		 1   2   3   4		 5   6   7   8		 9   10  11 12		 13  14  15 16     effects   : The function reads each entry into the array weights in the                 same order as that shown above.     modifies  : weights                                            */  int i;  FILE *fp;  float a, b, c, d;  if ( (fp = fopen(weight_file, "r")) == NULL) {    fprintf(stderr, "\nERROR: Input file %s cannot be opened in the function read_weights\n\n", weight_file);    fprintf(stderr, "This is either the weights or the biases  file\n");    exit(2);  }    for (i=0; i<=12; i=i+4){    fscanf(fp, "%f %f %f %f", &a, &b, &c, &d);    weights[i] = a;    weights[i+1]=b;    weights[i+2]=c;    weights[i+3]=d;  }  fclose(fp);} /* void read_weights.... */float find_distance(float *test, float *known, int no){  /* Requires: no be less than the size of test or known     effects : finds the Euclidean distance between the corresponding                coefficients stored in test and known     modifies: nothing                                         */    int i;  float sum=0;    for (i=0; i<no; i++){    sum += ( (test[i] - known[i]) * (test[i]-known[i]));  }  return (sqrt(sum));} /* float find_distance ....... */void print_match(Tface *faces, int n, int no_files, char out[], float thres, char comline[]){  int i, j,k, count, l, rec=0;  float d, e;  Matchptr temp;  FILE *fp;  char test[MAX_CHARS], known[MAX_CHARS];    if ( (fp = fopen(out, "w")) == NULL){    fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", out);    exit(2);  }    fprintf(stderr, "Command line : %s \n\n", comline);  fprintf(fp, "Command Line: %s\n\n", comline);  fprintf(fp, "<Probe number> <Match order> <Gallery Match Number> <Found Flag> <Confidence>\n");  for (i=0; i<no_files; i++){    temp = faces[i].similar;   /* fprintf(stderr, "in print_match faces[%d].name is %s\n", i, faces[i].name);      fprintf(stderr, "in print_match temp->name is %s\n", temp->name);      fprintf(stderr, "in print_match temp->next->name is %s\n", temp->next->name);*/    count = 1;        fprintf(fp, "\n\n");        l = strlen(faces[i].name);    for (k=0; k<l ; k++){      if ( faces[i].name[k] >= '0' && faces[i].name[k] <= '9'){	test[k] = faces[i].name[k];      }      else 	break;    }    test[k]='\0';        for (j=0; j<n; j++){      l = strlen(temp->name);      for (k=0; k<l; k++){	if (temp->name[k] >= '0' && temp->name[k] <= '9')	  known[k] = temp->name[k];	else 	  break;      }      known[k] = '\0';            if (count == 1) {	d  = temp->distance;	if ( strcmp(test, known)== 0){	    rec++;	  }      }            /*e = 100*log(conf_factor)/log(temp->distance + conf_factor);*/      /*	fprintf(stderr, "in print_match: temp->name is %s\n", temp->name);		fprintf(stderr, "in print_match: temp->dist is %f\n\n", temp->distance);*/      /* fprintf(fp, "%4d %3d %4d   %1d   %5.3f %f\n", atoi(test), count, atoi(known), (e < thres ? 0 : 1), e, temp->distance); */                  fprintf(fp, "%4d %3d %4d   %1d   %5.3f\n", atoi(test), count, atoi(known), (temp->distance < thres ? 0 : 1), temp->distance);      temp = temp->next;      count++;      /* fprintf(stderr, "the value of e is %f\n", e);	 fprintf(stderr, "the value of d is %f\n", d); */    }  }      fprintf(stdout, "%f\n", (float) rec/no_files);  fprintf(stderr, "\n\nThe recognition rate is  %f  (%d/%d)\n", 	  (float) rec/no_files, rec, no_files);  fprintf(fp, "\n\nThe recognition rate is  %f  (%d/%d)\n", 	  (float) rec/no_files, rec, no_files);    fclose(fp);} /* Void print_match */Matchptr add_match(Matchptr root, Matchptr new, int n){  Matchptr temp, prev;  int count = 0;  if (root==NULL){    root=new;    return new;  }  else {    if (root->distance < new->distance){      new->next = root;      return new;    }  }    temp = root;  prev = root;  while ( ((temp != NULL) && (count != n))  && (temp->distance >= new->distance)){    prev = temp;    temp = temp->next;    count++;  }  new->next = temp;  prev->next = new;  return root;} /* void add_match(.......... */int print_file(Tface tfaces, int n, FILE *fout, float thres, int count){  int i, j, k, l, rec = 1;  float d, e;  Matchptr temp;    char test[MAX_CHARS], known[MAX_CHARS];  temp = tfaces.similar;    l = strlen(tfaces.name);  for (k=0; k<l ; k++){    if ( tfaces.name[k] >= '0' && tfaces.name[k] <= '9'){      test[k] = tfaces.name[k];    }    else       break;  }  test[k]='\0';      for (j=0; j<n; j++){    l = strlen(temp->name);    for (k=0; k<l; k++){      if (temp->name[k] >= '0' && temp->name[k] <= '9')	known[k] = temp->name[k];      else 	break;    }    known[k] = '\0';        if (rec == 1){      d = temp->distance;      if (strcmp(test, known) == 0){	count++;      }    }        fprintf(fout, "%4d %3d %4d   %1d   %5.3f\n", atoi(test), rec, atoi(known), (temp->distance < thres ? 0 : 1), temp->distance);    temp = temp->next;    rec++;  }  return(count);}void kill_list(Matchptr list){  if (!empty_list(list)){    kill_list(list->next);    free(list->name);    free(list);  }}int empty_list(Matchptr list){  if (list == NULL)    return (1);  return (0);}

⌨️ 快捷键说明

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