52c4d825d1b47b387d396faa318d3c583d5311c8
[platform/upstream/opencv.git] / modules / objdetect / src / lsvmparser.cpp
1 #include "precomp.hpp"
2 #include <stdio.h>
3 #include "string.h"
4 #include "_lsvmparser.h"
5 #include "_lsvm_error.h"
6
7 namespace
8 {
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;
14     return 0;
15 }
16 int isP        (char *str){
17     char stag [] = "<P>";
18     char etag [] = "</P>";
19     if(strcmp(stag, str) == 0)return  P;
20     if(strcmp(etag, str) == 0)return EP;
21     return 0;
22 }
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;
28     return 0;
29 }
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;
35     return 0;
36 }
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;
42     return 0;
43 }
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;
49     return 0;
50 }
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;
56     return 0;
57 }
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;
63     return 0;
64 }
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;
70     return 0;
71 }
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;
77     return 0;
78 }
79 int isV        (char *str){
80     char stag [] = "<V>";
81     char etag [] = "</V>";
82     if(strcmp(stag, str) == 0)return  TAGV;
83     if(strcmp(etag, str) == 0)return ETAGV;
84     return 0;
85 }
86 int isVx       (char *str){
87     char stag [] = "<Vx>";
88     char etag [] = "</Vx>";
89     if(strcmp(stag, str) == 0)return  Vx;
90     if(strcmp(etag, str) == 0)return EVx;
91     return 0;
92 }
93 int isVy       (char *str){
94     char stag [] = "<Vy>";
95     char etag [] = "</Vy>";
96     if(strcmp(stag, str) == 0)return  Vy;
97     if(strcmp(etag, str) == 0)return EVy;
98     return 0;
99 }
100 int isD        (char *str){
101     char stag [] = "<Penalty>";
102     char etag [] = "</Penalty>";
103     if(strcmp(stag, str) == 0)return  TAGD;
104     if(strcmp(etag, str) == 0)return ETAGD;
105     return 0;
106 }
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;
112     return 0;
113 }
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;
119     return 0;
120 }
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;
126     return 0;
127 }
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;
133     return 0;
134 }
135 int isB      (char *str){
136     char stag [] = "<LinearTerm>";
137     char etag [] = "</LinearTerm>";
138     if(strcmp(stag, str) == 0)return  BTAG;
139     if(strcmp(etag, str) == 0)return EBTAG;
140     return 0;
141 }
142
143 int getTeg(char *str){
144     int sum = 0;
145     sum = isMODEL (str)+
146     isP        (str)+
147     isSCORE    (str)+
148     isCOMP     (str)+
149     isRFILTER  (str)+
150     isPFILTERs (str)+
151     isPFILTER  (str)+
152     isSIZEX    (str)+
153     isSIZEY    (str)+
154     isWEIGHTS  (str)+
155     isV        (str)+
156     isVx       (str)+
157     isVy       (str)+
158     isD        (str)+
159     isDx       (str)+
160     isDy       (str)+
161     isDxx      (str)+
162     isDyy      (str)+
163     isB        (str);
164
165     return sum;
166 }
167
168 void addFilter(CvLSVMFilterObject *** model, int *last, int *max){
169     CvLSVMFilterObject ** nmodel;
170     int i;
171     (*last) ++;
172     if((*last) >= (*max)){
173         (*max) += 10;
174         nmodel = (CvLSVMFilterObject **)malloc(sizeof(CvLSVMFilterObject *) * (*max));
175         for(i = 0; i < *last; i++){
176             nmodel[i] = (* model)[i];
177         }
178         free(* model);
179         (*model) = nmodel;
180     }
181     (*model) [(*last)] = (CvLSVMFilterObject *)malloc(sizeof(CvLSVMFilterObject));
182 }
183
184 void parserRFilter  (FILE * xmlf, int p, CvLSVMFilterObject * model, float *b){
185     int st = 0;
186     int sizeX=0, sizeY=0;
187     int tag;
188     int tagVal;
189     char ch;
190     int i,j,ii;
191     char buf[1024];
192     char tagBuf[1024];
193     double *data;
194     //printf("<RootFilter>\n");
195
196     model->V.x = 0;
197     model->V.y = 0;
198     model->V.l = 0;
199     model->fineFunction[0] = 0.0;
200     model->fineFunction[1] = 0.0;
201     model->fineFunction[2] = 0.0;
202     model->fineFunction[3] = 0.0;
203
204     i   = 0;
205     j   = 0;
206     st  = 0;
207     tag = 0;
208     while(!feof(xmlf)){
209         ch = (char)fgetc( xmlf );
210         if(ch == '<'){
211             tag = 1;
212             j   = 1;
213             tagBuf[j - 1] = ch;
214         }else {
215             if(ch == '>'){
216                 tagBuf[j    ] = ch;
217                 tagBuf[j + 1] = '\0';
218
219                 tagVal = getTeg(tagBuf);
220
221                 if(tagVal == ERFILTER){
222                     //printf("</RootFilter>\n");
223                     return;
224                 }
225                 if(tagVal == SIZEX){
226                     st = 1;
227                     i = 0;
228                 }
229                 if(tagVal == ESIZEX){
230                     st = 0;
231                     buf[i] = '\0';
232                     sizeX = atoi(buf);
233                     model->sizeX = sizeX;
234                     //printf("<sizeX>%d</sizeX>\n", sizeX);
235                 }
236                 if(tagVal == SIZEY){
237                     st = 1;
238                     i = 0;
239                 }
240                 if(tagVal == ESIZEY){
241                     st = 0;
242                     buf[i] = '\0';
243                     sizeY = atoi(buf);
244                     model->sizeY = sizeY;
245                     //printf("<sizeY>%d</sizeY>\n", sizeY);
246                 }
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];
254                     }
255                     free(data);
256                 }
257                 if(tagVal == EWEIGHTS){
258                     //printf("WEIGHTS OK\n");
259                 }
260                 if(tagVal == BTAG){
261                     st = 1;
262                     i = 0;
263                 }
264                 if(tagVal == EBTAG){
265                     st = 0;
266                     buf[i] = '\0';
267                     *b =(float) atof(buf);
268                     //printf("<B>%f</B>\n", *b);
269                 }
270
271                 tag = 0;
272                 i   = 0;
273             }else{
274                 if((tag == 0)&& (st == 1)){
275                     buf[i] = ch; i++;
276                 }else{
277                     tagBuf[j] = ch; j++;
278                 }
279             }
280         }
281     }
282 }
283
284 void parserV  (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
285     int st = 0;
286     int tag;
287     int tagVal;
288     char ch;
289     int i,j;
290     char buf[1024];
291     char tagBuf[1024];
292     //printf("    <V>\n");
293
294     i   = 0;
295     j   = 0;
296     st  = 0;
297     tag = 0;
298     while(!feof(xmlf)){
299         ch = (char)fgetc( xmlf );
300         if(ch == '<'){
301             tag = 1;
302             j   = 1;
303             tagBuf[j - 1] = ch;
304         }else {
305             if(ch == '>'){
306                 tagBuf[j    ] = ch;
307                 tagBuf[j + 1] = '\0';
308
309                 tagVal = getTeg(tagBuf);
310
311                 if(tagVal == ETAGV){
312                     //printf("    </V>\n");
313                     return;
314                 }
315                 if(tagVal == Vx){
316                     st = 1;
317                     i = 0;
318                 }
319                 if(tagVal == EVx){
320                     st = 0;
321                     buf[i] = '\0';
322                     model->V.x = atoi(buf);
323                     //printf("        <Vx>%d</Vx>\n", model->V.x);
324                 }
325                 if(tagVal == Vy){
326                     st = 1;
327                     i = 0;
328                 }
329                 if(tagVal == EVy){
330                     st = 0;
331                     buf[i] = '\0';
332                     model->V.y = atoi(buf);
333                     //printf("        <Vy>%d</Vy>\n", model->V.y);
334                 }
335                 tag = 0;
336                 i   = 0;
337             }else{
338                 if((tag == 0)&& (st == 1)){
339                     buf[i] = ch; i++;
340                 }else{
341                     tagBuf[j] = ch; j++;
342                 }
343             }
344         }
345     }
346 }
347 void parserD  (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
348     int st = 0;
349     int tag;
350     int tagVal;
351     char ch;
352     int i,j;
353     char buf[1024];
354     char tagBuf[1024];
355     //printf("    <D>\n");
356
357     i   = 0;
358     j   = 0;
359     st  = 0;
360     tag = 0;
361     while(!feof(xmlf)){
362         ch = (char)fgetc( xmlf );
363         if(ch == '<'){
364             tag = 1;
365             j   = 1;
366             tagBuf[j - 1] = ch;
367         }else {
368             if(ch == '>'){
369                 tagBuf[j    ] = ch;
370                 tagBuf[j + 1] = '\0';
371
372                 tagVal = getTeg(tagBuf);
373
374                 if(tagVal == ETAGD){
375                     //printf("    </D>\n");
376                     return;
377                 }
378                 if(tagVal == Dx){
379                     st = 1;
380                     i = 0;
381                 }
382                 if(tagVal == EDx){
383                     st = 0;
384                     buf[i] = '\0';
385
386                     model->fineFunction[0] = (float)atof(buf);
387                     //printf("        <Dx>%f</Dx>\n", model->fineFunction[0]);
388                 }
389                 if(tagVal == Dy){
390                     st = 1;
391                     i = 0;
392                 }
393                 if(tagVal == EDy){
394                     st = 0;
395                     buf[i] = '\0';
396
397                     model->fineFunction[1] = (float)atof(buf);
398                     //printf("        <Dy>%f</Dy>\n", model->fineFunction[1]);
399                 }
400                 if(tagVal == Dxx){
401                     st = 1;
402                     i = 0;
403                 }
404                 if(tagVal == EDxx){
405                     st = 0;
406                     buf[i] = '\0';
407
408                     model->fineFunction[2] = (float)atof(buf);
409                     //printf("        <Dxx>%f</Dxx>\n", model->fineFunction[2]);
410                 }
411                 if(tagVal == Dyy){
412                     st = 1;
413                     i = 0;
414                 }
415                 if(tagVal == EDyy){
416                     st = 0;
417                     buf[i] = '\0';
418
419                     model->fineFunction[3] = (float)atof(buf);
420                     //printf("        <Dyy>%f</Dyy>\n", model->fineFunction[3]);
421                 }
422
423                 tag = 0;
424                 i   = 0;
425             }else{
426                 if((tag == 0)&& (st == 1)){
427                     buf[i] = ch; i++;
428                 }else{
429                     tagBuf[j] = ch; j++;
430                 }
431             }
432         }
433     }
434 }
435
436 void parserPFilter  (FILE * xmlf, int p, int /*N_path*/, CvLSVMFilterObject * model){
437     int st = 0;
438     int sizeX=0, sizeY=0;
439     int tag;
440     int tagVal;
441     char ch;
442     int i,j, ii;
443     char buf[1024];
444     char tagBuf[1024];
445     double *data;
446     //printf("<PathFilter> (%d)\n", N_path);
447
448     model->V.x = 0;
449     model->V.y = 0;
450     model->V.l = 0;
451     model->fineFunction[0] = 0.0f;
452     model->fineFunction[1] = 0.0f;
453     model->fineFunction[2] = 0.0f;
454     model->fineFunction[3] = 0.0f;
455
456     i   = 0;
457     j   = 0;
458     st  = 0;
459     tag = 0;
460     while(!feof(xmlf)){
461         ch = (char)fgetc( xmlf );
462         if(ch == '<'){
463             tag = 1;
464             j   = 1;
465             tagBuf[j - 1] = ch;
466         }else {
467             if(ch == '>'){
468                 tagBuf[j    ] = ch;
469                 tagBuf[j + 1] = '\0';
470
471                 tagVal = getTeg(tagBuf);
472
473                 if(tagVal == EPFILTER){
474                     //printf("</PathFilter>\n");
475                     return;
476                 }
477
478                 if(tagVal == TAGV){
479                     parserV(xmlf, p, model);
480                 }
481                 if(tagVal == TAGD){
482                     parserD(xmlf, p, model);
483                 }
484                 if(tagVal == SIZEX){
485                     st = 1;
486                     i = 0;
487                 }
488                 if(tagVal == ESIZEX){
489                     st = 0;
490                     buf[i] = '\0';
491                     sizeX = atoi(buf);
492                     model->sizeX = sizeX;
493                     //printf("<sizeX>%d</sizeX>\n", sizeX);
494                 }
495                 if(tagVal == SIZEY){
496                     st = 1;
497                     i = 0;
498                 }
499                 if(tagVal == ESIZEY){
500                     st = 0;
501                     buf[i] = '\0';
502                     sizeY = atoi(buf);
503                     model->sizeY = sizeY;
504                     //printf("<sizeY>%d</sizeY>\n", sizeY);
505                 }
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];
513                     }
514                     free(data);
515                 }
516                 if(tagVal == EWEIGHTS){
517                     //printf("WEIGHTS OK\n");
518                 }
519                 tag = 0;
520                 i   = 0;
521             }else{
522                 if((tag == 0)&& (st == 1)){
523                     buf[i] = ch; i++;
524                 }else{
525                     tagBuf[j] = ch; j++;
526                 }
527             }
528         }
529     }
530 }
531 void parserPFilterS (FILE * xmlf, int p, CvLSVMFilterObject *** model, int *last, int *max){
532     int st = 0;
533     int N_path = 0;
534     int tag;
535     int tagVal;
536     char ch;
537     int /*i,*/j;
538     //char buf[1024];
539     char tagBuf[1024];
540     //printf("<PartFilters>\n");
541
542     //i   = 0;
543     j   = 0;
544     st  = 0;
545     tag = 0;
546     while(!feof(xmlf)){
547         ch = (char)fgetc( xmlf );
548         if(ch == '<'){
549             tag = 1;
550             j   = 1;
551             tagBuf[j - 1] = ch;
552         }else {
553             if(ch == '>'){
554                 tagBuf[j    ] = ch;
555                 tagBuf[j + 1] = '\0';
556
557                 tagVal = getTeg(tagBuf);
558
559                 if(tagVal == EPFILTERs){
560                     //printf("</PartFilters>\n");
561                     return;
562                 }
563                 if(tagVal == PFILTER){
564                     addFilter(model, last, max);
565                     parserPFilter  (xmlf, p, N_path, (*model)[*last]);
566                     N_path++;
567                 }
568                 tag = 0;
569                 //i   = 0;
570             }else{
571                 if((tag == 0)&& (st == 1)){
572                     //buf[i] = ch; i++;
573                 }else{
574                     tagBuf[j] = ch; j++;
575                 }
576             }
577         }
578     }
579 }
580 void parserComp (FILE * xmlf, int p, int *N_comp, CvLSVMFilterObject *** model, float *b, int *last, int *max){
581     int st = 0;
582     int tag;
583     int tagVal;
584     char ch;
585     int /*i,*/j;
586     //char buf[1024];
587     char tagBuf[1024];
588     //printf("<Component> %d\n", *N_comp);
589
590     //i   = 0;
591     j   = 0;
592     st  = 0;
593     tag = 0;
594     while(!feof(xmlf)){
595         ch = (char)fgetc( xmlf );
596         if(ch == '<'){
597             tag = 1;
598             j   = 1;
599             tagBuf[j - 1] = ch;
600         }else {
601             if(ch == '>'){
602                 tagBuf[j    ] = ch;
603                 tagBuf[j + 1] = '\0';
604
605                 tagVal = getTeg(tagBuf);
606
607                 if(tagVal == ECOMP){
608                     (*N_comp) ++;
609                     return;
610                 }
611                 if(tagVal == RFILTER){
612                     addFilter(model, last, max);
613                     parserRFilter   (xmlf, p, (*model)[*last],b);
614                 }
615                 if(tagVal == PFILTERs){
616                     parserPFilterS  (xmlf, p, model, last, max);
617                 }
618                 tag = 0;
619                 //i   = 0;
620             }else{
621                 if((tag == 0)&& (st == 1)){
622                     //buf[i] = ch; i++;
623                 }else{
624                     tagBuf[j] = ch; j++;
625                 }
626             }
627         }
628     }
629 }
630 void parserModel(FILE * xmlf, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
631     int p = 0;
632     int N_comp = 0;
633     int * cmp;
634     float *bb;
635     int st = 0;
636     int tag;
637     int tagVal;
638     char ch;
639     int i,j, ii = 0;
640     char buf[1024];
641     char tagBuf[1024];
642
643     //printf("<Model>\n");
644
645     i   = 0;
646     j   = 0;
647     st  = 0;
648     tag = 0;
649     while(!feof(xmlf)){
650         ch = (char)fgetc( xmlf );
651         if(ch == '<'){
652             tag = 1;
653             j   = 1;
654             tagBuf[j - 1] = ch;
655         }else {
656             if(ch == '>'){
657                 tagBuf[j    ] = ch;
658                 tagBuf[j + 1] = '\0';
659
660                 tagVal = getTeg(tagBuf);
661
662                 if(tagVal == EMODEL){
663                     //printf("</Model>\n");
664                     for(ii = 0; ii <= *last; ii++){
665                         (*model)[ii]->numFeatures = p;
666                     }
667                     * count = N_comp;
668                     return;
669                 }
670                 if(tagVal == COMP){
671                     if(N_comp == 0){
672                         cmp = (int    *)malloc(sizeof(int));
673                         bb  = (float *)malloc(sizeof(float));
674                         * comp = cmp;
675                         * b    = bb;
676                         * count = N_comp + 1;
677                     } else {
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];
683                         }
684                         free(* comp);
685                         free(* b   );
686                         * comp = cmp;
687                         * b    = bb;
688                         * count = N_comp + 1;
689                     }
690                     parserComp(xmlf, p, &N_comp, model, &((*b)[N_comp]), last, max);
691                     cmp[N_comp - 1] = *last;
692                 }
693                 if(tagVal == P){
694                     st = 1;
695                     i = 0;
696                 }
697                 if(tagVal == EP){
698                     st = 0;
699                     buf[i] = '\0';
700                     p = atoi(buf);
701                     //printf("<P>%d</P>\n", p);
702                 }
703                 if(tagVal == SCORE){
704                     st = 1;
705                     i = 0;
706                 }
707                 if(tagVal == ESCORE){
708                     st = 0;
709                     buf[i] = '\0';
710                     *score = (float)atof(buf);
711                     //printf("<ScoreThreshold>%f</ScoreThreshold>\n", score);
712                 }
713                 tag = 0;
714                 i   = 0;
715             }else{
716                 if((tag == 0)&& (st == 1)){
717                     buf[i] = ch; i++;
718                 }else{
719                     tagBuf[j] = ch; j++;
720                 }
721             }
722         }
723     }
724 }
725
726 }//namespace
727
728 int LSVMparser(const char * filename, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
729     //int st = 0;
730     int tag;
731     char ch;
732     int /*i,*/j;
733     FILE *xmlf;
734     //char buf[1024];
735     char tagBuf[1024];
736
737     (*max) = 10;
738     (*last) = -1;
739     (*model) = (CvLSVMFilterObject ** )malloc((sizeof(CvLSVMFilterObject * )) * (*max));
740
741     //printf("parse : %s\n", filename);
742
743     xmlf = fopen(filename, "rb");
744     if(xmlf == NULL) {
745         free(*model);
746         *model = NULL;
747         return LSVM_PARSER_FILE_NOT_FOUND;
748     }
749
750     //i   = 0;
751     j   = 0;
752     //st  = 0;
753     tag = 0;
754     while(!feof(xmlf)){
755         ch = (char)fgetc( xmlf );
756         if(ch == '<'){
757             tag = 1;
758             j   = 1;
759             tagBuf[j - 1] = ch;
760         }else {
761             if(ch == '>'){
762                 tag = 0;
763                 //i   = 0;
764                 tagBuf[j    ] = ch;
765                 tagBuf[j + 1] = '\0';
766                 if(getTeg(tagBuf) == MODEL){
767                     parserModel(xmlf, model, last, max, comp, b, count, score);
768                 }
769             }else{
770                 if(tag == 0){
771                     //buf[i] = ch; i++;
772                 }else{
773                     tagBuf[j] = ch; j++;
774                 }
775             }
776         }
777     }
778
779     fclose(xmlf);
780     return LATENT_SVM_OK;
781 }
782
783 int loadModel(
784               const char *modelPath,
785               CvLSVMFilterObject ***filters,
786               int *kFilters,
787               int *kComponents,
788               int **kPartFilters,
789               float **b,
790               float *scoreThreshold){
791     int last;
792     int max;
793     int *comp = NULL;
794     int count;
795     int i;
796     int err;
797     float score;
798     //printf("start_parse\n\n");
799
800     err = LSVMparser(modelPath, filters, &last, &max, &comp, b, &count, &score);
801     if(err != LATENT_SVM_OK){
802         return err;
803     }
804     (*kFilters)       = last + 1;
805     (*kComponents)    = count;
806     (*scoreThreshold) = (float) score;
807
808     (*kPartFilters) = (int *)malloc(sizeof(int) * count);
809
810     for(i = 1; i < count;i++){
811         (*kPartFilters)[i] = (comp[i] - comp[i - 1]) - 1;
812     }
813     (*kPartFilters)[0] = comp[0];
814     free(comp);
815
816     return 0;
817 }