4 #include "_lsvmparser.h"
5 #include "_lsvm_error.h"
9 int isMODEL (char *str){
10 char stag [] = "<Model>";
11 char etag [] = "</Model>";
12 if(strcmp(stag, str) == 0)return MODEL;
13 if(strcmp(etag, str) == 0)return EMODEL;
18 char etag [] = "</P>";
19 if(strcmp(stag, str) == 0)return P;
20 if(strcmp(etag, str) == 0)return EP;
23 int isSCORE (char *str){
24 char stag [] = "<ScoreThreshold>";
25 char etag [] = "</ScoreThreshold>";
26 if(strcmp(stag, str) == 0)return SCORE;
27 if(strcmp(etag, str) == 0)return ESCORE;
30 int isCOMP (char *str){
31 char stag [] = "<Component>";
32 char etag [] = "</Component>";
33 if(strcmp(stag, str) == 0)return COMP;
34 if(strcmp(etag, str) == 0)return ECOMP;
37 int isRFILTER (char *str){
38 char stag [] = "<RootFilter>";
39 char etag [] = "</RootFilter>";
40 if(strcmp(stag, str) == 0)return RFILTER;
41 if(strcmp(etag, str) == 0)return ERFILTER;
44 int isPFILTERs (char *str){
45 char stag [] = "<PartFilters>";
46 char etag [] = "</PartFilters>";
47 if(strcmp(stag, str) == 0)return PFILTERs;
48 if(strcmp(etag, str) == 0)return EPFILTERs;
51 int isPFILTER (char *str){
52 char stag [] = "<PartFilter>";
53 char etag [] = "</PartFilter>";
54 if(strcmp(stag, str) == 0)return PFILTER;
55 if(strcmp(etag, str) == 0)return EPFILTER;
58 int isSIZEX (char *str){
59 char stag [] = "<sizeX>";
60 char etag [] = "</sizeX>";
61 if(strcmp(stag, str) == 0)return SIZEX;
62 if(strcmp(etag, str) == 0)return ESIZEX;
65 int isSIZEY (char *str){
66 char stag [] = "<sizeY>";
67 char etag [] = "</sizeY>";
68 if(strcmp(stag, str) == 0)return SIZEY;
69 if(strcmp(etag, str) == 0)return ESIZEY;
72 int isWEIGHTS (char *str){
73 char stag [] = "<Weights>";
74 char etag [] = "</Weights>";
75 if(strcmp(stag, str) == 0)return WEIGHTS;
76 if(strcmp(etag, str) == 0)return EWEIGHTS;
81 char etag [] = "</V>";
82 if(strcmp(stag, str) == 0)return TAGV;
83 if(strcmp(etag, str) == 0)return ETAGV;
87 char stag [] = "<Vx>";
88 char etag [] = "</Vx>";
89 if(strcmp(stag, str) == 0)return Vx;
90 if(strcmp(etag, str) == 0)return EVx;
94 char stag [] = "<Vy>";
95 char etag [] = "</Vy>";
96 if(strcmp(stag, str) == 0)return Vy;
97 if(strcmp(etag, str) == 0)return EVy;
101 char stag [] = "<Penalty>";
102 char etag [] = "</Penalty>";
103 if(strcmp(stag, str) == 0)return TAGD;
104 if(strcmp(etag, str) == 0)return ETAGD;
107 int isDx (char *str){
108 char stag [] = "<dx>";
109 char etag [] = "</dx>";
110 if(strcmp(stag, str) == 0)return Dx;
111 if(strcmp(etag, str) == 0)return EDx;
114 int isDy (char *str){
115 char stag [] = "<dy>";
116 char etag [] = "</dy>";
117 if(strcmp(stag, str) == 0)return Dy;
118 if(strcmp(etag, str) == 0)return EDy;
121 int isDxx (char *str){
122 char stag [] = "<dxx>";
123 char etag [] = "</dxx>";
124 if(strcmp(stag, str) == 0)return Dxx;
125 if(strcmp(etag, str) == 0)return EDxx;
128 int isDyy (char *str){
129 char stag [] = "<dyy>";
130 char etag [] = "</dyy>";
131 if(strcmp(stag, str) == 0)return Dyy;
132 if(strcmp(etag, str) == 0)return EDyy;
136 char stag [] = "<LinearTerm>";
137 char etag [] = "</LinearTerm>";
138 if(strcmp(stag, str) == 0)return BTAG;
139 if(strcmp(etag, str) == 0)return EBTAG;
143 int getTeg(char *str){
168 void addFilter(CvLSVMFilterObject *** model, int *last, int *max){
169 CvLSVMFilterObject ** nmodel;
172 if((*last) >= (*max)){
174 nmodel = (CvLSVMFilterObject **)malloc(sizeof(CvLSVMFilterObject *) * (*max));
175 for(i = 0; i < *last; i++){
176 nmodel[i] = (* model)[i];
181 (*model) [(*last)] = (CvLSVMFilterObject *)malloc(sizeof(CvLSVMFilterObject));
184 void parserRFilter (FILE * xmlf, int p, CvLSVMFilterObject * model, float *b){
186 int sizeX=0, sizeY=0;
194 //printf("<RootFilter>\n");
199 model->fineFunction[0] = 0.0;
200 model->fineFunction[1] = 0.0;
201 model->fineFunction[2] = 0.0;
202 model->fineFunction[3] = 0.0;
209 ch = (char)fgetc( xmlf );
217 tagBuf[j + 1] = '\0';
219 tagVal = getTeg(tagBuf);
221 if(tagVal == ERFILTER){
222 //printf("</RootFilter>\n");
229 if(tagVal == ESIZEX){
233 model->sizeX = sizeX;
234 //printf("<sizeX>%d</sizeX>\n", sizeX);
240 if(tagVal == ESIZEY){
244 model->sizeY = sizeY;
245 //printf("<sizeY>%d</sizeY>\n", sizeY);
247 if(tagVal == WEIGHTS){
248 data = (double *)malloc( sizeof(double) * p * sizeX * sizeY);
249 size_t elements_read = fread(data, sizeof(double), p * sizeX * sizeY, xmlf);
250 CV_Assert(elements_read == (size_t)(p * sizeX * sizeY));
251 model->H = (float *)malloc(sizeof(float)* p * sizeX * sizeY);
252 for(ii = 0; ii < p * sizeX * sizeY; ii++){
253 model->H[ii] = (float)data[ii];
257 if(tagVal == EWEIGHTS){
258 //printf("WEIGHTS OK\n");
267 *b =(float) atof(buf);
268 //printf("<B>%f</B>\n", *b);
274 if((tag == 0)&& (st == 1)){
284 void parserV (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
299 ch = (char)fgetc( xmlf );
307 tagBuf[j + 1] = '\0';
309 tagVal = getTeg(tagBuf);
322 model->V.x = atoi(buf);
323 //printf(" <Vx>%d</Vx>\n", model->V.x);
332 model->V.y = atoi(buf);
333 //printf(" <Vy>%d</Vy>\n", model->V.y);
338 if((tag == 0)&& (st == 1)){
347 void parserD (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
362 ch = (char)fgetc( xmlf );
370 tagBuf[j + 1] = '\0';
372 tagVal = getTeg(tagBuf);
386 model->fineFunction[0] = (float)atof(buf);
387 //printf(" <Dx>%f</Dx>\n", model->fineFunction[0]);
397 model->fineFunction[1] = (float)atof(buf);
398 //printf(" <Dy>%f</Dy>\n", model->fineFunction[1]);
408 model->fineFunction[2] = (float)atof(buf);
409 //printf(" <Dxx>%f</Dxx>\n", model->fineFunction[2]);
419 model->fineFunction[3] = (float)atof(buf);
420 //printf(" <Dyy>%f</Dyy>\n", model->fineFunction[3]);
426 if((tag == 0)&& (st == 1)){
436 void parserPFilter (FILE * xmlf, int p, int /*N_path*/, CvLSVMFilterObject * model){
438 int sizeX=0, sizeY=0;
446 //printf("<PathFilter> (%d)\n", N_path);
451 model->fineFunction[0] = 0.0f;
452 model->fineFunction[1] = 0.0f;
453 model->fineFunction[2] = 0.0f;
454 model->fineFunction[3] = 0.0f;
461 ch = (char)fgetc( xmlf );
469 tagBuf[j + 1] = '\0';
471 tagVal = getTeg(tagBuf);
473 if(tagVal == EPFILTER){
474 //printf("</PathFilter>\n");
479 parserV(xmlf, p, model);
482 parserD(xmlf, p, model);
488 if(tagVal == ESIZEX){
492 model->sizeX = sizeX;
493 //printf("<sizeX>%d</sizeX>\n", sizeX);
499 if(tagVal == ESIZEY){
503 model->sizeY = sizeY;
504 //printf("<sizeY>%d</sizeY>\n", sizeY);
506 if(tagVal == WEIGHTS){
507 data = (double *)malloc( sizeof(double) * p * sizeX * sizeY);
508 size_t elements_read = fread(data, sizeof(double), p * sizeX * sizeY, xmlf);
509 CV_Assert(elements_read == (size_t)(p * sizeX * sizeY));
510 model->H = (float *)malloc(sizeof(float)* p * sizeX * sizeY);
511 for(ii = 0; ii < p * sizeX * sizeY; ii++){
512 model->H[ii] = (float)data[ii];
516 if(tagVal == EWEIGHTS){
517 //printf("WEIGHTS OK\n");
522 if((tag == 0)&& (st == 1)){
531 void parserPFilterS (FILE * xmlf, int p, CvLSVMFilterObject *** model, int *last, int *max){
540 //printf("<PartFilters>\n");
547 ch = (char)fgetc( xmlf );
555 tagBuf[j + 1] = '\0';
557 tagVal = getTeg(tagBuf);
559 if(tagVal == EPFILTERs){
560 //printf("</PartFilters>\n");
563 if(tagVal == PFILTER){
564 addFilter(model, last, max);
565 parserPFilter (xmlf, p, N_path, (*model)[*last]);
571 if((tag == 0)&& (st == 1)){
580 void parserComp (FILE * xmlf, int p, int *N_comp, CvLSVMFilterObject *** model, float *b, int *last, int *max){
588 //printf("<Component> %d\n", *N_comp);
595 ch = (char)fgetc( xmlf );
603 tagBuf[j + 1] = '\0';
605 tagVal = getTeg(tagBuf);
611 if(tagVal == RFILTER){
612 addFilter(model, last, max);
613 parserRFilter (xmlf, p, (*model)[*last],b);
615 if(tagVal == PFILTERs){
616 parserPFilterS (xmlf, p, model, last, max);
621 if((tag == 0)&& (st == 1)){
630 void parserModel(FILE * xmlf, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
643 //printf("<Model>\n");
650 ch = (char)fgetc( xmlf );
658 tagBuf[j + 1] = '\0';
660 tagVal = getTeg(tagBuf);
662 if(tagVal == EMODEL){
663 //printf("</Model>\n");
664 for(ii = 0; ii <= *last; ii++){
665 (*model)[ii]->numFeatures = p;
672 cmp = (int *)malloc(sizeof(int));
673 bb = (float *)malloc(sizeof(float));
676 * count = N_comp + 1;
678 cmp = (int *)malloc(sizeof(int) * (N_comp + 1));
679 bb = (float *)malloc(sizeof(float) * (N_comp + 1));
680 for(ii = 0; ii < N_comp; ii++){
681 cmp[ii] = (* comp)[ii];
682 bb [ii] = (* b )[ii];
688 * count = N_comp + 1;
690 parserComp(xmlf, p, &N_comp, model, &((*b)[N_comp]), last, max);
691 cmp[N_comp - 1] = *last;
701 //printf("<P>%d</P>\n", p);
707 if(tagVal == ESCORE){
710 *score = (float)atof(buf);
711 //printf("<ScoreThreshold>%f</ScoreThreshold>\n", score);
716 if((tag == 0)&& (st == 1)){
728 int LSVMparser(const char * filename, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
739 (*model) = (CvLSVMFilterObject ** )malloc((sizeof(CvLSVMFilterObject * )) * (*max));
741 //printf("parse : %s\n", filename);
743 xmlf = fopen(filename, "rb");
747 return LSVM_PARSER_FILE_NOT_FOUND;
755 ch = (char)fgetc( xmlf );
765 tagBuf[j + 1] = '\0';
766 if(getTeg(tagBuf) == MODEL){
767 parserModel(xmlf, model, last, max, comp, b, count, score);
780 return LATENT_SVM_OK;
784 const char *modelPath,
785 CvLSVMFilterObject ***filters,
790 float *scoreThreshold){
798 //printf("start_parse\n\n");
800 err = LSVMparser(modelPath, filters, &last, &max, &comp, b, &count, &score);
801 if(err != LATENT_SVM_OK){
804 (*kFilters) = last + 1;
805 (*kComponents) = count;
806 (*scoreThreshold) = (float) score;
808 (*kPartFilters) = (int *)malloc(sizeof(int) * count);
810 for(i = 1; i < count;i++){
811 (*kPartFilters)[i] = (comp[i] - comp[i - 1]) - 1;
813 (*kPartFilters)[0] = comp[0];