File: recogtest2.c

package info (click to toggle)
leptonlib 1.84.1-4
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 29,476 kB
  • sloc: ansic: 196,041; sh: 4,543; cpp: 2,026; makefile: 284
file content (193 lines) | stat: -rw-r--r-- 7,516 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -
 -  Redistribution and use in source and binary forms, with or without
 -  modification, are permitted provided that the following conditions
 -  are met:
 -  1. Redistributions of source code must retain the above copyright
 -     notice, this list of conditions and the following disclaimer.
 -  2. Redistributions in binary form must reproduce the above
 -     copyright notice, this list of conditions and the following
 -     disclaimer in the documentation and/or other materials
 -     provided with the distribution.
 -
 -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
 -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================*/

/*
 *  recogtest2.c
 *
 *     Test bootstrap recognizer (BSR) to train a book-adapted
 *     recognizer (BAR), starting with unlabeled bitmaps from the book.
 *
 *     Several BSRs are used.
 *     The BAR images are taken from recog/sets/train*.pa.  We really
 *     know their classes, but pretend we don't, by erasing the labels.
 */

#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif  /* HAVE_CONFIG_H */

#include "string.h"
#include "allheaders.h"

    /* Sets for training using boot recognizers */
static char  trainset1[] = "recog/sets/train04.pa";  /* partial set */
static char  trainset2[] = "recog/sets/train05.pa";  /* full set */

    /* Use scanned images or width-normalized lines */
#if 1
static const l_int32  linew = 0;  /* use scanned bitmaps */
#else
static const l_int32  linew = 5;  /* use generated lines */
#endif

