188 #include "allheaders.h" 190 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20;
191 static const l_int32 MAX_EXAMPLES_IN_CLASS = 256;
195 static const l_int32 DEFAULT_MIN_NOPAD = 1;
196 static const l_float32 DEFAULT_MAX_WH_RATIO = 3.0;
198 static const l_float32 DEFAULT_MAX_HT_RATIO = 2.6;
200 static const l_int32 DEFAULT_THRESHOLD = 150;
201 static const l_int32 DEFAULT_MAXYSHIFT = 1;
242 PROCNAME(
"recogCreateFromRecog");
245 return (
L_RECOG *)ERROR_PTR(
"recs not defined", procName, NULL);
288 PROCNAME(
"recogCreateFromPixa");
291 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
294 threshold, maxyshift);
296 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
300 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
333 l_int32 full, n, i, ntext, same, maxd;
337 PROCNAME(
"recogCreateFromPixaNoFinish");
340 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
343 return (
L_RECOG *)ERROR_PTR(
"not all pix are 1 bpp", procName, NULL);
347 return (
L_RECOG *)ERROR_PTR(
"not all pix are present", procName, NULL);
352 return (
L_RECOG *)ERROR_PTR(
"no pix have text strings", procName, NULL);
354 L_ERROR(
"%d text strings < %d pix\n", procName, ntext, n);
356 recog =
recogCreate(scalew, scaleh, linew, threshold, maxyshift);
358 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
359 for (i = 0; i < n; i++) {
362 if (!text || strlen(text) == 0) {
363 L_ERROR(
"pix[%d] has no text\n", procName, i);
414 PROCNAME(
"recogCreate");
416 if (scalew < 0 || scaleh < 0)
417 return (
L_RECOG *)ERROR_PTR(
"invalid scalew or scaleh", procName, NULL);
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;
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",
431 }
else if (maxyshift == 2) {
432 L_WARNING(
"Using maxyshift = 2; slower\n", procName);
436 return (
L_RECOG *)ERROR_PTR(
"rec not made", procName, NULL);
441 recog->
linew = linew;
480 PROCNAME(
"recogDestroy");
483 L_WARNING(
"ptr address is null\n", procName);
487 if ((recog = *precog) == NULL)
return;
531 PROCNAME(
"recogGetCount");
534 return ERROR_INT(
"recog not defined", procName, 0);
570 l_float32 max_wh_ratio,
571 l_float32 max_ht_ratio)
574 PROCNAME(
"recogSetParams");
577 return ERROR_INT(
"recog not defined", procName, 1);
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;
599 PROCNAME(
"recogGetCharsetSize");
615 L_ERROR(
"invalid charset_type %d\n", procName, type);
657 PROCNAME(
"recogGetClassIndex");
660 return ERROR_INT(
"&index not defined", procName, 2);
663 return ERROR_INT(
"recog not defined", procName, 2);
665 return ERROR_INT(
"text not defined", procName, 2);
669 for (i = 0; i < n; i++) {
702 PROCNAME(
"recogStringtoIndex");
705 return ERROR_INT(
"&index not defined", procName, 1);
708 return ERROR_INT(
"recog not defined", procName, 1);
710 return ERROR_INT(
"text not defined", procName, 1);
714 for (i = 0; i < n; i++) {
717 L_ERROR(
"string not found for index %d\n", procName, i);
720 diff = strcmp(text, charstr);
752 PROCNAME(
"recogGetClassString");
755 return ERROR_INT(
"&charstr not defined", procName, 1);
758 return ERROR_INT(
"recog not defined", procName, 2);
760 if (index < 0 || index >= recog->
setsize)
761 return ERROR_INT(
"invalid index", procName, 1);
762 LEPT_FREE(*pcharstr);
783 PROCNAME(
"l_convertCharstrToInt");
786 return ERROR_INT(
"&val not defined", procName, 1);
789 return ERROR_INT(
"str not defined", procName, 1);
792 return ERROR_INT(
"empty string", procName, 1);
794 return ERROR_INT(
"invalid string: > 4 bytes", procName, 1);
796 val = (l_int32)str[0];
798 val = (val << 8) + (l_int32)str[1];
800 val = (val << 8) + (l_int32)str[2];
802 val = (val << 8) + (l_int32)str[3];
842 PROCNAME(
"recogRead");
845 return (
L_RECOG *)ERROR_PTR(
"filename not defined", procName, NULL);
847 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
851 return (
L_RECOG *)ERROR_PTR(
"recog not read", procName, NULL);
868 l_int32 version, setsize, threshold, scalew, scaleh, linew;
869 l_int32 maxyshift, nc;
875 PROCNAME(
"recogReadStream");
878 return (
L_RECOG *)ERROR_PTR(
"stream not defined", procName, NULL);
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,
898 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
900 if (fscanf(fp,
"\nLabels for character set:\n") != 0) {
902 return (
L_RECOG *)ERROR_PTR(
"label intro not read", procName, NULL);
907 return (
L_RECOG *)ERROR_PTR(
"dna_tochar not read", procName, NULL);
913 return (
L_RECOG *)ERROR_PTR(
"sa_text not read", procName, NULL);
917 if (fscanf(fp,
"\nPixaa of all samples in the training set:\n") != 0) {
919 return (
L_RECOG *)ERROR_PTR(
"pixaa intro not read", procName, NULL);
923 return (
L_RECOG *)ERROR_PTR(
"pixaa not read", procName, NULL);
930 L_ERROR(
"(setsize = %d) != (paa count = %d)\n", procName,
938 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
957 PROCNAME(
"recogReadMem");
960 return (
L_RECOG *)ERROR_PTR(
"data not defined", procName, NULL);
962 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
966 if (!recog) L_ERROR(
"recog not read\n", procName);
994 PROCNAME(
"recogWrite");
997 return ERROR_INT(
"filename not defined", procName, 1);
999 return ERROR_INT(
"recog not defined", procName, 1);
1002 return ERROR_INT(
"stream not opened", procName, 1);
1006 return ERROR_INT(
"recog not written to stream", procName, 1);
1022 PROCNAME(
"recogWriteStream");
1025 return ERROR_INT(
"stream not defined", procName, 1);
1027 return ERROR_INT(
"recog not defined", procName, 1);
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");
1039 fprintf(fp,
"\nPixaa of all samples in the training set:\n");
1067 PROCNAME(
"recogWriteMem");
1069 if (pdata) *pdata = NULL;
1070 if (psize) *psize = 0;
1072 return ERROR_INT(
"&data not defined", procName, 1);
1074 return ERROR_INT(
"&size not defined", procName, 1);
1076 return ERROR_INT(
"recog not defined", procName, 1);
1079 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1080 return ERROR_INT(
"stream not opened", procName, 1);
1083 L_INFO(
"work-around: writing to a temp file\n", procName);
1086 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1088 if ((fp = tmpfile()) == NULL)
1089 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1116 PROCNAME(
"recogExtractPixa");
1119 return (
PIXA *)ERROR_PTR(
"recog not defined", procName, NULL);
1136 l_int32 i, j, n1, n2;
1141 PROCNAME(
"recogAddCharstrLabels");
1144 return ERROR_INT(
"recog not defined", procName, 1);
1149 for (i = 0; i < n1; i++) {
1153 for (j = 0; j < n2; j++) {
1190 l_int32 i, j, nc, ns;
1195 PROCNAME(
"recogAddAllSamples");
1198 return ERROR_INT(
"&recog not defined", procName, 1);
1199 if ((recog = *precog) == NULL)
1200 return ERROR_INT(
"recog not defined", procName, 1);
1203 return ERROR_INT(
"paa not defined", procName, 1);
1207 for (i = 0; i < nc; i++) {
1213 for (j = 0; j < ns; j++) {
1215 if (debug) fprintf(stderr,
"pix[%d,%d]: text = %s\n", i, j, text);
1224 return ERROR_INT(
"bad templates; recog destroyed", procName, 1);
l_int32 recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog)
recogWriteMem()
void pixaaDestroy(PIXAA **ppaa)
pixaaDestroy()
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
struct Pixa * pixadb_boot
PIXAA * pixaaCreate(l_int32 n)
pixaaCreate()
void rchaDestroy(L_RCHA **prcha)
rchaDestroy()
L_RECOG * recogReadMem(const l_uint8 *data, size_t size)
recogReadMem()
l_int32 pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
l_int32 recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
l_int32 pixaaAddPixa(PIXAA *paa, PIXA *pixa, l_int32 copyflag)
pixaaAddPixa()
l_int32 l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
l_int32 recogWriteStream(FILE *fp, L_RECOG *recog)
recogWriteStream()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
l_int32 l_dnaWriteStream(FILE *fp, L_DNA *da)
l_dnaWriteStream()
char * stringNew(const char *src)
stringNew()
struct Pixa * pixadb_split
l_int32 pixaaAddPix(PIXAA *paa, l_int32 index, PIX *pix, BOX *box, l_int32 copyflag)
pixaaAddPix()
L_RECOG * recogCreateFromPixa(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixa()
PIXAA * pixaaReadStream(FILE *fp)
pixaaReadStream()
PIXA * pixaaGetPixa(PIXAA *paa, l_int32 index, l_int32 accesstype)
pixaaGetPixa()
l_int32 recogGetClassString(L_RECOG *recog, l_int32 index, char **pcharstr)
recogGetClassString()
l_int32 recogSetParams(L_RECOG *recog, l_int32 type, l_int32 min_nopad, l_float32 max_wh_ratio, l_float32 max_ht_ratio)
recogSetParams()
l_int32 pixaaGetCount(PIXAA *paa, NUMA **pna)
pixaaGetCount()
void rchDestroy(L_RCH **prch)
rchDestroy()
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
l_int32 sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
l_int32 l_convertCharstrToInt(const char *str, l_int32 *pval)
l_convertCharstrToInt()
l_int32 pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
l_int32 l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
static const l_int32 L_INSERT
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
l_int32 pixaCountText(PIXA *pixa, l_int32 *pntext)
pixaCountText()
L_RECOG * recogCreate(l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreate()
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
L_DNA * l_dnaReadStream(FILE *fp)
l_dnaReadStream()
l_int32 recogTrainLabeled(L_RECOG *recog, PIX *pixs, BOX *box, char *text, l_int32 debug)
recogTrainLabeled()
l_int32 recogGetClassIndex(L_RECOG *recog, l_int32 val, char *text, l_int32 *pindex)
recogGetClassIndex()
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_int32 sarrayAddString(SARRAY *sa, char *string, l_int32 copyflag)
sarrayAddString()
struct L_Dna * dna_tochar
void pixDestroy(PIX **ppix)
pixDestroy()
L_RECOG * recogRead(const char *filename)
recogRead()
static l_int32 recogGetCharsetSize(l_int32 type)
recogGetCharsetSize()
void numaDestroy(NUMA **pna)
numaDestroy()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
static l_int32 recogAddCharstrLabels(L_RECOG *recog)
recogAddCharstrLabels()
l_int32 recogTrainingFinished(L_RECOG **precog, l_int32 modifyflag, l_int32 minsize, l_float32 minfract)
recogTrainingFinished()
char * pixGetText(PIX *pix)
pixGetText()
l_int32 recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
PIXA * recogExtractPixa(L_RECOG *recog)
recogExtractPixa()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
L_RECOG * recogCreateFromRecog(L_RECOG *recs, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromRecog()
L_RECOG * recogCreateFromPixaNoFinish(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixaNoFinish()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_int32 recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug)
recogAddAllSamples()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
L_RECOG * recogReadStream(FILE *fp)
recogReadStream()
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
void recogDestroy(L_RECOG **precog)
recogDestroy()
l_int32 pixSetText(PIX *pix, const char *textstring)
pixSetText()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
l_int32 pixaaWriteStream(FILE *fp, PIXAA *paa)
pixaaWriteStream()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()