190 #include <config_auto.h>
194 #include "allheaders.h"
196 static const l_int32 MaxExamplesInClass = 256;
200 static const l_int32 DefaultMinNopad = 1;
201 static const l_float32 DefaultMaxWHRatio = 3.0;
203 static const l_float32 DefaultMaxHTRatio = 2.6;
205 static const l_int32 DefaultThreshold = 150;
206 static const l_int32 DefaultMaxYShift = 1;
247 PROCNAME(
"recogCreateFromRecog");
250 return (
L_RECOG *)ERROR_PTR(
"recs not defined", procName, NULL);
293 PROCNAME(
"recogCreateFromPixa");
296 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
299 threshold, maxyshift);
301 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
305 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
338 l_int32 full, n, i, ntext, same, maxd;
342 PROCNAME(
"recogCreateFromPixaNoFinish");
345 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
348 return (
L_RECOG *)ERROR_PTR(
"not all pix are 1 bpp", procName, NULL);
352 return (
L_RECOG *)ERROR_PTR(
"not all pix are present", procName, NULL);
357 return (
L_RECOG *)ERROR_PTR(
"no pix have text strings", procName, NULL);
359 L_ERROR(
"%d text strings < %d pix\n", procName, ntext, n);
361 recog =
recogCreate(scalew, scaleh, linew, threshold, maxyshift);
363 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
364 for (i = 0; i < n; i++) {
367 if (!text || strlen(text) == 0) {
368 L_ERROR(
"pix[%d] has no text\n", procName, i);
419 PROCNAME(
"recogCreate");
421 if (scalew < 0 || scaleh < 0)
422 return (
L_RECOG *)ERROR_PTR(
"invalid scalew or scaleh", procName, NULL);
424 return (
L_RECOG *)ERROR_PTR(
"invalid linew > 10", procName, NULL);
425 if (threshold == 0) threshold = DefaultThreshold;
426 if (threshold < 0 || threshold > 255) {
427 L_WARNING(
"invalid threshold; using default\n", procName);
428 threshold = DefaultThreshold;
430 if (maxyshift < 0 || maxyshift > 2) {
431 L_WARNING(
"invalid maxyshift; using default value\n", procName);
432 maxyshift = DefaultMaxYShift;
433 }
else if (maxyshift == 0) {
434 L_WARNING(
"Using maxyshift = 0; faster, worse correlation results\n",
436 }
else if (maxyshift == 2) {
437 L_WARNING(
"Using maxyshift = 2; slower\n", procName);
445 recog->
linew = linew;
484 PROCNAME(
"recogDestroy");
487 L_WARNING(
"ptr address is null\n", procName);
491 if ((recog = *precog) == NULL)
return;
537 PROCNAME(
"recogGetCount");
540 return ERROR_INT(
"recog not defined", procName, 0);
576 l_float32 max_wh_ratio,
577 l_float32 max_ht_ratio)
579 PROCNAME(
"recogSetParams");
582 return ERROR_INT(
"recog not defined", procName, 1);
584 recog->
charset_type = (type >= 0) ? type : DefaultCharsetType;
586 recog->
min_nopad = (min_nopad >= 0) ? min_nopad : DefaultMinNopad;
587 recog->
max_wh_ratio = (max_wh_ratio > 0.0) ? max_wh_ratio :
589 recog->
max_ht_ratio = (max_ht_ratio > 1.0) ? max_ht_ratio :
604 PROCNAME(
"recogGetCharsetSize");
620 L_ERROR(
"invalid charset_type %d\n", procName, type);
662 PROCNAME(
"recogGetClassIndex");
665 return ERROR_INT(
"&index not defined", procName, 2);
668 return ERROR_INT(
"recog not defined", procName, 2);
670 return ERROR_INT(
"text not defined", procName, 2);
674 for (i = 0; i < n; i++) {
707 PROCNAME(
"recogStringtoIndex");
710 return ERROR_INT(
"&index not defined", procName, 1);
713 return ERROR_INT(
"recog not defined", procName, 1);
715 return ERROR_INT(
"text not defined", procName, 1);
719 for (i = 0; i < n; i++) {
722 L_ERROR(
"string not found for index %d\n", procName, i);
725 diff = strcmp(text, charstr);
757 PROCNAME(
"recogGetClassString");
760 return ERROR_INT(
"&charstr not defined", procName, 1);
763 return ERROR_INT(
"recog not defined", procName, 2);
765 if (index < 0 || index >= recog->
setsize)
766 return ERROR_INT(
"invalid index", procName, 1);
767 LEPT_FREE(*pcharstr);
788 PROCNAME(
"l_convertCharstrToInt");
791 return ERROR_INT(
"&val not defined", procName, 1);
794 return ERROR_INT(
"str not defined", procName, 1);
797 return ERROR_INT(
"empty string", procName, 1);
799 return ERROR_INT(
"invalid string: > 4 bytes", procName, 1);
801 val = (l_int32)str[0];
803 val = (val << 8) + (l_int32)str[1];
805 val = (val << 8) + (l_int32)str[2];
807 val = (val << 8) + (l_int32)str[3];
847 PROCNAME(
"recogRead");
850 return (
L_RECOG *)ERROR_PTR(
"filename not defined", procName, NULL);
852 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
856 return (
L_RECOG *)ERROR_PTR(
"recog not read", procName, NULL);
873 l_int32 version, setsize, threshold, scalew, scaleh, linew;
874 l_int32 maxyshift, nc;
880 PROCNAME(
"recogReadStream");
883 return (
L_RECOG *)ERROR_PTR(
"stream not defined", procName, NULL);
885 if (fscanf(fp,
"\nRecog Version %d\n", &version) != 1)
886 return (
L_RECOG *)ERROR_PTR(
"not a recog file", procName, NULL);
887 if (version != RECOG_VERSION_NUMBER)
888 return (
L_RECOG *)ERROR_PTR(
"invalid recog version", procName, NULL);
889 if (fscanf(fp,
"Size of character set = %d\n", &setsize) != 1)
890 return (
L_RECOG *)ERROR_PTR(
"setsize not read", procName, NULL);
891 if (fscanf(fp,
"Binarization threshold = %d\n", &threshold) != 1)
892 return (
L_RECOG *)ERROR_PTR(
"binary thresh not read", procName, NULL);
893 if (fscanf(fp,
"Maxyshift = %d\n", &maxyshift) != 1)
894 return (
L_RECOG *)ERROR_PTR(
"maxyshift not read", procName, NULL);
895 if (fscanf(fp,
"Scale to width = %d\n", &scalew) != 1)
896 return (
L_RECOG *)ERROR_PTR(
"width not read", procName, NULL);
897 if (fscanf(fp,
"Scale to height = %d\n", &scaleh) != 1)
898 return (
L_RECOG *)ERROR_PTR(
"height not read", procName, NULL);
899 if (fscanf(fp,
"Normalized line width = %d\n", &linew) != 1)
900 return (
L_RECOG *)ERROR_PTR(
"line width not read", procName, NULL);
901 if ((recog =
recogCreate(scalew, scaleh, linew, threshold,
903 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
905 if (fscanf(fp,
"\nLabels for character set:\n") != 0) {
907 return (
L_RECOG *)ERROR_PTR(
"label intro not read", procName, NULL);
912 return (
L_RECOG *)ERROR_PTR(
"dna_tochar not read", procName, NULL);
918 return (
L_RECOG *)ERROR_PTR(
"sa_text not read", procName, NULL);
922 if (fscanf(fp,
"\nPixaa of all samples in the training set:\n") != 0) {
924 return (
L_RECOG *)ERROR_PTR(
"pixaa intro not read", procName, NULL);
928 return (
L_RECOG *)ERROR_PTR(
"pixaa not read", procName, NULL);
935 L_ERROR(
"(setsize = %d) != (paa count = %d)\n", procName,
943 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
962 PROCNAME(
"recogReadMem");
965 return (
L_RECOG *)ERROR_PTR(
"data not defined", procName, NULL);
967 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
971 if (!recog) L_ERROR(
"recog not read\n", procName);
999 PROCNAME(
"recogWrite");
1002 return ERROR_INT(
"filename not defined", procName, 1);
1004 return ERROR_INT(
"recog not defined", procName, 1);
1007 return ERROR_INT(
"stream not opened", procName, 1);
1011 return ERROR_INT(
"recog not written to stream", procName, 1);
1027 PROCNAME(
"recogWriteStream");
1030 return ERROR_INT(
"stream not defined", procName, 1);
1032 return ERROR_INT(
"recog not defined", procName, 1);
1034 fprintf(fp,
"\nRecog Version %d\n", RECOG_VERSION_NUMBER);
1035 fprintf(fp,
"Size of character set = %d\n", recog->
setsize);
1036 fprintf(fp,
"Binarization threshold = %d\n", recog->
threshold);
1037 fprintf(fp,
"Maxyshift = %d\n", recog->
maxyshift);
1038 fprintf(fp,
"Scale to width = %d\n", recog->
scalew);
1039 fprintf(fp,
"Scale to height = %d\n", recog->
scaleh);
1040 fprintf(fp,
"Normalized line width = %d\n", recog->
linew);
1041 fprintf(fp,
"\nLabels for character set:\n");
1044 fprintf(fp,
"\nPixaa of all samples in the training set:\n");
1072 PROCNAME(
"recogWriteMem");
1074 if (pdata) *pdata = NULL;
1075 if (psize) *psize = 0;
1077 return ERROR_INT(
"&data not defined", procName, 1);
1079 return ERROR_INT(
"&size not defined", procName, 1);
1081 return ERROR_INT(
"recog not defined", procName, 1);
1084 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1085 return ERROR_INT(
"stream not opened", procName, 1);
1089 *psize = *psize - 1;
1091 L_INFO(
"work-around: writing to a temp file\n", procName);
1094 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1096 if ((fp = tmpfile()) == NULL)
1097 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1124 PROCNAME(
"recogExtractPixa");
1127 return (
PIXA *)ERROR_PTR(
"recog not defined", procName, NULL);
1144 l_int32 i, j, n1, n2;
1149 PROCNAME(
"recogAddCharstrLabels");
1152 return ERROR_INT(
"recog not defined", procName, 1);
1157 for (i = 0; i < n1; i++) {
1161 for (j = 0; j < n2; j++) {
1198 l_int32 i, j, nc, ns;
1203 PROCNAME(
"recogAddAllSamples");
1206 return ERROR_INT(
"&recog not defined", procName, 1);
1207 if ((recog = *precog) == NULL)
1208 return ERROR_INT(
"recog not defined", procName, 1);
1211 return ERROR_INT(
"paa not defined", procName, 1);
1215 for (i = 0; i < nc; i++) {
1221 for (j = 0; j < ns; j++) {
1223 if (debug)
lept_stderr(
"pix[%d,%d]: text = %s\n", i, j, text);
1232 return ERROR_INT(
"bad templates; recog destroyed", procName, 1);
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
L_DNA * l_dnaReadStream(FILE *fp)
l_dnaReadStream()
l_ok l_dnaWriteStream(FILE *fp, L_DNA *da)
l_dnaWriteStream()
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
void numaDestroy(NUMA **pna)
numaDestroy()
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
void pixDestroy(PIX **ppix)
pixDestroy()
char * pixGetText(PIX *pix)
pixGetText()
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
PIXAA * pixaaCreate(l_int32 n)
pixaaCreate()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
l_ok pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
l_ok pixaCountText(PIXA *pixa, l_int32 *pntext)
pixaCountText()
PIXAA * pixaaReadStream(FILE *fp)
pixaaReadStream()
l_ok pixaaAddPix(PIXAA *paa, l_int32 index, PIX *pix, BOX *box, l_int32 copyflag)
pixaaAddPix()
l_ok pixaaAddPixa(PIXAA *paa, PIXA *pixa, l_int32 copyflag)
pixaaAddPixa()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
l_ok pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
l_ok pixaaWriteStream(FILE *fp, PIXAA *paa)
pixaaWriteStream()
void pixaaDestroy(PIXAA **ppaa)
pixaaDestroy()
l_int32 pixaaGetCount(PIXAA *paa, NUMA **pna)
pixaaGetCount()
PIXA * pixaaGetPixa(PIXAA *paa, l_int32 index, l_int32 accesstype)
pixaaGetPixa()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
l_ok recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
L_RECOG * recogCreateFromRecog(L_RECOG *recs, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromRecog()
static l_int32 recogGetCharsetSize(l_int32 type)
recogGetCharsetSize()
L_RECOG * recogRead(const char *filename)
recogRead()
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_ok l_convertCharstrToInt(const char *str, l_int32 *pval)
l_convertCharstrToInt()
PIXA * recogExtractPixa(L_RECOG *recog)
recogExtractPixa()
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 * recogCreate(l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreate()
L_RECOG * recogCreateFromPixa(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixa()
L_RECOG * recogReadMem(const l_uint8 *data, size_t size)
recogReadMem()
l_int32 recogGetClassString(L_RECOG *recog, l_int32 index, char **pcharstr)
recogGetClassString()
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
L_RECOG * recogReadStream(FILE *fp)
recogReadStream()
void recogDestroy(L_RECOG **precog)
recogDestroy()
L_RECOG * recogCreateFromPixaNoFinish(PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 linew, l_int32 threshold, l_int32 maxyshift)
recogCreateFromPixaNoFinish()
l_ok recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
void rchaDestroy(L_RCHA **prcha)
rchaDestroy()
void rchDestroy(L_RCH **prch)
rchDestroy()
l_ok recogTrainingFinished(L_RECOG **precog, l_int32 modifyflag, l_int32 minsize, l_float32 minfract)
recogTrainingFinished()
l_ok recogTrainLabeled(L_RECOG *recog, PIX *pixs, BOX *box, char *text, l_int32 debug)
recogTrainLabeled()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
SARRAY * sarrayReadStream(FILE *fp)
sarrayReadStream()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
l_ok sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
struct L_Dna * dna_tochar
struct Pixa * pixadb_split
struct Pixa * pixadb_boot
void lept_stderr(const char *fmt,...)
lept_stderr()
char * stringNew(const char *src)
stringNew()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
FILE * fopenReadStream(const char *filename)
fopenReadStream()