190 #include "allheaders.h" 192 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20;
193 static const l_int32 MAX_EXAMPLES_IN_CLASS = 256;
197 static const l_int32 DEFAULT_MIN_NOPAD = 1;
198 static const l_float32 DEFAULT_MAX_WH_RATIO = 3.0;
200 static const l_float32 DEFAULT_MAX_HT_RATIO = 2.6;
202 static const l_int32 DEFAULT_THRESHOLD = 150;
203 static const l_int32 DEFAULT_MAXYSHIFT = 1;
244 PROCNAME(
"recogCreateFromRecog");
247 return (
L_RECOG *)ERROR_PTR(
"recs not defined", procName, NULL);
290 PROCNAME(
"recogCreateFromPixa");
293 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
296 threshold, maxyshift);
298 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
302 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
335 l_int32 full, n, i, ntext, same, maxd;
339 PROCNAME(
"recogCreateFromPixaNoFinish");
342 return (
L_RECOG *)ERROR_PTR(
"pixa not defined", procName, NULL);
345 return (
L_RECOG *)ERROR_PTR(
"not all pix are 1 bpp", procName, NULL);
349 return (
L_RECOG *)ERROR_PTR(
"not all pix are present", procName, NULL);
354 return (
L_RECOG *)ERROR_PTR(
"no pix have text strings", procName, NULL);
356 L_ERROR(
"%d text strings < %d pix\n", procName, ntext, n);
358 recog =
recogCreate(scalew, scaleh, linew, threshold, maxyshift);
360 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
361 for (i = 0; i < n; i++) {
364 if (!text || strlen(text) == 0) {
365 L_ERROR(
"pix[%d] has no text\n", procName, i);
416 PROCNAME(
"recogCreate");
418 if (scalew < 0 || scaleh < 0)
419 return (
L_RECOG *)ERROR_PTR(
"invalid scalew or scaleh", procName, NULL);
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;
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",
433 }
else if (maxyshift == 2) {
434 L_WARNING(
"Using maxyshift = 2; slower\n", procName);
438 return (
L_RECOG *)ERROR_PTR(
"rec not made", procName, NULL);
443 recog->
linew = linew;
482 PROCNAME(
"recogDestroy");
485 L_WARNING(
"ptr address is null\n", procName);
489 if ((recog = *precog) == NULL)
return;
536 PROCNAME(
"recogGetCount");
539 return ERROR_INT(
"recog not defined", procName, 0);
575 l_float32 max_wh_ratio,
576 l_float32 max_ht_ratio)
578 PROCNAME(
"recogSetParams");
581 return ERROR_INT(
"recog not defined", procName, 1);
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;
603 PROCNAME(
"recogGetCharsetSize");
619 L_ERROR(
"invalid charset_type %d\n", procName, type);
661 PROCNAME(
"recogGetClassIndex");
664 return ERROR_INT(
"&index not defined", procName, 2);
667 return ERROR_INT(
"recog not defined", procName, 2);
669 return ERROR_INT(
"text not defined", procName, 2);
673 for (i = 0; i < n; i++) {
706 PROCNAME(
"recogStringtoIndex");
709 return ERROR_INT(
"&index not defined", procName, 1);
712 return ERROR_INT(
"recog not defined", procName, 1);
714 return ERROR_INT(
"text not defined", procName, 1);
718 for (i = 0; i < n; i++) {
721 L_ERROR(
"string not found for index %d\n", procName, i);
724 diff = strcmp(text, charstr);
756 PROCNAME(
"recogGetClassString");
759 return ERROR_INT(
"&charstr not defined", procName, 1);
762 return ERROR_INT(
"recog not defined", procName, 2);
764 if (index < 0 || index >= recog->
setsize)
765 return ERROR_INT(
"invalid index", procName, 1);
766 LEPT_FREE(*pcharstr);
787 PROCNAME(
"l_convertCharstrToInt");
790 return ERROR_INT(
"&val not defined", procName, 1);
793 return ERROR_INT(
"str not defined", procName, 1);
796 return ERROR_INT(
"empty string", procName, 1);
798 return ERROR_INT(
"invalid string: > 4 bytes", procName, 1);
800 val = (l_int32)str[0];
802 val = (val << 8) + (l_int32)str[1];
804 val = (val << 8) + (l_int32)str[2];
806 val = (val << 8) + (l_int32)str[3];
846 PROCNAME(
"recogRead");
849 return (
L_RECOG *)ERROR_PTR(
"filename not defined", procName, NULL);
851 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
855 return (
L_RECOG *)ERROR_PTR(
"recog not read", procName, NULL);
872 l_int32 version, setsize, threshold, scalew, scaleh, linew;
873 l_int32 maxyshift, nc;
879 PROCNAME(
"recogReadStream");
882 return (
L_RECOG *)ERROR_PTR(
"stream not defined", procName, NULL);
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,
902 return (
L_RECOG *)ERROR_PTR(
"recog not made", procName, NULL);
904 if (fscanf(fp,
"\nLabels for character set:\n") != 0) {
906 return (
L_RECOG *)ERROR_PTR(
"label intro not read", procName, NULL);
911 return (
L_RECOG *)ERROR_PTR(
"dna_tochar not read", procName, NULL);
917 return (
L_RECOG *)ERROR_PTR(
"sa_text not read", procName, NULL);
921 if (fscanf(fp,
"\nPixaa of all samples in the training set:\n") != 0) {
923 return (
L_RECOG *)ERROR_PTR(
"pixaa intro not read", procName, NULL);
927 return (
L_RECOG *)ERROR_PTR(
"pixaa not read", procName, NULL);
934 L_ERROR(
"(setsize = %d) != (paa count = %d)\n", procName,
942 return (
L_RECOG *)ERROR_PTR(
"bad templates", procName, NULL);
961 PROCNAME(
"recogReadMem");
964 return (
L_RECOG *)ERROR_PTR(
"data not defined", procName, NULL);
966 return (
L_RECOG *)ERROR_PTR(
"stream not opened", procName, NULL);
970 if (!recog) L_ERROR(
"recog not read\n", procName);
998 PROCNAME(
"recogWrite");
1001 return ERROR_INT(
"filename not defined", procName, 1);
1003 return ERROR_INT(
"recog not defined", procName, 1);
1006 return ERROR_INT(
"stream not opened", procName, 1);
1010 return ERROR_INT(
"recog not written to stream", procName, 1);
1026 PROCNAME(
"recogWriteStream");
1029 return ERROR_INT(
"stream not defined", procName, 1);
1031 return ERROR_INT(
"recog not defined", procName, 1);
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");
1043 fprintf(fp,
"\nPixaa of all samples in the training set:\n");
1071 PROCNAME(
"recogWriteMem");
1073 if (pdata) *pdata = NULL;
1074 if (psize) *psize = 0;
1076 return ERROR_INT(
"&data not defined", procName, 1);
1078 return ERROR_INT(
"&size not defined", procName, 1);
1080 return ERROR_INT(
"recog not defined", procName, 1);
1083 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1084 return ERROR_INT(
"stream not opened", procName, 1);
1087 L_INFO(
"work-around: writing to a temp file\n", procName);
1090 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1092 if ((fp = tmpfile()) == NULL)
1093 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
1120 PROCNAME(
"recogExtractPixa");
1123 return (
PIXA *)ERROR_PTR(
"recog not defined", procName, NULL);
1140 l_int32 i, j, n1, n2;
1145 PROCNAME(
"recogAddCharstrLabels");
1148 return ERROR_INT(
"recog not defined", procName, 1);
1153 for (i = 0; i < n1; i++) {
1157 for (j = 0; j < n2; j++) {
1194 l_int32 i, j, nc, ns;
1199 PROCNAME(
"recogAddAllSamples");
1202 return ERROR_INT(
"&recog not defined", procName, 1);
1203 if ((recog = *precog) == NULL)
1204 return ERROR_INT(
"recog not defined", procName, 1);
1207 return ERROR_INT(
"paa not defined", procName, 1);
1211 for (i = 0; i < nc; i++) {
1217 for (j = 0; j < ns; j++) {
1219 if (debug) fprintf(stderr,
"pix[%d,%d]: text = %s\n", i, j, text);
1228 return ERROR_INT(
"bad templates; recog destroyed", procName, 1);
l_ok recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
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_ok recogWriteMem(l_uint8 **pdata, size_t *psize, L_RECOG *recog)
recogWriteMem()
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
l_int32 * makePixelCentroidTab8(void)
makePixelCentroidTab8()
l_ok recogTrainLabeled(L_RECOG *recog, PIX *pixs, BOX *box, char *text, l_int32 debug)
recogTrainLabeled()
l_ok pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
l_ok pixaCountText(PIXA *pixa, l_int32 *pntext)
pixaCountText()
char * stringNew(const char *src)
stringNew()
struct Pixa * pixadb_split
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 pixaaGetCount(PIXAA *paa, NUMA **pna)
pixaaGetCount()
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
void rchDestroy(L_RCH **prch)
rchDestroy()
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
l_ok sarrayWriteStream(FILE *fp, SARRAY *sa)
sarrayWriteStream()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
l_ok recogWrite(const char *filename, L_RECOG *recog)
recogWrite()
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
l_ok recogWriteStream(FILE *fp, L_RECOG *recog)
recogWriteStream()
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
l_ok recogTrainingFinished(L_RECOG **precog, l_int32 modifyflag, l_int32 minsize, l_float32 minfract)
recogTrainingFinished()
l_ok l_dnaWriteStream(FILE *fp, L_DNA *da)
l_dnaWriteStream()
l_int32 recogGetCount(L_RECOG *recog)
recogGetCount()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
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()
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
L_DNA * l_dnaReadStream(FILE *fp)
l_dnaReadStream()
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()
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()
char * pixGetText(PIX *pix)
pixGetText()
l_ok pixaIsFull(PIXA *pixa, l_int32 *pfullpa, l_int32 *pfullba)
pixaIsFull()
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_ok pixaaAddPixa(PIXAA *paa, PIXA *pixa, l_int32 copyflag)
pixaaAddPixa()
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_ok recogStringToIndex(L_RECOG *recog, char *text, l_int32 *pindex)
recogStringToIndex()
l_ok pixaaWriteStream(FILE *fp, PIXAA *paa)
pixaaWriteStream()
static l_int32 recogAddAllSamples(L_RECOG **precog, PIXAA *paa, l_int32 debug)
recogAddAllSamples()
l_ok recogSetParams(L_RECOG *recog, l_int32 type, l_int32 min_nopad, l_float32 max_wh_ratio, l_float32 max_ht_ratio)
recogSetParams()
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_ok pixaaAddPix(PIXAA *paa, l_int32 index, PIX *pix, BOX *box, l_int32 copyflag)
pixaaAddPix()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()