190#include <config_auto.h>
194#include "allheaders.h"
196static const l_int32 MaxExamplesInClass = 256;
200static const l_int32 DefaultMinNopad = 1;
201static const l_float32 DefaultMaxWHRatio = 3.0f;
203static const l_float32 DefaultMaxHTRatio = 2.6f;
205static const l_int32 DefaultThreshold = 150;
206static const l_int32 DefaultMaxYShift = 1;
248 return (
L_RECOG *)ERROR_PTR(
"recs not defined", __func__, NULL);
292 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", __func__, NULL);
295 threshold, maxyshift);
297 return (
L_RECOG *)ERROR_PTR(
"recog not made", __func__, NULL);
299 recogTrainingFinished(&recog, 1, -1, -1.0);
301 return (
L_RECOG *)ERROR_PTR(
"bad templates", __func__, NULL);
334l_int32 full, n, i, ntext, same, maxd;
339 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", __func__, NULL);
340 pixaVerifyDepth(pixa, &same, &maxd);
342 return (
L_RECOG *)ERROR_PTR(
"not all pix are 1 bpp", __func__, NULL);
344 pixaIsFull(pixa, &full, NULL);
346 return (
L_RECOG *)ERROR_PTR(
"not all pix are present", __func__, NULL);
348 n = pixaGetCount(pixa);
349 pixaCountText(pixa, &ntext);
351 return (
L_RECOG *)ERROR_PTR(
"no pix have text strings", __func__, NULL);
353 L_ERROR(
"%d text strings < %d pix\n", __func__, ntext, n);
355 recog =
recogCreate(scalew, scaleh, linew, threshold, maxyshift);
357 return (
L_RECOG *)ERROR_PTR(
"recog not made", __func__, NULL);
358 for (i = 0; i < n; i++) {
359 pix = pixaGetPix(pixa, i,
L_CLONE);
360 text = pixGetText(pix);
361 if (!text || strlen(text) == 0) {
362 L_ERROR(
"pix[%d] has no text\n", __func__, i);
366 recogTrainLabeled(recog, pix, NULL, text, 0);
413 if (scalew < 0 || scaleh < 0)
414 return (
L_RECOG *)ERROR_PTR(
"invalid scalew or scaleh", __func__, NULL);
416 return (
L_RECOG *)ERROR_PTR(
"invalid linew > 10", __func__, NULL);
417 if (threshold == 0) threshold = DefaultThreshold;
418 if (threshold < 0 || threshold > 255) {
419 L_WARNING(
"invalid threshold; using default\n", __func__);
420 threshold = DefaultThreshold;
422 if (maxyshift < 0 || maxyshift > 2) {
423 L_WARNING(
"invalid maxyshift; using default value\n", __func__);
424 maxyshift = DefaultMaxYShift;
425 }
else if (maxyshift == 0) {
426 L_WARNING(
"Using maxyshift = 0; faster, worse correlation results\n",
428 }
else if (maxyshift == 2) {
429 L_WARNING(
"Using maxyshift = 2; slower\n", __func__);
437 recog->
linew = linew;
440 recog->
bmf = bmfCreate(NULL, 6);
445 recog->
centtab = makePixelCentroidTab8();
446 recog->
sumtab = makePixelSumTab8();
447 recog->
sa_text = sarrayCreate(0);
477 L_WARNING(
"ptr address is null\n", __func__);
481 if ((recog = *precog) == NULL)
return;
485 sarrayDestroy(&recog->
sa_text);
488 pixaDestroy(&recog->
pixa_u);
489 ptaaDestroy(&recog->
ptaa_u);
490 ptaDestroy(&recog->
pta_u);
493 pixaaDestroy(&recog->
pixaa);
494 pixaDestroy(&recog->
pixa);
495 ptaaDestroy(&recog->
ptaa);
496 ptaDestroy(&recog->
pta);
497 numaDestroy(&recog->
nasum);
498 numaaDestroy(&recog->
naasum);
506 bmfDestroy(&recog->
bmf);
507 rchDestroy(&recog->
rch);
508 rchaDestroy(&recog->
rcha);
509 recogDestroyDid(recog);
528 return ERROR_INT(
"recog not defined", __func__, 0);
564 l_float32 max_wh_ratio,
565 l_float32 max_ht_ratio)
568 return ERROR_INT(
"recog not defined", __func__, 1);
570 recog->
charset_type = (type >= 0) ? type : DefaultCharsetType;
572 recog->
min_nopad = (min_nopad >= 0) ? min_nopad : DefaultMinNopad;
573 recog->
max_wh_ratio = (max_wh_ratio > 0.0) ? max_wh_ratio :
575 recog->
max_ht_ratio = (max_ht_ratio > 1.0) ? max_ht_ratio :
604 L_ERROR(
"invalid charset_type %d\n", __func__, type);
647 return ERROR_INT(
"&index not defined", __func__, 2);
650 return ERROR_INT(
"recog not defined", __func__, 2);
652 return ERROR_INT(
"text not defined", __func__, 2);
656 for (i = 0; i < n; i++) {
690 return ERROR_INT(
"&index not defined", __func__, 1);
693 return ERROR_INT(
"recog not defined", __func__, 1);
695 return ERROR_INT(
"text not defined", __func__, 1);
699 for (i = 0; i < n; i++) {
702 L_ERROR(
"string not found for index %d\n", __func__, i);
705 diff = strcmp(text, charstr);
738 return ERROR_INT(
"&charstr not defined", __func__, 1);
739 *pcharstr = stringNew(
"");
741 return ERROR_INT(
"recog not defined", __func__, 2);
743 if (index < 0 || index >= recog->
setsize)
744 return ERROR_INT(
"invalid index", __func__, 1);
745 LEPT_FREE(*pcharstr);
768 return ERROR_INT(
"&val not defined", __func__, 1);
771 return ERROR_INT(
"str not defined", __func__, 1);
774 return ERROR_INT(
"empty string", __func__, 1);
776 return ERROR_INT(
"invalid string: > 4 bytes", __func__, 1);
778 val = (l_uint8)str[0];
780 val = (val << 8) + (l_uint8)str[1];
782 val = (val << 8) + (l_uint8)str[2];
784 val = (val << 8) + (l_uint8)str[3];
785 *pval = (l_int32)(val & 0x7fffffff);
825 return (
L_RECOG *)ERROR_PTR(
"filename not defined", __func__, NULL);
826 if ((fp = fopenReadStream(filename)) == NULL)
827 return (
L_RECOG *)ERROR_PTR_1(
"stream not opened",
828 filename, __func__, NULL);
832 return (
L_RECOG *)ERROR_PTR_1(
"recog not read",
833 filename, __func__, NULL);
850l_int32 version, setsize, threshold, scalew, scaleh, linew;
851l_int32 maxyshift, nc;
858 return (
L_RECOG *)ERROR_PTR(
"stream not defined", __func__, NULL);
860 if (fscanf(fp,
"\nRecog Version %d\n", &version) != 1)
861 return (
L_RECOG *)ERROR_PTR(
"not a recog file", __func__, NULL);
862 if (version != RECOG_VERSION_NUMBER)
863 return (
L_RECOG *)ERROR_PTR(
"invalid recog version", __func__, NULL);
864 if (fscanf(fp,
"Size of character set = %d\n", &setsize) != 1)
865 return (
L_RECOG *)ERROR_PTR(
"setsize not read", __func__, NULL);
866 if (fscanf(fp,
"Binarization threshold = %d\n", &threshold) != 1)
867 return (
L_RECOG *)ERROR_PTR(
"binary thresh not read", __func__, NULL);
868 if (fscanf(fp,
"Maxyshift = %d\n", &maxyshift) != 1)
869 return (
L_RECOG *)ERROR_PTR(
"maxyshift not read", __func__, NULL);
870 if (fscanf(fp,
"Scale to width = %d\n", &scalew) != 1)
871 return (
L_RECOG *)ERROR_PTR(
"width not read", __func__, NULL);
872 if (fscanf(fp,
"Scale to height = %d\n", &scaleh) != 1)
873 return (
L_RECOG *)ERROR_PTR(
"height not read", __func__, NULL);
874 if (fscanf(fp,
"Normalized line width = %d\n", &linew) != 1)
875 return (
L_RECOG *)ERROR_PTR(
"line width not read", __func__, NULL);
876 if ((recog =
recogCreate(scalew, scaleh, linew, threshold,
878 return (
L_RECOG *)ERROR_PTR(
"recog not made", __func__, NULL);
880 if (fscanf(fp,
"\nLabels for character set:\n") == -1) {
882 return (
L_RECOG *)ERROR_PTR(
"label intro not read", __func__, NULL);
885 if ((dna_tochar = l_dnaReadStream(fp)) == NULL) {
887 return (
L_RECOG *)ERROR_PTR(
"dna_tochar not read", __func__, NULL);
890 sarrayDestroy(&recog->
sa_text);
891 if ((sa_text = sarrayReadStream(fp)) == NULL) {
893 return (
L_RECOG *)ERROR_PTR(
"sa_text not read", __func__, NULL);
897 if (fscanf(fp,
"\nPixaa of all samples in the training set:\n") == -1) {
899 return (
L_RECOG *)ERROR_PTR(
"pixaa intro not read", __func__, NULL);
901 if ((paa = pixaaReadStream(fp)) == NULL) {
903 return (
L_RECOG *)ERROR_PTR(
"pixaa not read", __func__, NULL);
906 nc = pixaaGetCount(paa, NULL);
910 L_ERROR(
"(setsize = %d) != (paa count = %d)\n", __func__,
918 return (
L_RECOG *)ERROR_PTR(
"bad templates", __func__, NULL);
938 return (
L_RECOG *)ERROR_PTR(
"data not defined", __func__, NULL);
939 if ((fp = fopenReadFromMemory(data, size)) == NULL)
940 return (
L_RECOG *)ERROR_PTR(
"stream not opened", __func__, NULL);
944 if (!recog) L_ERROR(
"recog not read\n", __func__);
973 return ERROR_INT(
"filename not defined", __func__, 1);
975 return ERROR_INT(
"recog not defined", __func__, 1);
977 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
978 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
982 return ERROR_INT_1(
"recog not written to stream",
983 filename, __func__, 1);
1000 return ERROR_INT(
"stream not defined", __func__, 1);
1002 return ERROR_INT(
"recog not defined", __func__, 1);
1004 fprintf(fp,
"\nRecog Version %d\n", RECOG_VERSION_NUMBER);
1005 fprintf(fp,
"Size of character set = %d\n", recog->
setsize);
1006 fprintf(fp,
"Binarization threshold = %d\n", recog->
threshold);
1007 fprintf(fp,
"Maxyshift = %d\n", recog->
maxyshift);
1008 fprintf(fp,
"Scale to width = %d\n", recog->
scalew);
1009 fprintf(fp,
"Scale to height = %d\n", recog->
scaleh);
1010 fprintf(fp,
"Normalized line width = %d\n", recog->
linew);
1011 fprintf(fp,
"\nLabels for character set:\n");
1013 sarrayWriteStream(fp, recog->
sa_text);
1014 fprintf(fp,
"\nPixaa of all samples in the training set:\n");
1015 pixaaWriteStream(fp, recog->
pixaa);
1042 if (pdata) *pdata = NULL;
1043 if (psize) *psize = 0;
1045 return ERROR_INT(
"&data not defined", __func__, 1);
1047 return ERROR_INT(
"&size not defined", __func__, 1);
1049 return ERROR_INT(
"recog not defined", __func__, 1);
1052 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1053 return ERROR_INT(
"stream not opened", __func__, 1);
1057 if (*psize > 0) *psize = *psize - 1;
1059 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1061 if ((fp = fopenWriteWinTempfile()) == NULL)
1062 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1064 if ((fp = tmpfile()) == NULL)
1065 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1069 *pdata = l_binaryReadStream(fp, psize);
1093 return (
PIXA *)ERROR_PTR(
"recog not defined", __func__, NULL);
1110l_int32 i, j, n1, n2;
1116 return ERROR_INT(
"recog not defined", __func__, 1);
1120 n1 = pixaaGetCount(paa, NULL);
1121 for (i = 0; i < n1; i++) {
1122 pixa = pixaaGetPixa(paa, i,
L_CLONE);
1124 n2 = pixaGetCount(pixa);
1125 for (j = 0; j < n2; j++) {
1126 pix = pixaGetPix(pixa, j,
L_CLONE);
1127 pixSetText(pix, text);
1162l_int32 i, j, nc, ns;
1168 return ERROR_INT(
"&recog not defined", __func__, 1);
1169 if ((recog = *precog) == NULL)
1170 return ERROR_INT(
"recog not defined", __func__, 1);
1174 return ERROR_INT(
"paa not defined", __func__, 1);
1177 nc = pixaaGetCount(paa, NULL);
1178 for (i = 0; i < nc; i++) {
1179 pixa = pixaaGetPixa(paa, i,
L_CLONE);
1180 ns = pixaGetCount(pixa);
1182 pixa1 = pixaCreate(ns);
1184 for (j = 0; j < ns; j++) {
1185 pix = pixaGetPix(pixa, j,
L_CLONE);
1186 if (debug) lept_stderr(
"pix[%d,%d]: text = %s\n", i, j, text);
1192 recogTrainingFinished(&recog, 0, -1, -1.0);
1195 return ERROR_INT(
"bad templates; recog destroyed", __func__, 1);
l_ok recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
l_ok recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
L_RECOG * recogCreateFromPixa(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixa()
static l_int32 recogGetCharsetSize(l_int32 type)
recogGetCharsetSize()
l_ok recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog)
recogWriteMem()
l_ok recogWriteStream(FILE *fp, L_RECOG *recog)
recogWriteStream()
l_ok recogSetParams(L_RECOG *recog, l_int32 type, l_int32 min_nopad, l_float32 max_wh_ratio, l_float32 max_ht_ratio)
recogSetParams()
static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug)
recogAddAllSamples()
L_RECOG * recogRead(const char *filename)
recogRead()
l_ok l_convertCharstrToInt(const char *str, l_int32 *pval)
l_convertCharstrToInt()
L_RECOG * recogCreate(l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreate()
l_int32 recogGetClassIndex(L_RECOG *recog, l_int32 val, char *text, l_int32 *pindex)
recogGetClassIndex()
static l_int32 recogAddCharstrLabels(L_RECOG *recog)
recogAddCharstrLabels()
L_RECOG * recogCreateFromPixaNoFinish(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixaNoFinish()
L_RECOG * recogCreateFromRecog(L_RECOG *recs, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromRecog()
l_int32 recogGetClassString(L_RECOG *recog, l_int32 index, char **pcharstr)
recogGetClassString()
PIXA * recogExtractPixa(L_RECOG *recog)
recogExtractPixa()
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
void recogDestroy(L_RECOG **precog)
recogDestroy()
L_RECOG * recogReadMem(const l_uint8 *data, size_t size)
recogReadMem()
L_RECOG * recogReadStream(FILE *fp)
recogReadStream()
struct L_Dna * dna_tochar
struct Pixa * pixadb_split
struct Pixa * pixadb_boot