예제 #1
0
파일: other.c 프로젝트: 10crimes/code
void go() {

  String fname=Sconc(ImageDir,iname,".bmp");
  String ename=Sconc(ImageDir,"exp/",iname,".bmp");
printf("read %s\n",fname);
  orig=RGBmp::readfile(fname);
//  image=orig->scaledby(scale)->getv3ds();
  image=orig->scaletowidth(processwidth)->getv3ds();
  printf("Scanning image %i x %i\n",image->width,image->height);
printf("done\n");
  if (train || test)
    expected=Map2d<float>::readfile(ename)->threshold(0.5)->binscaleto(image->width,image->height);

  greyscale=image->applyfn(&V3dtofloat);

  starttimer();

  greyscale->edgedetection(Map2d<float>::sobel(),&edgemag,&edgeang);

  ghistscale=1.0/(float)windres;
  ghs=new Map2d<GHist *>(image->width*ghistscale,image->height*ghistscale,(GHist *)NULL);
  
  if (train)
    data=List<IOData>(2000);
  
  printf("Starting segmentation...\n");
  Seg seg;
  if (train)
    seg=Seg(&classifytrain);
  else
    seg=Seg(&classifynn);
  

  if (show)
    for (int i=1;i<=nummeasures;i++)
      measmaps.add(new Map2d<float>(image->width,image->height,(float)0));

  if (twopass) {
    pass=1;
    printf("First scan, reading measures...\n");
    Map2d<bool> *b=seg.classifywindows();
    destroy(b);
    normalisedata();
    pausetimer();
    printf("Asking neural network...\n");
printf("A\n");
    for (int i=1;i<=data.len;i++) {
      writetraindata(data.num(i).input,123.456);
      data.p2num(i)->freedom();
    }
    printf("Freeing data\n");
    data.freedom();
printf("B\n");
    addheaderto(&traindata,numtrainexs);
    writelinestofile(&traindata,"question.pat");
    printf("Not freeing traindata strings\n");
//    traindata.freeall();
    printf("Freeing traindata\n");
    traindata.freedom();
    system("./asknn");
    traindata=readlinesfromfile("answer.res");
    unpausetimer();
    currentline=14;
    pass=2;
    printf("Second scan...\n");
  }
  Map2d<bool> *b=seg.classifywindows();

  printf("Time taken: %f seconds.\n",gettimer());

  if (show)
    for (int i=1;i<=nummeasures;i++)
      measmaps.num(i)->writefile(getnextfilename("meas","bmp"));

//  Seg seg=Seg(&measureglvariance);
//  Map2d<bool> *b=seg.classifytopdown();
//  Map2d<bool> *b=seg.classifyneighbours();
//  Map2d<bool> *b=seg.classifywindows();

  b->scaleto(image->width,image->height)->writefile("initseg.bmp");
  
  // Reject small regions
  List< Region * > *l=b->getrealregions();
  b=new Map2d<bool>(b->width,b->height,false);
  for (int i=1;i<=l->len;i++) {
    List<Pixel> *ps=l->num(i)->getlist();
    if (ps->len>=minarea)
      for (int j=1;j<=ps->len;j++)
        b->setpos(ps->num(j),true);
  }
  b->writefile("initb4join.bmp");

  // Perform morphological joining (dilation and erosion)
  b=b->binscaleto(b->width/windres,b->height/windres);
  b=b->expand(morphrad)->contract(2*morphrad)->expand(morphrad);
  // b=b->contract(morphrad)->expand(2*morphrad)->contract(morphrad);
  // b=b->expand(morphrad)->inverse()->expand(morphrad)->inverse();
  b=b->binscaleto(image->width,image->height);
  // b->writefile("initjoined.bmp");
  
  b->writefile("textseg.bmp");
  
  if (test) {
    int correct=0;
    int wrong=0;
    int ctextcorrect=0;
    int cnottextcorrect=0;
    int ctextincorrect=0;
    int cnottextincorrect=0;
    int total=0;
    for (int i=0;i<b->width;i++)
    for (int j=0;j<b->height;j++) {
      total++;
      bool exp=expected->getpos(i*expected->width/b->width,j*expected->height/b->height);
      bool cla=b->getpos(i,j);
      if (exp==cla)
        correct++;
      else
        wrong++;
      if (exp)
        if (cla)
          ctextcorrect++;
        else
          cnottextincorrect++;
      else
        if (cla)
          ctextincorrect++;
        else
          cnottextcorrect++;
    }
    if (fileexists("test.dat")) {
      List<String> ls=readlinesfromfile("test.dat");
      correct+=tofloat(ls.num(1));
      wrong+=tofloat(ls.num(2));
      ctextcorrect+=tofloat(ls.num(3));
      cnottextcorrect+=tofloat(ls.num(4));
      ctextincorrect+=tofloat(ls.num(5));
      cnottextincorrect+=tofloat(ls.num(6));
      total+=tofloat(ls.num(7));
    }
    List<String> ls;
    ls.add(Sformat("%i correctly classified",correct));
    ls.add(Sformat("%i incorrectly classified",wrong));
    ls.add(Sformat("%i correctly classified as text",ctextcorrect));
    ls.add(Sformat("%i correct classified as non-text",cnottextcorrect));
    ls.add(Sformat("%i classified as text when not",ctextincorrect));
    ls.add(Sformat("%i classified as non-text when was text",cnottextincorrect));
    ls.add(Sformat("%i total",total));
    ls.add("");
    int totaltext=ctextcorrect+cnottextincorrect;
    int totalnottext=ctextincorrect+cnottextcorrect;
    ls.add(Sformat("That's %i text in total",totaltext));
    ls.add(Sformat("and %i non-text in total",totalnottext));
    ls.add("");
    ls.add("And in percent:");
    ls.add(Sformat("%f percent correctly classified",100.0*(float)correct/(float)total));
    ls.add(Sformat("%f percent incorrectly classified",100.0*(float)wrong/(float)total));
    ls.add(Sformat("%f percent of text correctly classified as text",100.0*(float)ctextcorrect/(float)totaltext));
    ls.add(Sformat("%f percent of non-text correctly classified as non-text",100.0*(float)cnottextcorrect/(float)totalnottext));
    writelinestofile(ls,"test.dat");
  }
    
}
예제 #2
0
파일: timer.c 프로젝트: dpl0/timer
int
main(int argc, char *argv[])
{
	char *home;
	int err;
	enum mode m;
	pid_t timerpid;
	struct stat st;

	if (argc == 2)
		switch (argv[1][0]) { 
		case 'p':
			m = PAUSE;
			break;
		case 'r':
			m = RESTART;
			break;
		case 's':
			m = STOP;
			break;
		default:
			usage(argv[0]);
			break;
		}
	else if (argc == 1)
		m = BEGIN;
	else
		usage(argv[0]);		

	//Check if we're starting the timer
	//and the file exists.
	err = access(PIDFILE, F_OK);
	if (m !=  BEGIN && err != 0) {
		printf("Timer is not running, please start it first.\n");
		exit(1);
	} else if (m == BEGIN && err == 0) {
		printf("Timer is already running, please stop it first.\n");
		exit(1);
	}

	//Create or read the file used to store the PID of the timer.
	if (m != BEGIN)
		pidfile = fopen(PIDFILE, "r+");
	else if (m == BEGIN)
		pidfile = fopen(PIDFILE, "w+");

	if (pidfile == NULL && m != BEGIN) {
		printf("Couldn't open %s for read it\n", PIDFILE);
		exit(1);
	} else if (pidfile == NULL && m == BEGIN) {
		printf("Couldn't create %s.\n", PIDFILE);
		exit(1);
	}

	if (m != BEGIN)
		timerpid = (pid_t)getw(pidfile);

	if ((home = getenv("HOME")) == NULL) {
		printf("Error: No $HOME, you hobbo!.\n");
		printf("Continuing anyways.\n");
	} else
		timerfilename = strncat(home, "/.timer", sizeof(timerfilename));

	switch (m) {
	case BEGIN:
		begin();
		break;
	case PAUSE:
		pausetimer(timerpid);
		break;
	case RESTART:
		restarttimer(timerpid);
		break;
	case STOP:
		stoptimer(timerpid);
		break;
	}	

	exit (0);
}