Leptonica  1.73
Image processing and image analysis suite
recogbasic.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
187 #include <string.h>
188 #include "allheaders.h"
189 
190 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20; /* n'import quoi */
191 static const l_int32 MAX_EXAMPLES_IN_CLASS = 256;
192 
193  /* Default recog parameters that can be changed */
194 static const l_int32 DEFAULT_CHARSET_TYPE = L_ARABIC_NUMERALS;
195 static const l_int32 DEFAULT_MIN_NOPAD = 1;
196 static const l_float32 DEFAULT_MAX_WH_RATIO = 3.0; /* max allowed w/h
197  ratio for a component to be split */
198 static const l_float32 DEFAULT_MAX_HT_RATIO = 2.6; /* max allowed ratio of
199  max/min unscaled averaged template heights */
200 static const l_int32 DEFAULT_THRESHOLD = 150; /* for binarization */
201 static const l_int32 DEFAULT_MAXYSHIFT = 1; /* for identification */
202 
203  /* Static functions */
204 static l_int32 recogGetCharsetSize(l_int32 type);
205 static l_int32 recogAddCharstrLabels(L_RECOG *recog);
206 static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug);
207 
208 
209 /*------------------------------------------------------------------------*
210  * Recog: initialization and destruction *
211  *------------------------------------------------------------------------*/
231 L_RECOG *
233  l_int32 scalew,
234  l_int32 scaleh,
235  l_int32 linew,
236  l_int32 threshold,
237  l_int32 maxyshift)
238 {
239 L_RECOG *recd;
240 PIXA *pixa;
241 
242  PROCNAME("recogCreateFromRecog");
243 
244  if (!recs)
245  return (L_RECOG *)ERROR_PTR("recs not defined", procName, NULL);
246 
247  pixa = recogExtractPixa(recs);
248  recd = recogCreateFromPixa(pixa, scalew, scaleh, linew, threshold,
249  maxyshift);
250  pixaDestroy(&pixa);
251  return recd;
252 }
253 
254 
278 L_RECOG *
280  l_int32 scalew,
281  l_int32 scaleh,
282  l_int32 linew,
283  l_int32 threshold,
284  l_int32 maxyshift)
285 {
286 L_RECOG *recog;
287 
288  PROCNAME("recogCreateFromPixa");
289 
290  if (!pixa)
291  return (L_RECOG *)ERROR_PTR("pixa not defined", procName, NULL);
292 
293  recog = recogCreateFromPixaNoFinish(pixa, scalew, scaleh, linew,
294  threshold, maxyshift);
295  if (!recog)
296  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
297 
298  recogTrainingFinished(&recog, 1, -1, -1.0);
299  if (!recog)
300  return (L_RECOG *)ERROR_PTR("bad templates", procName, NULL);
301  return recog;
302 }
303 
304 
324 L_RECOG *
326  l_int32 scalew,
327  l_int32 scaleh,
328  l_int32 linew,
329  l_int32 threshold,
330  l_int32 maxyshift)
331 {
332 char *text;
333 l_int32 full, n, i, ntext, same, maxd;
334 PIX *pix;
335 L_RECOG *recog;
336 
337  PROCNAME("recogCreateFromPixaNoFinish");
338 
339  if (!pixa)
340  return (L_RECOG *)ERROR_PTR("pixa not defined", procName, NULL);
341  pixaVerifyDepth(pixa, &same, &maxd);
342  if (maxd > 1)
343  return (L_RECOG *)ERROR_PTR("not all pix are 1 bpp", procName, NULL);
344 
345  pixaIsFull(pixa, &full, NULL);
346  if (!full)
347  return (L_RECOG *)ERROR_PTR("not all pix are present", procName, NULL);
348 
349  n = pixaGetCount(pixa);
350  pixaCountText(pixa, &ntext);
351  if (ntext == 0)
352  return (L_RECOG *)ERROR_PTR("no pix have text strings", procName, NULL);
353  if (ntext < n)
354  L_ERROR("%d text strings < %d pix\n", procName, ntext, n);
355 
356  recog = recogCreate(scalew, scaleh, linew, threshold, maxyshift);
357  if (!recog)
358  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
359  for (i = 0; i < n; i++) {
360  pix = pixaGetPix(pixa, i, L_CLONE);
361  text = pixGetText(pix);
362  if (!text || strlen(text) == 0) {
363  L_ERROR("pix[%d] has no text\n", procName, i);
364  pixDestroy(&pix);
365  continue;
366  }
367  recogTrainLabeled(recog, pix, NULL, text, 0);
368  pixDestroy(&pix);
369  }
370 
371  return recog;
372 }
373 
374 
405 L_RECOG *
406 recogCreate(l_int32 scalew,
407  l_int32 scaleh,
408  l_int32 linew,
409  l_int32 threshold,
410  l_int32 maxyshift)
411 {
412 L_RECOG *recog;
413 
414  PROCNAME("recogCreate");
415 
416  if (scalew < 0 || scaleh < 0)
417  return (L_RECOG *)ERROR_PTR("invalid scalew or scaleh", procName, NULL);
418  if (linew > 10)
419  return (L_RECOG *)ERROR_PTR("invalid linew > 10", procName, NULL);
420  if (threshold == 0) threshold = DEFAULT_THRESHOLD;
421  if (threshold < 0 || threshold > 255) {
422  L_WARNING("invalid threshold; using default\n", procName);
423  threshold = DEFAULT_THRESHOLD;
424  }
425  if (maxyshift < 0 || maxyshift > 2) {
426  L_WARNING("invalid maxyshift; using default value\n", procName);
427  maxyshift = DEFAULT_MAXYSHIFT;
428  } else if (maxyshift == 0) {
429  L_WARNING("Using maxyshift = 0; faster, worse correlation results\n",
430  procName);
431  } else if (maxyshift == 2) {
432  L_WARNING("Using maxyshift = 2; slower\n", procName);
433  }
434 
435  if ((recog = (L_RECOG *)LEPT_CALLOC(1, sizeof(L_RECOG))) == NULL)
436  return (L_RECOG *)ERROR_PTR("rec not made", procName, NULL);
437  recog->templ_use = L_USE_ALL_TEMPLATES; /* default */
438  recog->threshold = threshold;
439  recog->scalew = scalew;
440  recog->scaleh = scaleh;
441  recog->linew = linew;
442  recog->maxyshift = maxyshift;
443  recogSetParams(recog, 1, -1, -1.0, -1.0);
444  recog->bmf = bmfCreate(NULL, 6);
445  recog->bmf_size = 6;
446  recog->maxarraysize = MAX_EXAMPLES_IN_CLASS;
447 
448  /* Generate the LUTs */
449  recog->centtab = makePixelCentroidTab8();
450  recog->sumtab = makePixelSumTab8();
451  recog->sa_text = sarrayCreate(0);
452  recog->dna_tochar = l_dnaCreate(0);
453 
454  /* Input default values for min component size for splitting.
455  * These are overwritten when pixTrainingFinished() is called. */
456  recog->min_splitw = 6;
457  recog->max_splith = 60;
458 
459  /* Allocate the paa for the unscaled training bitmaps */
460  recog->pixaa_u = pixaaCreate(recog->maxarraysize);
461 
462  /* Generate the storage for debugging */
463  recog->pixadb_boot = pixaCreate(2);
464  recog->pixadb_split = pixaCreate(2);
465  return recog;
466 }
467 
468 
475 void
477 {
478 L_RECOG *recog;
479 
480  PROCNAME("recogDestroy");
481 
482  if (!precog) {
483  L_WARNING("ptr address is null\n", procName);
484  return;
485  }
486 
487  if ((recog = *precog) == NULL) return;
488 
489  LEPT_FREE(recog->centtab);
490  LEPT_FREE(recog->sumtab);
491  sarrayDestroy(&recog->sa_text);
492  l_dnaDestroy(&recog->dna_tochar);
493  pixaaDestroy(&recog->pixaa_u);
494  pixaDestroy(&recog->pixa_u);
495  ptaaDestroy(&recog->ptaa_u);
496  ptaDestroy(&recog->pta_u);
497  numaDestroy(&recog->nasum_u);
498  numaaDestroy(&recog->naasum_u);
499  pixaaDestroy(&recog->pixaa);
500  pixaDestroy(&recog->pixa);
501  ptaaDestroy(&recog->ptaa);
502  ptaDestroy(&recog->pta);
503  numaDestroy(&recog->nasum);
504  numaaDestroy(&recog->naasum);
505  pixaDestroy(&recog->pixa_tr);
506  pixaDestroy(&recog->pixadb_ave);
507  pixaDestroy(&recog->pixa_id);
508  pixDestroy(&recog->pixdb_ave);
509  pixDestroy(&recog->pixdb_range);
510  pixaDestroy(&recog->pixadb_boot);
511  pixaDestroy(&recog->pixadb_split);
512  bmfDestroy(&recog->bmf);
513  rchDestroy(&recog->rch);
514  rchaDestroy(&recog->rcha);
515  recogDestroyDid(recog);
516  LEPT_FREE(recog);
517  *precog = NULL;
518  return;
519 }
520 
521 
528 l_int32
530 {
531  PROCNAME("recogGetCount");
532 
533  if (!recog)
534  return ERROR_INT("recog not defined", procName, 0);
535  return recog->setsize;
536 }
537 
538 
566 l_int32
568  l_int32 type,
569  l_int32 min_nopad,
570  l_float32 max_wh_ratio,
571  l_float32 max_ht_ratio)
572 {
573 
574  PROCNAME("recogSetParams");
575 
576  if (!recog)
577  return ERROR_INT("recog not defined", procName, 1);
578 
579  recog->charset_type = (type >= 0) ? type : DEFAULT_CHARSET_TYPE;
581  recog->min_nopad = (min_nopad >= 0) ? min_nopad : DEFAULT_MIN_NOPAD;
582  recog->max_wh_ratio = (max_wh_ratio > 0.0) ? max_wh_ratio :
583  DEFAULT_MAX_WH_RATIO;
584  recog->max_ht_ratio = (max_ht_ratio > 1.0) ? max_ht_ratio :
585  DEFAULT_MAX_HT_RATIO;
586  return 0;
587 }
588 
589 
596 static l_int32
597 recogGetCharsetSize(l_int32 type)
598 {
599  PROCNAME("recogGetCharsetSize");
600 
601  switch (type) {
602  case L_UNKNOWN:
603  return 0;
604  case L_ARABIC_NUMERALS:
605  return 10;
606  case L_LC_ROMAN_NUMERALS:
607  return 7;
608  case L_UC_ROMAN_NUMERALS:
609  return 7;
610  case L_LC_ALPHA:
611  return 26;
612  case L_UC_ALPHA:
613  return 26;
614  default:
615  L_ERROR("invalid charset_type %d\n", procName, type);
616  return 0;
617  }
618  return 0; /* shouldn't happen */
619 }
620 
621 
622 /*------------------------------------------------------------------------*
623  * Character/index lookup *
624  *------------------------------------------------------------------------*/
649 l_int32
651  l_int32 val,
652  char *text,
653  l_int32 *pindex)
654 {
655 l_int32 i, n, ival;
656 
657  PROCNAME("recogGetClassIndex");
658 
659  if (!pindex)
660  return ERROR_INT("&index not defined", procName, 2);
661  *pindex = -1;
662  if (!recog)
663  return ERROR_INT("recog not defined", procName, 2);
664  if (!text)
665  return ERROR_INT("text not defined", procName, 2);
666 
667  /* Search existing characters */
668  n = l_dnaGetCount(recog->dna_tochar);
669  for (i = 0; i < n; i++) {
670  l_dnaGetIValue(recog->dna_tochar, i, &ival);
671  if (val == ival) { /* found */
672  *pindex = i;
673  return 0;
674  }
675  }
676 
677  /* If not found... */
678  l_dnaAddNumber(recog->dna_tochar, val);
679  sarrayAddString(recog->sa_text, text, L_COPY);
680  recog->setsize++;
681  *pindex = n;
682  return 1;
683 }
684 
685 
694 l_int32
696  char *text,
697  l_int32 *pindex)
698 {
699 char *charstr;
700 l_int32 i, n, diff;
701 
702  PROCNAME("recogStringtoIndex");
703 
704  if (!pindex)
705  return ERROR_INT("&index not defined", procName, 1);
706  *pindex = -1;
707  if (!recog)
708  return ERROR_INT("recog not defined", procName, 1);
709  if (!text)
710  return ERROR_INT("text not defined", procName, 1);
711 
712  /* Search existing characters */
713  n = recog->setsize;
714  for (i = 0; i < n; i++) {
715  recogGetClassString(recog, i, &charstr);
716  if (!charstr) {
717  L_ERROR("string not found for index %d\n", procName, i);
718  continue;
719  }
720  diff = strcmp(text, charstr);
721  LEPT_FREE(charstr);
722  if (diff) continue;
723  *pindex = i;
724  return 0;
725  }
726 
727  return 1; /* not found */
728 }
729 
730 
747 l_int32
749  l_int32 index,
750  char **pcharstr)
751 {
752  PROCNAME("recogGetClassString");
753 
754  if (!pcharstr)
755  return ERROR_INT("&charstr not defined", procName, 1);
756  *pcharstr = stringNew("");
757  if (!recog)
758  return ERROR_INT("recog not defined", procName, 2);
759 
760  if (index < 0 || index >= recog->setsize)
761  return ERROR_INT("invalid index", procName, 1);
762  LEPT_FREE(*pcharstr);
763  *pcharstr = sarrayGetString(recog->sa_text, index, L_COPY);
764  return 0;
765 }
766 
767 
777 l_int32
778 l_convertCharstrToInt(const char *str,
779  l_int32 *pval)
780 {
781 l_int32 size, val;
782 
783  PROCNAME("l_convertCharstrToInt");
784 
785  if (!pval)
786  return ERROR_INT("&val not defined", procName, 1);
787  *pval = 0;
788  if (!str)
789  return ERROR_INT("str not defined", procName, 1);
790  size = strlen(str);
791  if (size == 0)
792  return ERROR_INT("empty string", procName, 1);
793  if (size > 4)
794  return ERROR_INT("invalid string: > 4 bytes", procName, 1);
795 
796  val = (l_int32)str[0];
797  if (size > 1)
798  val = (val << 8) + (l_int32)str[1];
799  if (size > 2)
800  val = (val << 8) + (l_int32)str[2];
801  if (size > 3)
802  val = (val << 8) + (l_int32)str[3];
803  *pval = val;
804  return 0;
805 }
806 
807 
808 /*------------------------------------------------------------------------*
809  * Serialization *
810  *------------------------------------------------------------------------*/
836 L_RECOG *
837 recogRead(const char *filename)
838 {
839 FILE *fp;
840 L_RECOG *recog;
841 
842  PROCNAME("recogRead");
843 
844  if (!filename)
845  return (L_RECOG *)ERROR_PTR("filename not defined", procName, NULL);
846  if ((fp = fopenReadStream(filename)) == NULL)
847  return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL);
848 
849  if ((recog = recogReadStream(fp)) == NULL) {
850  fclose(fp);
851  return (L_RECOG *)ERROR_PTR("recog not read", procName, NULL);
852  }
853 
854  fclose(fp);
855  return recog;
856 }
857 
858 
865 L_RECOG *
867 {
868 l_int32 version, setsize, threshold, scalew, scaleh, linew;
869 l_int32 maxyshift, nc;
870 L_DNA *dna_tochar;
871 PIXAA *paa;
872 L_RECOG *recog;
873 SARRAY *sa_text;
874 
875  PROCNAME("recogReadStream");
876 
877  if (!fp)
878  return (L_RECOG *)ERROR_PTR("stream not defined", procName, NULL);
879 
880  if (fscanf(fp, "\nRecog Version %d\n", &version) != 1)
881  return (L_RECOG *)ERROR_PTR("not a recog file", procName, NULL);
882  if (version != RECOG_VERSION_NUMBER)
883  return (L_RECOG *)ERROR_PTR("invalid recog version", procName, NULL);
884  if (fscanf(fp, "Size of character set = %d\n", &setsize) != 1)
885  return (L_RECOG *)ERROR_PTR("setsize not read", procName, NULL);
886  if (fscanf(fp, "Binarization threshold = %d\n", &threshold) != 1)
887  return (L_RECOG *)ERROR_PTR("binary thresh not read", procName, NULL);
888  if (fscanf(fp, "Maxyshift = %d\n", &maxyshift) != 1)
889  return (L_RECOG *)ERROR_PTR("maxyshift not read", procName, NULL);
890  if (fscanf(fp, "Scale to width = %d\n", &scalew) != 1)
891  return (L_RECOG *)ERROR_PTR("width not read", procName, NULL);
892  if (fscanf(fp, "Scale to height = %d\n", &scaleh) != 1)
893  return (L_RECOG *)ERROR_PTR("height not read", procName, NULL);
894  if (fscanf(fp, "Normalized line width = %d\n", &linew) != 1)
895  return (L_RECOG *)ERROR_PTR("line width not read", procName, NULL);
896  if ((recog = recogCreate(scalew, scaleh, linew, threshold,
897  maxyshift)) == NULL)
898  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
899 
900  if (fscanf(fp, "\nLabels for character set:\n") != 0) {
901  recogDestroy(&recog);
902  return (L_RECOG *)ERROR_PTR("label intro not read", procName, NULL);
903  }
904  l_dnaDestroy(&recog->dna_tochar);
905  if ((dna_tochar = l_dnaReadStream(fp)) == NULL) {
906  recogDestroy(&recog);
907  return (L_RECOG *)ERROR_PTR("dna_tochar not read", procName, NULL);
908  }
909  recog->dna_tochar = dna_tochar;
910  sarrayDestroy(&recog->sa_text);
911  if ((sa_text = sarrayReadStream(fp)) == NULL) {
912  recogDestroy(&recog);
913  return (L_RECOG *)ERROR_PTR("sa_text not read", procName, NULL);
914  }
915  recog->sa_text = sa_text;
916 
917  if (fscanf(fp, "\nPixaa of all samples in the training set:\n") != 0) {
918  recogDestroy(&recog);
919  return (L_RECOG *)ERROR_PTR("pixaa intro not read", procName, NULL);
920  }
921  if ((paa = pixaaReadStream(fp)) == NULL) {
922  recogDestroy(&recog);
923  return (L_RECOG *)ERROR_PTR("pixaa not read", procName, NULL);
924  }
925  recog->setsize = setsize;
926  nc = pixaaGetCount(paa, NULL);
927  if (nc != setsize) {
928  recogDestroy(&recog);
929  pixaaDestroy(&paa);
930  L_ERROR("(setsize = %d) != (paa count = %d)\n", procName,
931  setsize, nc);
932  return NULL;
933  }
934 
935  recogAddAllSamples(&recog, paa, 0); /* this finishes */
936  pixaaDestroy(&paa);
937  if (!recog)
938  return (L_RECOG *)ERROR_PTR("bad templates", procName, NULL);
939  return recog;
940 }
941 
942 
950 L_RECOG *
951 recogReadMem(const l_uint8 *data,
952  size_t size)
953 {
954 FILE *fp;
955 L_RECOG *recog;
956 
957  PROCNAME("recogReadMem");
958 
959  if (!data)
960  return (L_RECOG *)ERROR_PTR("data not defined", procName, NULL);
961  if ((fp = fopenReadFromMemory(data, size)) == NULL)
962  return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL);
963 
964  recog = recogReadStream(fp);
965  fclose(fp);
966  if (!recog) L_ERROR("recog not read\n", procName);
967  return recog;
968 }
969 
970 
987 l_int32
988 recogWrite(const char *filename,
989  L_RECOG *recog)
990 {
991 l_int32 ret;
992 FILE *fp;
993 
994  PROCNAME("recogWrite");
995 
996  if (!filename)
997  return ERROR_INT("filename not defined", procName, 1);
998  if (!recog)
999  return ERROR_INT("recog not defined", procName, 1);
1000 
1001  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1002  return ERROR_INT("stream not opened", procName, 1);
1003  ret = recogWriteStream(fp, recog);
1004  fclose(fp);
1005  if (ret)
1006  return ERROR_INT("recog not written to stream", procName, 1);
1007  return 0;
1008 }
1009 
1010 
1018 l_int32
1020  L_RECOG *recog)
1021 {
1022  PROCNAME("recogWriteStream");
1023 
1024  if (!fp)
1025  return ERROR_INT("stream not defined", procName, 1);
1026  if (!recog)
1027  return ERROR_INT("recog not defined", procName, 1);
1028 
1029  fprintf(fp, "\nRecog Version %d\n", RECOG_VERSION_NUMBER);
1030  fprintf(fp, "Size of character set = %d\n", recog->setsize);
1031  fprintf(fp, "Binarization threshold = %d\n", recog->threshold);
1032  fprintf(fp, "Maxyshift = %d\n", recog->maxyshift);
1033  fprintf(fp, "Scale to width = %d\n", recog->scalew);
1034  fprintf(fp, "Scale to height = %d\n", recog->scaleh);
1035  fprintf(fp, "Normalized line width = %d\n", recog->linew);
1036  fprintf(fp, "\nLabels for character set:\n");
1037  l_dnaWriteStream(fp, recog->dna_tochar);
1038  sarrayWriteStream(fp, recog->sa_text);
1039  fprintf(fp, "\nPixaa of all samples in the training set:\n");
1040  pixaaWriteStream(fp, recog->pixaa);
1041 
1042  return 0;
1043 }
1044 
1045 
1059 l_int32
1060 recogWriteMem(l_uint8 **pdata,
1061  size_t *psize,
1062  L_RECOG *recog)
1063 {
1064 l_int32 ret;
1065 FILE *fp;
1066 
1067  PROCNAME("recogWriteMem");
1068 
1069  if (pdata) *pdata = NULL;
1070  if (psize) *psize = 0;
1071  if (!pdata)
1072  return ERROR_INT("&data not defined", procName, 1);
1073  if (!psize)
1074  return ERROR_INT("&size not defined", procName, 1);
1075  if (!recog)
1076  return ERROR_INT("recog not defined", procName, 1);
1077 
1078 #if HAVE_FMEMOPEN
1079  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1080  return ERROR_INT("stream not opened", procName, 1);
1081  ret = recogWriteStream(fp, recog);
1082 #else
1083  L_INFO("work-around: writing to a temp file\n", procName);
1084  #ifdef _WIN32
1085  if ((fp = fopenWriteWinTempfile()) == NULL)
1086  return ERROR_INT("tmpfile stream not opened", procName, 1);
1087  #else
1088  if ((fp = tmpfile()) == NULL)
1089  return ERROR_INT("tmpfile stream not opened", procName, 1);
1090  #endif /* _WIN32 */
1091  ret = recogWriteStream(fp, recog);
1092  rewind(fp);
1093  *pdata = l_binaryReadStream(fp, psize);
1094 #endif /* HAVE_FMEMOPEN */
1095  fclose(fp);
1096  return ret;
1097 }
1098 
1099 
1113 PIXA *
1115 {
1116  PROCNAME("recogExtractPixa");
1117 
1118  if (!recog)
1119  return (PIXA *)ERROR_PTR("recog not defined", procName, NULL);
1120 
1121  recogAddCharstrLabels(recog);
1122  return pixaaFlattenToPixa(recog->pixaa_u, NULL, L_CLONE);
1123 }
1124 
1125 
1132 static l_int32
1134 {
1135 char *text;
1136 l_int32 i, j, n1, n2;
1137 PIX *pix;
1138 PIXA *pixa;
1139 PIXAA *paa;
1140 
1141  PROCNAME("recogAddCharstrLabels");
1142 
1143  if (!recog)
1144  return ERROR_INT("recog not defined", procName, 1);
1145 
1146  /* Add the labels to each unscaled pix */
1147  paa = recog->pixaa_u;
1148  n1 = pixaaGetCount(paa, NULL);
1149  for (i = 0; i < n1; i++) {
1150  pixa = pixaaGetPixa(paa, i, L_CLONE);
1151  text = sarrayGetString(recog->sa_text, i, L_NOCOPY);
1152  n2 = pixaGetCount(pixa);
1153  for (j = 0; j < n2; j++) {
1154  pix = pixaGetPix(pixa, j, L_CLONE);
1155  pixSetText(pix, text);
1156  pixDestroy(&pix);
1157  }
1158  pixaDestroy(&pixa);
1159  }
1160 
1161  return 0;
1162 }
1163 
1164 
1184 static l_int32
1186  PIXAA *paa,
1187  l_int32 debug)
1188 {
1189 char *text;
1190 l_int32 i, j, nc, ns;
1191 PIX *pix;
1192 PIXA *pixa, *pixa1;
1193 L_RECOG *recog;
1194 
1195  PROCNAME("recogAddAllSamples");
1196 
1197  if (!precog)
1198  return ERROR_INT("&recog not defined", procName, 1);
1199  if ((recog = *precog) == NULL)
1200  return ERROR_INT("recog not defined", procName, 1);
1201  if (!paa) {
1202  recogDestroy(&recog);
1203  return ERROR_INT("paa not defined", procName, 1);
1204  }
1205 
1206  nc = pixaaGetCount(paa, NULL);
1207  for (i = 0; i < nc; i++) {
1208  pixa = pixaaGetPixa(paa, i, L_CLONE);
1209  ns = pixaGetCount(pixa);
1210  text = sarrayGetString(recog->sa_text, i, L_NOCOPY);
1211  pixa1 = pixaCreate(ns);
1212  pixaaAddPixa(recog->pixaa_u, pixa1, L_INSERT);
1213  for (j = 0; j < ns; j++) {
1214  pix = pixaGetPix(pixa, j, L_CLONE);
1215  if (debug) fprintf(stderr, "pix[%d,%d]: text = %s\n", i, j, text);
1216  pixaaAddPix(recog->pixaa_u, i, pix, NULL, L_INSERT);
1217  }
1218  pixaDestroy(&pixa);
1219  }
1220 
1221  recogTrainingFinished(&recog, 0, -1, -1.0); /* For second parameter,
1222  see comment in recogRead() */
1223  if (!recog)
1224  return ERROR_INT("bad templates; recog destroyed", procName, 1);
1225  return 0;
1226 }
l_int32 recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog)
recogWriteMem()
Definition: recogbasic.c:1060
struct Pixaa * pixaa_u
Definition: recog.h:152
void pixaaDestroy(PIXAA **ppaa)
pixaaDestroy()
Definition: pixabasic.c:1876
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
Definition: bmf.c:166
struct Pixa * pixadb_boot
Definition: recog.h:169
PIXAA * pixaaCreate(l_int32 n)
pixaaCreate()
Definition: pixabasic.c:1769
void rchaDestroy(L_RCHA **prcha)
rchaDestroy()
Definition: recogident.c:1168
L_RECOG * recogReadMem(const l_uint8 *data, size_t size)
recogReadMem()
Definition: recogbasic.c:951
l_int32 pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
Definition: pixabasic.c:1026
l_int32 threshold
Definition: recog.h:128
struct Pixa * pixa_u
Definition: recog.h:158
l_int32 recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
Definition: recogbasic.c:695
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
Definition: dnabasic.c:597
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
Definition: pix3.c:2343
l_int32 pixaaAddPixa(PIXAA *paa, PIXA *pixa, l_int32 copyflag)
pixaaAddPixa()
Definition: pixabasic.c:1920
struct Pta * pta
Definition: recog.h:162
l_int32 l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
Definition: dnabasic.c:439
l_int32 recogWriteStream(FILE *fp, L_RECOG *recog)
recogWriteStream()
Definition: recogbasic.c:1019
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:161
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
Definition: dnabasic.c:321
l_int32 l_dnaWriteStream(FILE *fp, L_DNA *da)
l_dnaWriteStream()
Definition: dnabasic.c:1087
Definition: recog.h:116
Definition: pix.h:704
l_int32 scalew
Definition: recog.h:117
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:202
struct Pixa * pixadb_split
Definition: recog.h:170
l_int32 pixaaAddPix(PIXAA *paa, l_int32 index, PIX *pix, BOX *box, l_int32 copyflag)
pixaaAddPix()
Definition: pixabasic.c:1989
L_RECOG * recogCreateFromPixa(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixa()
Definition: recogbasic.c:279
PIXAA * pixaaReadStream(FILE *fp)
pixaaReadStream()
Definition: pixabasic.c:2931
PIXA * pixaaGetPixa(PIXAA *paa, l_int32 index, l_int32 accesstype)
pixaaGetPixa()
Definition: pixabasic.c:2112
l_int32 linew
Definition: recog.h:121
l_int32 charset_type
Definition: recog.h:131
l_int32 recogGetClassString(L_RECOG *recog, l_int32 index, char **pcharstr)
recogGetClassString()
Definition: recogbasic.c:748
struct Numa * nasum
Definition: recog.h:163
struct Numa * nasum_u
Definition: recog.h:160
l_int32 recogSetParams(L_RECOG *recog, l_int32 type, l_int32 min_nopad, l_float32 max_wh_ratio, l_float32 max_ht_ratio)
recogSetParams()
Definition: recogbasic.c:567
l_int32 pixaaGetCount(PIXAA *paa, NUMA **pna)
pixaaGetCount()
Definition: pixabasic.c:2063
void rchDestroy(L_RCH **prch)
rchDestroy()
Definition: recogident.c:1242
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
Definition: pixafunc1.c:2093
struct Sarray * sa_text
Definition: recog.h:148
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:157
Definition: array.h:83
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1670
l_int32 sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
Definition: sarray1.c:1507
l_float32 max_wh_ratio
Definition: recog.h:144
struct Numaa * naasum
Definition: recog.h:157
l_int32 maxarraysize
Definition: recog.h:126
l_int32 l_convertCharstrToInt(const char *str, l_int32 *pval)
l_convertCharstrToInt()
Definition: recogbasic.c:778
struct L_Rcha * rcha
Definition: recog.h:175
l_int32 pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
Definition: pixabasic.c:929
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
Definition: numabasic.c:1410
Definition: array.h:116
struct Pixa * pixadb_ave
Definition: recog.h:165
l_int32 l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
Definition: dnabasic.c:693
l_int32 setsize
Definition: recog.h:127
static const l_int32 L_INSERT
Definition: pix.h:710
struct Numaa * naasum_u
Definition: recog.h:154
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
Definition: recogbasic.c:529
l_int32 pixaCountText(PIXA *pixa, l_int32 *pntext)
pixaCountText()
Definition: pixabasic.c:1076
l_int32 * sumtab
Definition: recog.h:151
struct Pix * pixdb_ave
Definition: recog.h:167
l_int32 max_splith
Definition: recog.h:147
L_RECOG * recogCreate(l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreate()
Definition: recogbasic.c:406
l_int32 scaleh
Definition: recog.h:119
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
Definition: ptabasic.c:933
struct L_Bmf * bmf
Definition: recog.h:171
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
Definition: pix3.c:2298
L_DNA * l_dnaReadStream(FILE *fp)
l_dnaReadStream()
Definition: dnabasic.c:1013
struct Pta * pta_u
Definition: recog.h:159
l_int32 recogTrainLabeled(L_RECOG *recog, PIX *pixs, BOX *box, char *text, l_int32 debug)
recogTrainLabeled()
Definition: recogtrain.c:212
l_int32 recogGetClassIndex(L_RECOG *recog, l_int32 val, char *text, l_int32 *pindex)
recogGetClassIndex()
Definition: recogbasic.c:650
l_float32 max_ht_ratio
Definition: recog.h:145
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1716
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:675
struct Ptaa * ptaa
Definition: recog.h:156
l_int32 sarrayAddString(SARRAY *sa, char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:439
struct Pixa * pixa_tr
Definition: recog.h:164
l_int32 size
Definition: recog.h:216
struct L_Dna * dna_tochar
Definition: recog.h:149
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:545
L_RECOG * recogRead(const char *filename)
recogRead()
Definition: recogbasic.c:837
static l_int32 recogGetCharsetSize(l_int32 type)
recogGetCharsetSize()
Definition: recogbasic.c:597
Definition: pix.h:454
struct Pix * pixdb_range
Definition: recog.h:168
void numaDestroy(NUMA **pna)
numaDestroy()
Definition: numabasic.c:359
Definition: pix.h:465
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1636
struct Pixaa * pixaa
Definition: recog.h:155
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1593
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1200
static l_int32 recogAddCharstrLabels(L_RECOG *recog)
recogAddCharstrLabels()
Definition: recogbasic.c:1133
struct Ptaa * ptaa_u
Definition: recog.h:153
l_int32 recogTrainingFinished(L_RECOG **precog, l_int32 modifyflag, l_int32 minsize, l_float32 minfract)
recogTrainingFinished()
Definition: recogtrain.c:783
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1442
l_int32 recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
Definition: recogbasic.c:988
l_int32 bmf_size
Definition: recog.h:172
PIXA * recogExtractPixa(L_RECOG *recog)
recogExtractPixa()
Definition: recogbasic.c:1114
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:660
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
Definition: dnabasic.c:169
Definition: pix.h:705
L_RECOG * recogCreateFromRecog(L_RECOG *recs, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromRecog()
Definition: recogbasic.c:232
L_RECOG * recogCreateFromPixaNoFinish(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixaNoFinish()
Definition: recogbasic.c:325
Definition: pix.h:134
l_int32 min_splitw
Definition: recog.h:146
Definition: pix.h:706
void ptaDestroy(PTA **ppta)
ptaDestroy()
Definition: ptabasic.c:191
l_int32 recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
Definition: recogdid.c:816
static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug)
recogAddAllSamples()
Definition: recogbasic.c:1185
l_int32 * centtab
Definition: recog.h:150
l_int32 maxyshift
Definition: recog.h:129
l_int32 charset_size
Definition: recog.h:132
l_int32 min_nopad
Definition: recog.h:133
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
Definition: pixabasic.c:398
struct Pixa * pixa
Definition: recog.h:161
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:620
struct Pixa * pixa_id
Definition: recog.h:166
L_RECOG * recogReadStream(FILE *fp)
recogReadStream()
Definition: recogbasic.c:866
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
Definition: sarray1.c:1375
void recogDestroy(L_RECOG **precog)
recogDestroy()
Definition: recogbasic.c:476
l_int32 pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1466
l_int32 templ_use
Definition: recog.h:123
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:349
l_int32 pixaaWriteStream(FILE *fp, PIXAA *paa)
pixaaWriteStream()
Definition: pixabasic.c:3064
struct L_Rch * rch
Definition: recog.h:174
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
Definition: bmf.c:114