l_int32 main(int    argc,
             char **argv)
{
char     *fname;
l_int32   i;
BOXA     *boxa1;
BOXAA    *baa;
NUMAA    *naa;
PIX      *pix1, *pix2, *pix3;
PIXA     *pixa1, *pixa2, *pixa3;
L_RECOG  *recogboot, *recog1;
SARRAY   *sa;

    if (argc != 1) {
        lept_stderr(" Syntax: recogtest2\n");
        return 1;
    }

    setLeptDebugOK(1);
    lept_mkdir("lept/recog");

        /* Files with 'unlabeled' templates from book */
    sa = sarrayCreate(2);
    sarrayAddString(sa, trainset1, L_COPY);
    sarrayAddString(sa, trainset2, L_COPY);

    /* ----------------------------------------------------------- */
    /*        Do operations with a simple bootstrap recognizer     */
    /* ----------------------------------------------------------- */

        /* Generate a BSR (boot-strap recog), and show the unscaled
         * and scaled versions of the templates */
    pixa1 = (PIXA *)l_bootnum_gen1();  /* from recog/digits/bootnum1.pa */
    recogboot = recogCreateFromPixa(pixa1, 0, 40, linew, 128, 1);
    recogWrite("/tmp/lept/recog/boot1.rec", recogboot);
    recogShowContent(stderr, recogboot, 1, 1);
    pixaDestroy(&pixa1);

        /* Generate a BAR (book-adapted recog) for a set of images from
         * one book.  Select a set of digit images.  These happen to
         * be labeled, so we clear the text field from each pix before
         * running it through the boot recognizer. */
    for (i = 0; i < 2; i++) {
        fname = sarrayGetString(sa, i, L_NOCOPY);
        pixa2 = pixaRead(fname);
        pixaSetText(pixa2, NULL, NULL);

            /* Train a new recognizer from the boot and unlabeled samples */
        pixa3 = recogTrainFromBoot(recogboot, pixa2, 0.65, 128, 1);
        recog1 = recogCreateFromPixa(pixa3, 0, 40, linew, 128, 1);
        recogShowContent(stderr, recog1, 2, 1);
        if (i == 0)
            recogWrite("/tmp/lept/recog/recog1.rec", recog1);
        else  /* i == 1 */
            recogWrite("/tmp/lept/recog/recog2.rec", recog1);
        pixaDestroy(&pixa2);
        pixaDestroy(&pixa3);
        recogDestroy(&recog1);
    }
    recogDestroy(&recogboot);

    /* ----------------------------------------------------------- */
    /*        Do operations with a larger bootstrap recognizer     */
    /* ----------------------------------------------------------- */

        /* Generate the boot recog, and show the unscaled and scaled
         * versions of the templates */
    recogboot = recogMakeBootDigitRecog(0, 40, linew, 1, 1);
    recogWrite("/tmp/lept/recog/boot2.rec", recogboot);
    recogShowContent(stderr, recogboot, 3, 1);

        /* Generate a BAR for a set of images from one book.
         * Select a set of digit images and erase the text field. */
    for (i = 0; i < 2; i++) {
        fname = sarrayGetString(sa, i, L_NOCOPY);
        pixa2 = pixaRead(fname);
        pixaSetText(pixa2, NULL, NULL);

            /* Train a new recognizer from the boot and unlabeled samples */
        pixa3 = recogTrainFromBoot(recogboot, pixa2, 0.65, 128, 1);
        recog1 = recogCreateFromPixa(pixa3, 0, 40, linew, 128, 1);
        recogShowContent(stderr, recog1, 4, 1);
        if (i == 0)
            recogWrite("/tmp/lept/recog/recog3.rec", recog1);
        else if (i == 1)
            recogWrite("/tmp/lept/recog/recog4.rec", recog1);
        pixaDestroy(&pixa2);
        pixaDestroy(&pixa3);
        recogDestroy(&recog1);
    }
    recogDestroy(&recogboot);
    sarrayDestroy(&sa);

#if 0
    recogShowMatchesInRange(recog, recog->pixa_tr, 0.0, 1.0, 1);
    recogShowContent(stderr, recog, 1);

        /* Now use minscore = 0.75 to remove the outliers in the BAR,
         * and show what is left. */
    lept_stderr("initial size: %d\n", recog->num_samples);
    pix1 = pix2 = NULL;
    recogRemoveOutliers1(&recog, 0.75, 5, 3, &pix1, &pix2);
    pixDisplay(pix1, 500, 0);
    pixDisplay(pix2, 500, 500);
    pixDestroy(&pix1);
    pixDestroy(&pix2);
    lept_stderr("final size: %d\n", recog->num_samples);
    recogDebugAverages(recog, 1);
    recogShowContent(stderr, recog, 1);
    recogShowMatchesInRange(recog, recog->pixa_tr, 0.75, 1.0, 1);
    pixWrite("/tmp/lept/recog/range.png", recog->pixdb_range, IFF_PNG);
#endif

    /* ----------------------------------------------------------- */
    /*      Show operation of the default bootstrap recognizer     */
    /* ----------------------------------------------------------- */

    recog1 = recogMakeBootDigitRecog(0, 40, 0, 1, 0);
    pix1 = pixRead("test-87220.59.png");
    recogIdentifyMultiple(recog1, pix1, 0, 1, &boxa1, NULL, NULL, 0);
    sa = recogExtractNumbers(recog1, boxa1, 0.75, -1, &baa, &naa);
    pixa1 = showExtractNumbers(pix1, sa, baa, naa, &pix3);
    pix2 = pixaDisplayTiledInRows(pixa1, 32, 600, 1.0, 0, 20, 2);
    pixDisplay(pix2, 0, 1000);
    pixDisplay(pix3, 600, 1000);
    pixWrite("/tmp/lept/recog/extract.png", pix3, IFF_PNG);
    pixDestroy(&pix1);
    pixDestroy(&pix2);
    pixDestroy(&pix3);
    pixaDestroy(&pixa1);
    sarrayDestroy(&sa);
    boxaDestroy(&boxa1);
    boxaaDestroy(&baa);
    numaaDestroy(&naa);
    recogDestroy(&recog1);

    return 0;
}