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
27 /*
28 * recogtest2.c
29 *
30 * Test bootstrap recognizer (BSR) to train a book-adapted
31 * recognizer (BAR), starting with unlabeled bitmaps from the book.
32 *
33 * Several BSRs are used.
34 * The BAR images are taken from recog/sets/train*.pa. We really
35 * know their classes, but pretend we don't, by erasing the labels.
36 */
37
38 #include "string.h"
39 #include "allheaders.h"
40
41 /* Sets for training using boot recognizers */
42 static char trainset1[] = "recog/sets/train04.pa"; /* partial set */
43 static char trainset2[] = "recog/sets/train05.pa"; /* full set */
44
45 /* Use scanned images or width-normalized lines */
46 #if 1
47 static const l_int32 linew = 0; /* use scanned bitmaps */
48 #else
49 static const l_int32 linew = 5; /* use generated lines */
50 #endif
51
main(int argc,char ** argv)52 l_int32 main(int argc,
53 char **argv)
54 {
55 char *fname;
56 l_int32 i;
57 BOXA *boxa1;
58 BOXAA *baa;
59 NUMAA *naa;
60 PIX *pix1, *pix2, *pix3;
61 PIXA *pixa1, *pixa2, *pixa3;
62 L_RECOG *recogboot, *recog1;
63 SARRAY *sa;
64
65 if (argc != 1) {
66 fprintf(stderr, " Syntax: recogtest2\n");
67 return 1;
68 }
69
70 setLeptDebugOK(1);
71 lept_mkdir("lept/recog");
72
73 /* Files with 'unlabeled' templates from book */
74 sa = sarrayCreate(2);
75 sarrayAddString(sa, trainset1, L_COPY);
76 sarrayAddString(sa, trainset2, L_COPY);
77
78 /* ----------------------------------------------------------- */
79 /* Do operations with a simple bootstrap recognizer */
80 /* ----------------------------------------------------------- */
81
82 /* Generate a BSR (boot-strap recog), and show the unscaled
83 * and scaled versions of the templates */
84 pixa1 = (PIXA *)l_bootnum_gen1(); /* from recog/digits/bootnum1.pa */
85 recogboot = recogCreateFromPixa(pixa1, 0, 40, linew, 128, 1);
86 recogWrite("/tmp/lept/recog/boot1.rec", recogboot);
87 recogShowContent(stderr, recogboot, 1, 1);
88 pixaDestroy(&pixa1);
89
90 /* Generate a BAR (book-adapted recog) for a set of images from
91 * one book. Select a set of digit images. These happen to
92 * be labeled, so we clear the text field from each pix before
93 * running it through the boot recognizer. */
94 for (i = 0; i < 2; i++) {
95 fname = sarrayGetString(sa, i, L_NOCOPY);
96 pixa2 = pixaRead(fname);
97 pixaSetText(pixa2, NULL);
98
99 /* Train a new recognizer from the boot and unlabeled samples */
100 pixa3 = recogTrainFromBoot(recogboot, pixa2, 0.65, 128, 1);
101 recog1 = recogCreateFromPixa(pixa3, 0, 40, linew, 128, 1);
102 recogShowContent(stderr, recog1, 2, 1);
103 if (i == 0)
104 recogWrite("/tmp/lept/recog/recog1.rec", recog1);
105 else /* i == 1 */
106 recogWrite("/tmp/lept/recog/recog2.rec", recog1);
107 pixaDestroy(&pixa2);
108 pixaDestroy(&pixa3);
109 recogDestroy(&recog1);
110 }
111 recogDestroy(&recogboot);
112
113 /* ----------------------------------------------------------- */
114 /* Do operations with a larger bootstrap recognizer */
115 /* ----------------------------------------------------------- */
116
117 /* Generate the boot recog, and show the unscaled and scaled
118 * versions of the templates */
119 recogboot = recogMakeBootDigitRecog(40, linew, 1, 1);
120 recogWrite("/tmp/lept/recog/boot2.rec", recogboot);
121 recogShowContent(stderr, recogboot, 3, 1);
122
123 /* Generate a BAR for a set of images from one book.
124 * Select a set of digit images and erase the text field. */
125 for (i = 0; i < 2; i++) {
126 fname = sarrayGetString(sa, i, L_NOCOPY);
127 pixa2 = pixaRead(fname);
128 pixaSetText(pixa2, NULL);
129
130 /* Train a new recognizer from the boot and unlabeled samples */
131 pixa3 = recogTrainFromBoot(recogboot, pixa2, 0.65, 128, 1);
132 recog1 = recogCreateFromPixa(pixa3, 0, 40, linew, 128, 1);
133 recogShowContent(stderr, recog1, 4, 1);
134 if (i == 0)
135 recogWrite("/tmp/lept/recog/recog3.rec", recog1);
136 else if (i == 1)
137 recogWrite("/tmp/lept/recog/recog4.rec", recog1);
138 pixaDestroy(&pixa2);
139 pixaDestroy(&pixa3);
140 recogDestroy(&recog1);
141 }
142 recogDestroy(&recogboot);
143 sarrayDestroy(&sa);
144
145 #if 0
146 recogShowMatchesInRange(recog, recog->pixa_tr, 0.0, 1.0, 1);
147 recogShowContent(stderr, recog, 1);
148
149 /* Now use minscore = 0.75 to remove the outliers in the BAR,
150 * and show what is left. */
151 fprintf(stderr, "initial size: %d\n", recog->num_samples);
152 pix1 = pix2 = NULL;
153 recogRemoveOutliers1(&recog, 0.75, 5, 3, &pix1, &pix2);
154 pixDisplay(pix1, 500, 0);
155 pixDisplay(pix2, 500, 500);
156 pixDestroy(&pix1);
157 pixDestroy(&pix2);
158 fprintf(stderr, "final size: %d\n", recog->num_samples);
159 recogDebugAverages(&recog, 1);
160 recogShowContent(stderr, recog, 1);
161 recogShowMatchesInRange(recog, recog->pixa_tr, 0.75, 1.0, 1);
162 pixWrite("/tmp/lept/recog/range.png", recog->pixdb_range, IFF_PNG);
163 #endif
164
165 /* ----------------------------------------------------------- */
166 /* Show operation of the default bootstrap recognizer */
167 /* ----------------------------------------------------------- */
168
169 recog1 = recogMakeBootDigitRecog(40, 0, 1, 0);
170 pix1 = pixRead("test-87220.59.png");
171 recogIdentifyMultiple(recog1, pix1, 0, 1, &boxa1, NULL, NULL, 0);
172 sa = recogExtractNumbers(recog1, boxa1, 0.75, -1, &baa, &naa);
173 pixa1 = showExtractNumbers(pix1, sa, baa, naa, &pix3);
174 pix2 = pixaDisplayTiledInRows(pixa1, 32, 600, 1.0, 0, 20, 2);
175 pixDisplay(pix2, 0, 1000);
176 pixDisplay(pix3, 600, 1000);
177 pixWrite("/tmp/lept/recog/extract.png", pix3, IFF_PNG);
178 pixDestroy(&pix1);
179 pixDestroy(&pix2);
180 pixDestroy(&pix3);
181 pixaDestroy(&pixa1);
182 sarrayDestroy(&sa);
183 boxaDestroy(&boxa1);
184 boxaaDestroy(&baa);
185 numaaDestroy(&naa);
186 recogDestroy(&recog1);
187
188 return 0;
189 }
190