Leptonica  1.77.0
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 
189 #include <string.h>
190 #include "allheaders.h"
191 
192 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20; /* n'import quoi */
193 static const l_int32 MAX_EXAMPLES_IN_CLASS = 256;
194 
195  /* Default recog parameters that can be changed */
196 static const l_int32 DEFAULT_CHARSET_TYPE = L_ARABIC_NUMERALS;
197 static const l_int32 DEFAULT_MIN_NOPAD = 1;
198 static const l_float32 DEFAULT_MAX_WH_RATIO = 3.0; /* max allowed w/h
199  ratio for a component to be split */
200 static const l_float32 DEFAULT_MAX_HT_RATIO = 2.6; /* max allowed ratio of
201  max/min unscaled averaged template heights */
202 static const l_int32 DEFAULT_THRESHOLD = 150; /* for binarization */
203 static const l_int32 DEFAULT_MAXYSHIFT = 1; /* for identification */
204 
205  /* Static functions */
206 static l_int32 recogGetCharsetSize(l_int32 type);
207 static l_int32 recogAddCharstrLabels(L_RECOG *recog);
208 static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug);
209 
210 
211 /*------------------------------------------------------------------------*
212  * Recog: initialization and destruction *
213  *------------------------------------------------------------------------*/
233 L_RECOG *
235  l_int32 scalew,
236  l_int32 scaleh,
237  l_int32 linew,
238  l_int32 threshold,
239  l_int32 maxyshift)
240 {
241 L_RECOG *recd;
242 PIXA *pixa;
243 
244  PROCNAME("recogCreateFromRecog");
245 
246  if (!recs)
247  return (L_RECOG *)ERROR_PTR("recs not defined", procName, NULL);
248 
249  pixa = recogExtractPixa(recs);
250  recd = recogCreateFromPixa(pixa, scalew, scaleh, linew, threshold,
251  maxyshift);
252  pixaDestroy(&pixa);
253  return recd;
254 }
255 
256 
280 L_RECOG *
282  l_int32 scalew,
283  l_int32 scaleh,
284  l_int32 linew,
285  l_int32 threshold,
286  l_int32 maxyshift)
287 {
288 L_RECOG *recog;
289 
290  PROCNAME("recogCreateFromPixa");
291 
292  if (!pixa)
293  return (L_RECOG *)ERROR_PTR("pixa not defined", procName, NULL);
294 
295  recog = recogCreateFromPixaNoFinish(pixa, scalew, scaleh, linew,
296  threshold, maxyshift);
297  if (!recog)
298  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
299 
300  recogTrainingFinished(&recog, 1, -1, -1.0);
301  if (!recog)
302  return (L_RECOG *)ERROR_PTR("bad templates", procName, NULL);
303  return recog;
304 }
305 
306 
326 L_RECOG *
328  l_int32 scalew,
329  l_int32 scaleh,
330  l_int32 linew,
331  l_int32 threshold,
332  l_int32 maxyshift)
333 {
334 char *text;
335 l_int32 full, n, i, ntext, same, maxd;
336 PIX *pix;
337 L_RECOG *recog;
338 
339  PROCNAME("recogCreateFromPixaNoFinish");
340 
341  if (!pixa)
342  return (L_RECOG *)ERROR_PTR("pixa not defined", procName, NULL);
343  pixaVerifyDepth(pixa, &same, &maxd);
344  if (maxd > 1)
345  return (L_RECOG *)ERROR_PTR("not all pix are 1 bpp", procName, NULL);
346 
347  pixaIsFull(pixa, &full, NULL);
348  if (!full)
349  return (L_RECOG *)ERROR_PTR("not all pix are present", procName, NULL);
350 
351  n = pixaGetCount(pixa);
352  pixaCountText(pixa, &ntext);
353  if (ntext == 0)
354  return (L_RECOG *)ERROR_PTR("no pix have text strings", procName, NULL);
355  if (ntext < n)
356  L_ERROR("%d text strings < %d pix\n", procName, ntext, n);
357 
358  recog = recogCreate(scalew, scaleh, linew, threshold, maxyshift);
359  if (!recog)
360  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
361  for (i = 0; i < n; i++) {
362  pix = pixaGetPix(pixa, i, L_CLONE);
363  text = pixGetText(pix);
364  if (!text || strlen(text) == 0) {
365  L_ERROR("pix[%d] has no text\n", procName, i);
366  pixDestroy(&pix);
367  continue;
368  }
369  recogTrainLabeled(recog, pix, NULL, text, 0);
370  pixDestroy(&pix);
371  }
372 
373  return recog;
374 }
375 
376 
407 L_RECOG *
408 recogCreate(l_int32 scalew,
409  l_int32 scaleh,
410  l_int32 linew,
411  l_int32 threshold,
412  l_int32 maxyshift)
413 {
414 L_RECOG *recog;
415 
416  PROCNAME("recogCreate");
417 
418  if (scalew < 0 || scaleh < 0)
419  return (L_RECOG *)ERROR_PTR("invalid scalew or scaleh", procName, NULL);
420  if (linew > 10)
421  return (L_RECOG *)ERROR_PTR("invalid linew > 10", procName, NULL);
422  if (threshold == 0) threshold = DEFAULT_THRESHOLD;
423  if (threshold < 0 || threshold > 255) {
424  L_WARNING("invalid threshold; using default\n", procName);
425  threshold = DEFAULT_THRESHOLD;
426  }
427  if (maxyshift < 0 || maxyshift > 2) {
428  L_WARNING("invalid maxyshift; using default value\n", procName);
429  maxyshift = DEFAULT_MAXYSHIFT;
430  } else if (maxyshift == 0) {
431  L_WARNING("Using maxyshift = 0; faster, worse correlation results\n",
432  procName);
433  } else if (maxyshift == 2) {
434  L_WARNING("Using maxyshift = 2; slower\n", procName);
435  }
436 
437  if ((recog = (L_RECOG *)LEPT_CALLOC(1, sizeof(L_RECOG))) == NULL)
438  return (L_RECOG *)ERROR_PTR("rec not made", procName, NULL);
439  recog->templ_use = L_USE_ALL_TEMPLATES; /* default */
440  recog->threshold = threshold;
441  recog->scalew = scalew;
442  recog->scaleh = scaleh;
443  recog->linew = linew;
444  recog->maxyshift = maxyshift;
445  recogSetParams(recog, 1, -1, -1.0, -1.0);
446  recog->bmf = bmfCreate(NULL, 6);
447  recog->bmf_size = 6;
448  recog->maxarraysize = MAX_EXAMPLES_IN_CLASS;
449 
450  /* Generate the LUTs */
451  recog->centtab = makePixelCentroidTab8();
452  recog->sumtab = makePixelSumTab8();
453  recog->sa_text = sarrayCreate(0);
454  recog->dna_tochar = l_dnaCreate(0);
455 
456  /* Input default values for min component size for splitting.
457  * These are overwritten when pixTrainingFinished() is called. */
458  recog->min_splitw = 6;
459  recog->max_splith = 60;
460 
461  /* Allocate the paa for the unscaled training bitmaps */
462  recog->pixaa_u = pixaaCreate(recog->maxarraysize);
463 
464  /* Generate the storage for debugging */
465  recog->pixadb_boot = pixaCreate(2);
466  recog->pixadb_split = pixaCreate(2);
467  return recog;
468 }
469 
470 
477 void
479 {
480 L_RECOG *recog;
481 
482  PROCNAME("recogDestroy");
483 
484  if (!precog) {
485  L_WARNING("ptr address is null\n", procName);
486  return;
487  }
488 
489  if ((recog = *precog) == NULL) return;
490 
491  LEPT_FREE(recog->centtab);
492  LEPT_FREE(recog->sumtab);
493  sarrayDestroy(&recog->sa_text);
494  l_dnaDestroy(&recog->dna_tochar);
495  pixaaDestroy(&recog->pixaa_u);
496  pixaDestroy(&recog->pixa_u);
497  ptaaDestroy(&recog->ptaa_u);
498  ptaDestroy(&recog->pta_u);
499  numaDestroy(&recog->nasum_u);
500  numaaDestroy(&recog->naasum_u);
501  pixaaDestroy(&recog->pixaa);
502  pixaDestroy(&recog->pixa);
503  ptaaDestroy(&recog->ptaa);
504  ptaDestroy(&recog->pta);
505  numaDestroy(&recog->nasum);
506  numaaDestroy(&recog->naasum);
507  pixaDestroy(&recog->pixa_tr);
508  pixaDestroy(&recog->pixadb_ave);
509  pixaDestroy(&recog->pixa_id);
510  pixDestroy(&recog->pixdb_ave);
511  pixDestroy(&recog->pixdb_range);
512  pixaDestroy(&recog->pixadb_boot);
513  pixaDestroy(&recog->pixadb_split);
514  bmfDestroy(&recog->bmf);
515  rchDestroy(&recog->rch);
516  rchaDestroy(&recog->rcha);
517  recogDestroyDid(recog);
518  LEPT_FREE(recog);
519  *precog = NULL;
520  return;
521 }
522 
523 
524 /*------------------------------------------------------------------------*
525  * Recog accessors *
526  *------------------------------------------------------------------------*/
533 l_int32
535 {
536  PROCNAME("recogGetCount");
537 
538  if (!recog)
539  return ERROR_INT("recog not defined", procName, 0);
540  return recog->setsize;
541 }
542 
543 
571 l_ok
573  l_int32 type,
574  l_int32 min_nopad,
575  l_float32 max_wh_ratio,
576  l_float32 max_ht_ratio)
577 {
578  PROCNAME("recogSetParams");
579 
580  if (!recog)
581  return ERROR_INT("recog not defined", procName, 1);
582 
583  recog->charset_type = (type >= 0) ? type : DEFAULT_CHARSET_TYPE;
585  recog->min_nopad = (min_nopad >= 0) ? min_nopad : DEFAULT_MIN_NOPAD;
586  recog->max_wh_ratio = (max_wh_ratio > 0.0) ? max_wh_ratio :
587  DEFAULT_MAX_WH_RATIO;
588  recog->max_ht_ratio = (max_ht_ratio > 1.0) ? max_ht_ratio :
589  DEFAULT_MAX_HT_RATIO;
590  return 0;
591 }
592 
593 
600 static l_int32
601 recogGetCharsetSize(l_int32 type)
602 {
603  PROCNAME("recogGetCharsetSize");
604 
605  switch (type) {
606  case L_UNKNOWN:
607  return 0;
608  case L_ARABIC_NUMERALS:
609  return 10;
610  case L_LC_ROMAN_NUMERALS:
611  return 7;
612  case L_UC_ROMAN_NUMERALS:
613  return 7;
614  case L_LC_ALPHA:
615  return 26;
616  case L_UC_ALPHA:
617  return 26;
618  default:
619  L_ERROR("invalid charset_type %d\n", procName, type);
620  return 0;
621  }
622  return 0; /* shouldn't happen */
623 }
624 
625 
626 /*------------------------------------------------------------------------*
627  * Character/index lookup *
628  *------------------------------------------------------------------------*/
653 l_int32
655  l_int32 val,
656  char *text,
657  l_int32 *pindex)
658 {
659 l_int32 i, n, ival;
660 
661  PROCNAME("recogGetClassIndex");
662 
663  if (!pindex)
664  return ERROR_INT("&index not defined", procName, 2);
665  *pindex = -1;
666  if (!recog)
667  return ERROR_INT("recog not defined", procName, 2);
668  if (!text)
669  return ERROR_INT("text not defined", procName, 2);
670 
671  /* Search existing characters */
672  n = l_dnaGetCount(recog->dna_tochar);
673  for (i = 0; i < n; i++) {
674  l_dnaGetIValue(recog->dna_tochar, i, &ival);
675  if (val == ival) { /* found */
676  *pindex = i;
677  return 0;
678  }
679  }
680 
681  /* If not found... */
682  l_dnaAddNumber(recog->dna_tochar, val);
683  sarrayAddString(recog->sa_text, text, L_COPY);
684  recog->setsize++;
685  *pindex = n;
686  return 1;
687 }
688 
689 
698 l_ok
700  char *text,
701  l_int32 *pindex)
702 {
703 char *charstr;
704 l_int32 i, n, diff;
705 
706  PROCNAME("recogStringtoIndex");
707 
708  if (!pindex)
709  return ERROR_INT("&index not defined", procName, 1);
710  *pindex = -1;
711  if (!recog)
712  return ERROR_INT("recog not defined", procName, 1);
713  if (!text)
714  return ERROR_INT("text not defined", procName, 1);
715 
716  /* Search existing characters */
717  n = recog->setsize;
718  for (i = 0; i < n; i++) {
719  recogGetClassString(recog, i, &charstr);
720  if (!charstr) {
721  L_ERROR("string not found for index %d\n", procName, i);
722  continue;
723  }
724  diff = strcmp(text, charstr);
725  LEPT_FREE(charstr);
726  if (diff) continue;
727  *pindex = i;
728  return 0;
729  }
730 
731  return 1; /* not found */
732 }
733 
734 
751 l_int32
753  l_int32 index,
754  char **pcharstr)
755 {
756  PROCNAME("recogGetClassString");
757 
758  if (!pcharstr)
759  return ERROR_INT("&charstr not defined", procName, 1);
760  *pcharstr = stringNew("");
761  if (!recog)
762  return ERROR_INT("recog not defined", procName, 2);
763 
764  if (index < 0 || index >= recog->setsize)
765  return ERROR_INT("invalid index", procName, 1);
766  LEPT_FREE(*pcharstr);
767  *pcharstr = sarrayGetString(recog->sa_text, index, L_COPY);
768  return 0;
769 }
770 
771 
781 l_ok
782 l_convertCharstrToInt(const char *str,
783  l_int32 *pval)
784 {
785 l_int32 size, val;
786 
787  PROCNAME("l_convertCharstrToInt");
788 
789  if (!pval)
790  return ERROR_INT("&val not defined", procName, 1);
791  *pval = 0;
792  if (!str)
793  return ERROR_INT("str not defined", procName, 1);
794  size = strlen(str);
795  if (size == 0)
796  return ERROR_INT("empty string", procName, 1);
797  if (size > 4)
798  return ERROR_INT("invalid string: > 4 bytes", procName, 1);
799 
800  val = (l_int32)str[0];
801  if (size > 1)
802  val = (val << 8) + (l_int32)str[1];
803  if (size > 2)
804  val = (val << 8) + (l_int32)str[2];
805  if (size > 3)
806  val = (val << 8) + (l_int32)str[3];
807  *pval = val;
808  return 0;
809 }
810 
811 
812 /*------------------------------------------------------------------------*
813  * Serialization *
814  *------------------------------------------------------------------------*/
840 L_RECOG *
841 recogRead(const char *filename)
842 {
843 FILE *fp;
844 L_RECOG *recog;
845 
846  PROCNAME("recogRead");
847 
848  if (!filename)
849  return (L_RECOG *)ERROR_PTR("filename not defined", procName, NULL);
850  if ((fp = fopenReadStream(filename)) == NULL)
851  return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL);
852 
853  if ((recog = recogReadStream(fp)) == NULL) {
854  fclose(fp);
855  return (L_RECOG *)ERROR_PTR("recog not read", procName, NULL);
856  }
857 
858  fclose(fp);
859  return recog;
860 }
861 
862 
869 L_RECOG *
871 {
872 l_int32 version, setsize, threshold, scalew, scaleh, linew;
873 l_int32 maxyshift, nc;
874 L_DNA *dna_tochar;
875 PIXAA *paa;
876 L_RECOG *recog;
877 SARRAY *sa_text;
878 
879  PROCNAME("recogReadStream");
880 
881  if (!fp)
882  return (L_RECOG *)ERROR_PTR("stream not defined", procName, NULL);
883 
884  if (fscanf(fp, "\nRecog Version %d\n", &version) != 1)
885  return (L_RECOG *)ERROR_PTR("not a recog file", procName, NULL);
886  if (version != RECOG_VERSION_NUMBER)
887  return (L_RECOG *)ERROR_PTR("invalid recog version", procName, NULL);
888  if (fscanf(fp, "Size of character set = %d\n", &setsize) != 1)
889  return (L_RECOG *)ERROR_PTR("setsize not read", procName, NULL);
890  if (fscanf(fp, "Binarization threshold = %d\n", &threshold) != 1)
891  return (L_RECOG *)ERROR_PTR("binary thresh not read", procName, NULL);
892  if (fscanf(fp, "Maxyshift = %d\n", &maxyshift) != 1)
893  return (L_RECOG *)ERROR_PTR("maxyshift not read", procName, NULL);
894  if (fscanf(fp, "Scale to width = %d\n", &scalew) != 1)
895  return (L_RECOG *)ERROR_PTR("width not read", procName, NULL);
896  if (fscanf(fp, "Scale to height = %d\n", &scaleh) != 1)
897  return (L_RECOG *)ERROR_PTR("height not read", procName, NULL);
898  if (fscanf(fp, "Normalized line width = %d\n", &linew) != 1)
899  return (L_RECOG *)ERROR_PTR("line width not read", procName, NULL);
900  if ((recog = recogCreate(scalew, scaleh, linew, threshold,
901  maxyshift)) == NULL)
902  return (L_RECOG *)ERROR_PTR("recog not made", procName, NULL);
903 
904  if (fscanf(fp, "\nLabels for character set:\n") != 0) {
905  recogDestroy(&recog);
906  return (L_RECOG *)ERROR_PTR("label intro not read", procName, NULL);
907  }
908  l_dnaDestroy(&recog->dna_tochar);
909  if ((dna_tochar = l_dnaReadStream(fp)) == NULL) {
910  recogDestroy(&recog);
911  return (L_RECOG *)ERROR_PTR("dna_tochar not read", procName, NULL);
912  }
913  recog->dna_tochar = dna_tochar;
914  sarrayDestroy(&recog->sa_text);
915  if ((sa_text = sarrayReadStream(fp)) == NULL) {
916  recogDestroy(&recog);
917  return (L_RECOG *)ERROR_PTR("sa_text not read", procName, NULL);
918  }
919  recog->sa_text = sa_text;
920 
921  if (fscanf(fp, "\nPixaa of all samples in the training set:\n") != 0) {
922  recogDestroy(&recog);
923  return (L_RECOG *)ERROR_PTR("pixaa intro not read", procName, NULL);
924  }
925  if ((paa = pixaaReadStream(fp)) == NULL) {
926  recogDestroy(&recog);
927  return (L_RECOG *)ERROR_PTR("pixaa not read", procName, NULL);
928  }
929  recog->setsize = setsize;
930  nc = pixaaGetCount(paa, NULL);
931  if (nc != setsize) {
932  recogDestroy(&recog);
933  pixaaDestroy(&paa);
934  L_ERROR("(setsize = %d) != (paa count = %d)\n", procName,
935  setsize, nc);
936  return NULL;
937  }
938 
939  recogAddAllSamples(&recog, paa, 0); /* this finishes */
940  pixaaDestroy(&paa);
941  if (!recog)
942  return (L_RECOG *)ERROR_PTR("bad templates", procName, NULL);
943  return recog;
944 }
945 
946 
954 L_RECOG *
955 recogReadMem(const l_uint8 *data,
956  size_t size)
957 {
958 FILE *fp;
959 L_RECOG *recog;
960 
961  PROCNAME("recogReadMem");
962 
963  if (!data)
964  return (L_RECOG *)ERROR_PTR("data not defined", procName, NULL);
965  if ((fp = fopenReadFromMemory(data, size)) == NULL)
966  return (L_RECOG *)ERROR_PTR("stream not opened", procName, NULL);
967 
968  recog = recogReadStream(fp);
969  fclose(fp);
970  if (!recog) L_ERROR("recog not read\n", procName);
971  return recog;
972 }
973 
974 
991 l_ok
992 recogWrite(const char *filename,
993  L_RECOG *recog)
994 {
995 l_int32 ret;
996 FILE *fp;
997 
998  PROCNAME("recogWrite");
999 
1000  if (!filename)
1001  return ERROR_INT("filename not defined", procName, 1);
1002  if (!recog)
1003  return ERROR_INT("recog not defined", procName, 1);
1004 
1005  if ((fp = fopenWriteStream(filename, "wb")) == NULL)
1006  return ERROR_INT("stream not opened", procName, 1);
1007  ret = recogWriteStream(fp, recog);
1008  fclose(fp);
1009  if (ret)
1010  return ERROR_INT("recog not written to stream", procName, 1);
1011  return 0;
1012 }
1013 
1014 
1022 l_ok
1024  L_RECOG *recog)
1025 {
1026  PROCNAME("recogWriteStream");
1027 
1028  if (!fp)
1029  return ERROR_INT("stream not defined", procName, 1);
1030  if (!recog)
1031  return ERROR_INT("recog not defined", procName, 1);
1032 
1033  fprintf(fp, "\nRecog Version %d\n", RECOG_VERSION_NUMBER);
1034  fprintf(fp, "Size of character set = %d\n", recog->setsize);
1035  fprintf(fp, "Binarization threshold = %d\n", recog->threshold);
1036  fprintf(fp, "Maxyshift = %d\n", recog->maxyshift);
1037  fprintf(fp, "Scale to width = %d\n", recog->scalew);
1038  fprintf(fp, "Scale to height = %d\n", recog->scaleh);
1039  fprintf(fp, "Normalized line width = %d\n", recog->linew);
1040  fprintf(fp, "\nLabels for character set:\n");
1041  l_dnaWriteStream(fp, recog->dna_tochar);
1042  sarrayWriteStream(fp, recog->sa_text);
1043  fprintf(fp, "\nPixaa of all samples in the training set:\n");
1044  pixaaWriteStream(fp, recog->pixaa);
1045 
1046  return 0;
1047 }
1048 
1049 
1063 l_ok
1064 recogWriteMem(l_uint8 **pdata,
1065  size_t *psize,
1066  L_RECOG *recog)
1067 {
1068 l_int32 ret;
1069 FILE *fp;
1070 
1071  PROCNAME("recogWriteMem");
1072 
1073  if (pdata) *pdata = NULL;
1074  if (psize) *psize = 0;
1075  if (!pdata)
1076  return ERROR_INT("&data not defined", procName, 1);
1077  if (!psize)
1078  return ERROR_INT("&size not defined", procName, 1);
1079  if (!recog)
1080  return ERROR_INT("recog not defined", procName, 1);
1081 
1082 #if HAVE_FMEMOPEN
1083  if ((fp = open_memstream((char **)pdata, psize)) == NULL)
1084  return ERROR_INT("stream not opened", procName, 1);
1085  ret = recogWriteStream(fp, recog);
1086 #else
1087  L_INFO("work-around: writing to a temp file\n", procName);
1088  #ifdef _WIN32
1089  if ((fp = fopenWriteWinTempfile()) == NULL)
1090  return ERROR_INT("tmpfile stream not opened", procName, 1);
1091  #else
1092  if ((fp = tmpfile()) == NULL)
1093  return ERROR_INT("tmpfile stream not opened", procName, 1);
1094  #endif /* _WIN32 */
1095  ret = recogWriteStream(fp, recog);
1096  rewind(fp);
1097  *pdata = l_binaryReadStream(fp, psize);
1098 #endif /* HAVE_FMEMOPEN */
1099  fclose(fp);
1100  return ret;
1101 }
1102 
1103 
1117 PIXA *
1119 {
1120  PROCNAME("recogExtractPixa");
1121 
1122  if (!recog)
1123  return (PIXA *)ERROR_PTR("recog not defined", procName, NULL);
1124 
1125  recogAddCharstrLabels(recog);
1126  return pixaaFlattenToPixa(recog->pixaa_u, NULL, L_CLONE);
1127 }
1128 
1129 
1136 static l_int32
1138 {
1139 char *text;
1140 l_int32 i, j, n1, n2;
1141 PIX *pix;
1142 PIXA *pixa;
1143 PIXAA *paa;
1144 
1145  PROCNAME("recogAddCharstrLabels");
1146 
1147  if (!recog)
1148  return ERROR_INT("recog not defined", procName, 1);
1149 
1150  /* Add the labels to each unscaled pix */
1151  paa = recog->pixaa_u;
1152  n1 = pixaaGetCount(paa, NULL);
1153  for (i = 0; i < n1; i++) {
1154  pixa = pixaaGetPixa(paa, i, L_CLONE);
1155  text = sarrayGetString(recog->sa_text, i, L_NOCOPY);
1156  n2 = pixaGetCount(pixa);
1157  for (j = 0; j < n2; j++) {
1158  pix = pixaGetPix(pixa, j, L_CLONE);
1159  pixSetText(pix, text);
1160  pixDestroy(&pix);
1161  }
1162  pixaDestroy(&pixa);
1163  }
1164 
1165  return 0;
1166 }
1167 
1168 
1188 static l_int32
1190  PIXAA *paa,
1191  l_int32 debug)
1192 {
1193 char *text;
1194 l_int32 i, j, nc, ns;
1195 PIX *pix;
1196 PIXA *pixa, *pixa1;
1197 L_RECOG *recog;
1198 
1199  PROCNAME("recogAddAllSamples");
1200 
1201  if (!precog)
1202  return ERROR_INT("&recog not defined", procName, 1);
1203  if ((recog = *precog) == NULL)
1204  return ERROR_INT("recog not defined", procName, 1);
1205  if (!paa) {
1206  recogDestroy(&recog);
1207  return ERROR_INT("paa not defined", procName, 1);
1208  }
1209 
1210  nc = pixaaGetCount(paa, NULL);
1211  for (i = 0; i < nc; i++) {
1212  pixa = pixaaGetPixa(paa, i, L_CLONE);
1213  ns = pixaGetCount(pixa);
1214  text = sarrayGetString(recog->sa_text, i, L_NOCOPY);
1215  pixa1 = pixaCreate(ns);
1216  pixaaAddPixa(recog->pixaa_u, pixa1, L_INSERT);
1217  for (j = 0; j < ns; j++) {
1218  pix = pixaGetPix(pixa, j, L_CLONE);
1219  if (debug) fprintf(stderr, "pix[%d,%d]: text = %s\n", i, j, text);
1220  pixaaAddPix(recog->pixaa_u, i, pix, NULL, L_INSERT);
1221  }
1222  pixaDestroy(&pixa);
1223  }
1224 
1225  recogTrainingFinished(&recog, 0, -1, -1.0); /* For second parameter,
1226  see comment in recogRead() */
1227  if (!recog)
1228  return ERROR_INT("bad templates; recog destroyed", procName, 1);
1229  return 0;
1230 }
l_ok recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
Definition: recogdid.c:818
struct Pixaa * pixaa_u
Definition: recog.h:152
void pixaaDestroy(PIXAA **ppaa)
pixaaDestroy()
Definition: pixabasic.c:1932
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:1825
void rchaDestroy(L_RCHA **prcha)
rchaDestroy()
Definition: recogident.c:1169
L_RECOG * recogReadMem(const l_uint8 *data, size_t size)
recogReadMem()
Definition: recogbasic.c:955
Definition: pix.h:717
l_int32 threshold
Definition: recog.h:128
struct Pixa * pixa_u
Definition: recog.h:158
l_ok recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog)
recogWriteMem()
Definition: recogbasic.c:1064
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
Definition: dnabasic.c:597
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
Definition: pix3.c:2337
l_ok recogTrainLabeled(L_RECOG *recog, PIX *pixs, BOX *box, char *text, l_int32 debug)
recogTrainLabeled()
Definition: recogtrain.c:212
l_ok pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
Definition: pixabasic.c:941
struct Pta * pta
Definition: recog.h:162
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:163
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
Definition: dnabasic.c:321
Definition: recog.h:116
Definition: pix.h:716
l_ok pixaCountText(PIXA *pixa, l_int32 *pntext)
pixaCountText()
Definition: pixabasic.c:1088
l_int32 scalew
Definition: recog.h:117
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:215
struct Pixa * pixadb_split
Definition: recog.h:170
L_RECOG * recogCreateFromPixa(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixa()
Definition: recogbasic.c:281
PIXAA * pixaaReadStream(FILE *fp)
pixaaReadStream()
Definition: pixabasic.c:3018
PIXA * pixaaGetPixa(PIXAA *paa, l_int32 index, l_int32 accesstype)
pixaaGetPixa()
Definition: pixabasic.c:2168
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:752
struct Numa * nasum
Definition: recog.h:163
struct Numa * nasum_u
Definition: recog.h:160
l_int32 pixaaGetCount(PIXAA *paa, NUMA **pna)
pixaaGetCount()
Definition: pixabasic.c:2119
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
Definition: dnabasic.c:693
void rchDestroy(L_RCH **prch)
rchDestroy()
Definition: recogident.c:1243
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
Definition: pixafunc1.c:2341
struct Sarray * sa_text
Definition: recog.h:148
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:163
l_ok sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
Definition: sarray1.c:1514
Definition: array.h:83
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
Definition: utils2.c:1734
l_float32 max_wh_ratio
Definition: recog.h:144
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
Definition: dnabasic.c:439
struct Numaa * naasum
Definition: recog.h:157
l_ok recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
Definition: recogbasic.c:992
l_int32 maxarraysize
Definition: recog.h:126
struct L_Rcha * rcha
Definition: recog.h:175
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
Definition: numabasic.c:1444
Definition: array.h:116
struct Pixa * pixadb_ave
Definition: recog.h:165
l_ok recogWriteStream(FILE *fp, L_RECOG *recog)
recogWriteStream()
Definition: recogbasic.c:1023
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1483
l_ok recogTrainingFinished(L_RECOG **precog, l_int32 modifyflag, l_int32 minsize, l_float32 minfract)
recogTrainingFinished()
Definition: recogtrain.c:783
l_ok l_dnaWriteStream(FILE *fp, L_DNA *da)
l_dnaWriteStream()
Definition: dnabasic.c:1087
l_int32 setsize
Definition: recog.h:127
struct Numaa * naasum_u
Definition: recog.h:154
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
Definition: recogbasic.c:534
l_int32 * sumtab
Definition: recog.h:151
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:446
struct Pix * pixdb_ave
Definition: recog.h:167
l_int32 max_splith
Definition: recog.h:147
l_ok l_convertCharstrToInt(const char *str, l_int32 *pval)
l_convertCharstrToInt()
Definition: recogbasic.c:782
L_RECOG * recogCreate(l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreate()
Definition: recogbasic.c:408
l_int32 scaleh
Definition: recog.h:119
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
Definition: ptabasic.c:967
struct L_Bmf * bmf
Definition: recog.h:171
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
Definition: pix3.c:2297
L_DNA * l_dnaReadStream(FILE *fp)
l_dnaReadStream()
Definition: dnabasic.c:1013
struct Pta * pta_u
Definition: recog.h:159
l_int32 recogGetClassIndex(L_RECOG *recog, l_int32 val, char *text, l_int32 *pindex)
recogGetClassIndex()
Definition: recogbasic.c:654
l_float32 max_ht_ratio
Definition: recog.h:145
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
Definition: utils2.c:1780
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
struct Ptaa * ptaa
Definition: recog.h:156
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:543
L_RECOG * recogRead(const char *filename)
recogRead()
Definition: recogbasic.c:841
static l_int32 recogGetCharsetSize(l_int32 type)
recogGetCharsetSize()
Definition: recogbasic.c:601
Definition: pix.h:454
struct Pix * pixdb_range
Definition: recog.h:168
void numaDestroy(NUMA **pna)
numaDestroy()
Definition: numabasic.c:360
Definition: pix.h:465
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1700
struct Pixaa * pixaa
Definition: recog.h:155
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1657
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1262
static l_int32 recogAddCharstrLabels(L_RECOG *recog)
recogAddCharstrLabels()
Definition: recogbasic.c:1137
struct Ptaa * ptaa_u
Definition: recog.h:153
char * pixGetText(PIX *pix)
pixGetText()
Definition: pix1.c:1459
l_ok pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
Definition: pixabasic.c:1038
l_int32 bmf_size
Definition: recog.h:172
PIXA * recogExtractPixa(L_RECOG *recog)
recogExtractPixa()
Definition: recogbasic.c:1118
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:672
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
Definition: dnabasic.c:169
Definition: pix.h:718
l_ok pixaaAddPixa(PIXAA *paa, PIXA *pixa, l_int32 copyflag)
pixaaAddPixa()
Definition: pixabasic.c:1976
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:234
L_RECOG * recogCreateFromPixaNoFinish(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixaNoFinish()
Definition: recogbasic.c:327
Definition: pix.h:134
l_int32 min_splitw
Definition: recog.h:146
Definition: pix.h:719
void ptaDestroy(PTA **ppta)
ptaDestroy()
Definition: ptabasic.c:192
l_ok recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
Definition: recogbasic.c:699
l_ok pixaaWriteStream(FILE *fp, PIXAA *paa)
pixaaWriteStream()
Definition: pixabasic.c:3151
static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug)
recogAddAllSamples()
Definition: recogbasic.c:1189
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
l_ok 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:572
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
Definition: pixabasic.c:408
struct Pixa * pixa
Definition: recog.h:161
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:631
struct Pixa * pixa_id
Definition: recog.h:166
L_RECOG * recogReadStream(FILE *fp)
recogReadStream()
Definition: recogbasic.c:870
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
Definition: sarray1.c:1382
void recogDestroy(L_RECOG **precog)
recogDestroy()
Definition: recogbasic.c:478
l_ok pixaaAddPix(PIXAA *paa, l_int32 index, PIX *pix, BOX *box, l_int32 copyflag)
pixaaAddPix()
Definition: pixabasic.c:2045
l_int32 templ_use
Definition: recog.h:123
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355
struct L_Rch * rch
Definition: recog.h:174
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
Definition: bmf.c:114