?? neighbor.c
字號:
rewind(ftlist);/* Begin Hack for FERET test Sept 16 1996 */ if ( (fout = fopen(out_file, "w")) == NULL){ fprintf(stderr, "\nERROR: output file %s cannot be opened\n", out_file); fprintf(stderr, "This is the output file to which results are to be written\n"); exit(2); } fprintf(stderr, "Command line : %s \n\n", comline); fprintf(fout, "Command Line: %s\n\n", comline); fprintf(fout, "<Probe number> <Match order> <Gallery Match Number> <Found Flag> <Confidence>\n\n");/* End Hack for FERET test Sept 16 1996 */ file_no = 0; while ( (fscanf(ftlist, "%s", line) != EOF)){ if ( (strncmp(line, "#", 1) != 0) || (strlen(line) > 1)){ /* Initialize face array */ faces[file_no].name = strdup(line); faces[file_no].similar = NULL; /* Read coeffs from the test faces */ sprintf(tfile, "%s/%s", test_dir, line); if ( (ftest = fopen(tfile, "r")) == NULL) { fprintf(stderr, "\nERROR: Input file %s cannot be opened\n\n", tfile); exit(2); } point = 0; while ( (fscanf(ftest, "%f", &a_point)) != EOF){ t_points[point] = a_point; point++; } fprintf(stdout, "Matching probe: %s ", line); fval = FLT_MAX; for (i=0; i < no_kfiles; i++){ if (do_bayesian) dist = bayesian_distance(t_points, kfaces[i].coeffs, total_no_points, &B_struct); else dist = find_distance(t_points+a, kfaces[i].coeffs+a, no_points); if (dist < fval) fval = dist; /* Create a match */ a_match = (Matchptr) calloc(1, sizeof(Match)); a_match->name = strdup(kfaces[i].name); a_match->distance = dist; a_match->next = NULL; /* Add to the list of matches of that particular face */ faces[file_no].similar = add_match(faces[file_no].similar, a_match, n); } fprintf(stdout, "\tD_min = %+1.6e \n", fval); fclose(ftest); /* Begin Hack for FERET test sept 16 1996 */ fprintf(fout, "\n"); count = print_file(faces[file_no], n, fout, thres, count); fprintf(fout, "\n"); fflush(fout); kill_list(faces[file_no].similar); /* End Hack for FERET test sept 16 1996 */ file_no++; } /* if ( (strncmp(line........... */ } /* while ( (fscanf(ftlist........ */ fclose(ftlist); /* Begin Hack for FERET test sept 16 1996 */ fprintf(stdout, "%f\n", (float) count/no_files); fprintf(stderr, "\n\nThe recognition rate is %f (%d/%d)\n", (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); /* Begin Hack for FERET test sept 16 1996 */ /* print_match(faces, n, no_files, out_file, thres, comline);*/} /* Main */ float find_distance(float *test, float *known, int no){ int i; float sum=0; for (i=0; i<no; i++){ sum += ( (test[i] - known[i]) * (test[i]-known[i])); } return (sqrt(sum));}float bayesian_distance(float *test, float *known, int N, Bayesian *B){ register int i,j,k; static float C0[MAX_NDIM]; static float C1[MAX_NDIM]; static float x[MAX_NDIM]; static float y[MAX_NDIM]; float f0,f1; float L0,L1; /* compute the difference vector (my arrays start with 1) */ for (i=0; i<N; i++) x[i+1] = test[i] - known[i]; /* project onto intra space */ for (i=1; i<=N; i++) y[i] = x[i] - B->intra_kl[1][i]; for (j=B->a; j<=B->b; j++) { f0 = 0; for (i=1; i<=N; i++) f0 += (y[i] * B->intra_kl[1+j][i]); C0[j] = f0; } /* project onto extra space */ for (i=1; i<=N; i++) y[i] = x[i] - B->extra_kl[1][i]; for (j=B->a; j<=B->b; j++) { f1 = 0; for (i=1; i<=N; i++) f1 += (y[i] * B->extra_kl[1+j][i]); C1[j] = f1; } /* compute intra log-likelihood */ f0 = 0; for (i=B->a; i<=B->b; i++) f0 += (C0[i]*C0[i]/(B->intra_kl[B->N+2][i])); L0 = -0.5 * f0 - B->logD0; /* compute extra log-likelihood */ f1 = 0; for (i=B->a; i<=B->b; i++) f1 += (C1[i]*C1[i]/(B->extra_kl[B->N+2][i])); L1 = -0.5 * f1 - B->logD1; /* return NEGATIVE log-ratio as a distance */ return (L1 - L0 + B->logP1 - B->logP0);}Matchptr add_match(Matchptr root, Matchptr new, int n){ Matchptr temp, prev; int count = 0; if (root==NULL) 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;}/*char *strdup(char *s) { char *p = (char*)malloc(strlen(s) + 1); if (p==NULL) { fprintf(stderr, "Error: Memory cannot be allocated"); exit (5); } return(strcpy(p, s)); } */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; 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'; if (n==0){ while (temp != NULL){ 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++; } } if (do_bayesian) e = 100/(1 + exp(sigmoid_scale * temp->distance)); else e = 100*log(conf_factor)/log(temp->distance + conf_factor);; fprintf(fp, "%4i\t%4i\t%4i\t%1d\t%11.8f \n", atoi(test), count, atoi(known),((e > thres) ? 1 : 0), e); temp = temp->next; count++; } } else { 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++; } } if (do_bayesian) e = 100/(1 + exp(sigmoid_scale * temp->distance)); else e = 100*log(conf_factor)/log(temp->distance + conf_factor);; fprintf(fp, "%4i\t%4i\t%4i\t%1d\t%11.8f \n", atoi(test), count, atoi(known), (e < thres ? 0 : 1), e); 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);}int number_files(char list[]){ 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); 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 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);}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++; } } if (do_bayesian){ e = 100/(1+exp(sigmoid_scale * temp->distance)); } else { e = 100* log(conf_factor)/log(temp->distance + conf_factor); } fprintf(fout, "%4d\t%3d\t%4d\t%1d\t%11.8f\n", atoi(test), rec, atoi(known), (e < thres ? 0 : 1), e); temp = temp->next; rec++; } return(count);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -