tesseract  4.0.0-1-g2a2b
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 
24 #include <cstdio>
25 
26 #include "classify.h"
27 #ifndef DISABLED_LEGACY_ENGINE
28 #include "featdefs.h"
29 #include "mf.h"
30 #include "normfeat.h"
31 #endif // ndef DISABLED_LEGACY_ENGINE
32 
33 static const char kUnknownFontName[] = "UnknownFont";
34 
35 STRING_VAR(classify_font_name, kUnknownFontName,
36  "Default font name to be used in training");
37 
38 namespace tesseract {
42 // Finds the name of the training font and returns it in fontname, by cutting
43 // it out based on the expectation that the filename is of the form:
44 // /path/to/dir/[lang].[fontname].exp[num]
45 // The [lang], [fontname] and [num] fields should not have '.' characters.
46 // If the global parameter classify_font_name is set, its value is used instead.
47 void ExtractFontName(const STRING& filename, STRING* fontname) {
48  *fontname = classify_font_name;
49  if (*fontname == kUnknownFontName) {
50  // filename is expected to be of the form [lang].[fontname].exp[num]
51  // The [lang], [fontname] and [num] fields should not have '.' characters.
52  const char *basename = strrchr(filename.string(), '/');
53  const char *firstdot = strchr(basename ? basename : filename.string(), '.');
54  const char *lastdot = strrchr(filename.string(), '.');
55  if (firstdot != lastdot && firstdot != nullptr && lastdot != nullptr) {
56  ++firstdot;
57  *fontname = firstdot;
58  fontname->truncate_at(lastdot - firstdot);
59  }
60  }
61 }
62 
63 
64 /*---------------------------------------------------------------------------*/
65 
66 #ifndef DISABLED_LEGACY_ENGINE
67 
68 // Extracts features from the given blob and saves them in the tr_file_data_
69 // member variable.
70 // fontname: Name of font that this blob was printed in.
71 // cn_denorm: Character normalization transformation to apply to the blob.
72 // fx_info: Character normalization parameters computed with cn_denorm.
73 // blob_text: Ground truth text for the blob.
74 void Classify::LearnBlob(const STRING& fontname, TBLOB* blob,
75  const DENORM& cn_denorm,
76  const INT_FX_RESULT_STRUCT& fx_info,
77  const char* blob_text) {
79  CharDesc->FeatureSets[0] = ExtractMicros(blob, cn_denorm);
80  CharDesc->FeatureSets[1] = ExtractCharNormFeatures(fx_info);
81  CharDesc->FeatureSets[2] = ExtractIntCNFeatures(*blob, fx_info);
82  CharDesc->FeatureSets[3] = ExtractIntGeoFeatures(*blob, fx_info);
83 
84  if (ValidCharDescription(feature_defs_, CharDesc)) {
85  // Label the features with a class name and font name.
86  tr_file_data_ += "\n";
87  tr_file_data_ += fontname;
88  tr_file_data_ += " ";
89  tr_file_data_ += blob_text;
90  tr_file_data_ += "\n";
91 
92  // write micro-features to file and clean up
93  WriteCharDescription(feature_defs_, CharDesc, &tr_file_data_);
94  } else {
95  tprintf("Blob learned was invalid!\n");
96  }
97  FreeCharDescription(CharDesc);
98 } // LearnBlob
99 
100 // Writes stored training data to a .tr file based on the given filename.
101 // Returns false on error.
102 bool Classify::WriteTRFile(const STRING& filename) {
103  bool result = false;
104  STRING tr_filename = filename + ".tr";
105  FILE* fp = fopen(tr_filename.string(), "wb");
106  if (fp) {
107  result =
108  tesseract::Serialize(fp, &tr_file_data_[0], tr_file_data_.length());
109  fclose(fp);
110  }
111  tr_file_data_.truncate_at(0);
112  return result;
113 }
114 
115 #endif // ndef DISABLED_LEGACY_ENGINE
116 
117 } // namespace tesseract.
void ExtractFontName(const STRING &filename, STRING *fontname)
Definition: blobclass.cpp:47
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:129
FEATURE_SET ExtractIntCNFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:219
FEATURE_SET ExtractIntGeoFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:249
#define STRING_VAR(name, val, comment)
Definition: params.h:282
const char * string() const
Definition: strngs.cpp:196
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void truncate_at(int32_t index)
Definition: strngs.cpp:267
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:195
Definition: strngs.h:45
bool WriteTRFile(const STRING &filename)
Definition: blobclass.cpp:102
FEATURE_SET ExtractCharNormFeatures(const INT_FX_RESULT_STRUCT &fx_info)
Definition: normfeat.cpp:61
char * classify_font_name
Definition: blobclass.cpp:36
void LearnBlob(const STRING &fontname, TBLOB *Blob, const DENORM &cn_denorm, const INT_FX_RESULT_STRUCT &fx_info, const char *blob_text)
Definition: blobclass.cpp:74
Definition: blobs.h:268
int32_t length() const
Definition: strngs.cpp:191
FEATURE_DEFS_STRUCT feature_defs_
Definition: classify.h:548
FEATURE_SET ExtractMicros(TBLOB *Blob, const DENORM &cn_denorm)
Definition: mf.cpp:43
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:148
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:174