Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blobclass.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: blobclass.c
3  ** Purpose: High level blob classification and training routines.
4  ** Author: Dan Johnson
5  ** History: 7/21/89, DSJ, Created.
6  **
7  ** (c) Copyright Hewlett-Packard Company, 1988.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  ******************************************************************************/
18 
22 #include "blobclass.h"
23 #include "extract.h"
24 #include "efio.h"
25 #include "featdefs.h"
26 #include "callcpp.h"
27 #include "chartoname.h"
28 
29 #include <math.h>
30 #include <stdio.h>
31 #include <signal.h>
32 
33 #define MAXFILENAME 80
34 #define MAXMATCHES 10
35 
36 static const char kUnknownFontName[] = "UnknownFont";
37 
38 STRING_VAR(classify_font_name, kUnknownFontName,
39  "Default font name to be used in training");
40 
44 /* name of current image file being processed */
45 extern char imagefile[];
46 
51 /*---------------------------------------------------------------------------*/
52 void LearnBlob(const FEATURE_DEFS_STRUCT &FeatureDefs, const STRING& filename,
53  TBLOB * Blob, const DENORM& denorm, const char* BlobText) {
54 /*
55  ** Parameters:
56  ** Blob blob whose micro-features are to be learned
57  ** Row row of text that blob came from
58  ** BlobText text that corresponds to blob
59  ** TextLength number of characters in blob
60  ** Globals:
61  ** imagefile base filename of the page being learned
62  ** classify_font_name
63  ** name of font currently being trained on
64  ** Operation:
65  ** Extract micro-features from the specified blob and append
66  ** them to the appropriate file.
67  ** Return: none
68  ** Exceptions: none
69  ** History: 7/28/89, DSJ, Created.
70  */
71 #define TRAIN_SUFFIX ".tr"
72  static FILE *FeatureFile = NULL;
73  STRING Filename(filename);
74 
75  // If no fontname was set, try to extract it from the filename
76  STRING CurrFontName = classify_font_name;
77  if (CurrFontName == kUnknownFontName) {
78  // filename is expected to be of the form [lang].[fontname].exp[num]
79  // The [lang], [fontname] and [num] fields should not have '.' characters.
80  const char *basename = strrchr(filename.string(), '/');
81  const char *firstdot = strchr(basename ? basename : filename.string(), '.');
82  const char *lastdot = strrchr(filename.string(), '.');
83  if (firstdot != lastdot && firstdot != NULL && lastdot != NULL) {
84  ++firstdot;
85  CurrFontName = firstdot;
86  CurrFontName[lastdot - firstdot] = '\0';
87  }
88  }
89 
90  // if a feature file is not yet open, open it
91  // the name of the file is the name of the image plus TRAIN_SUFFIX
92  if (FeatureFile == NULL) {
93  Filename += TRAIN_SUFFIX;
94  FeatureFile = Efopen(Filename.string(), "wb");
95  cprintf("TRAINING ... Font name = %s\n", CurrFontName.string());
96  }
97 
98  LearnBlob(FeatureDefs, FeatureFile, Blob, denorm, BlobText,
99  CurrFontName.string());
100 } // LearnBlob
101 
102 void LearnBlob(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE* FeatureFile,
103  TBLOB* Blob, const DENORM& denorm,
104  const char* BlobText, const char* FontName) {
105  CHAR_DESC CharDesc;
106 
107  ASSERT_HOST(FeatureFile != NULL);
108 
109  CharDesc = ExtractBlobFeatures(FeatureDefs, denorm, Blob);
110  if (CharDesc == NULL) {
111  cprintf("LearnBLob: CharDesc was NULL. Aborting.\n");
112  return;
113  }
114 
115  if (ValidCharDescription(FeatureDefs, CharDesc)) {
116  // label the features with a class name and font name
117  fprintf(FeatureFile, "\n%s %s\n", FontName, BlobText);
118 
119  // write micro-features to file and clean up
120  WriteCharDescription(FeatureDefs, FeatureFile, CharDesc);
121  } else {
122  tprintf("Blob learned was invalid!\n");
123  }
124  FreeCharDescription(CharDesc);
125 
126 } // LearnBlob