tesseract  4.1.0
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  **
6  ** (c) Copyright Hewlett-Packard Company, 1988.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  ******************************************************************************/
17 
21 #include "blobclass.h"
22 
23 #include <cstdio>
24 
25 #include "classify.h"
26 #ifndef DISABLED_LEGACY_ENGINE
27 #include "featdefs.h"
28 #include "mf.h"
29 #include "normfeat.h"
30 #endif // ndef DISABLED_LEGACY_ENGINE
31 
32 static const char kUnknownFontName[] = "UnknownFont";
33 
34 static STRING_VAR(classify_font_name, kUnknownFontName,
35  "Default font name to be used in training");
36 
37 namespace tesseract {
41 // Finds the name of the training font and returns it in fontname, by cutting
42 // it out based on the expectation that the filename is of the form:
43 // /path/to/dir/[lang].[fontname].exp[num]
44 // The [lang], [fontname] and [num] fields should not have '.' characters.
45 // If the global parameter classify_font_name is set, its value is used instead.
46 void ExtractFontName(const STRING& filename, STRING* fontname) {
47  *fontname = classify_font_name;
48  if (*fontname == kUnknownFontName) {
49  // filename is expected to be of the form [lang].[fontname].exp[num]
50  // The [lang], [fontname] and [num] fields should not have '.' characters.
51  const char *basename = strrchr(filename.string(), '/');
52  const char *firstdot = strchr(basename ? basename : filename.string(), '.');
53  const char *lastdot = strrchr(filename.string(), '.');
54  if (firstdot != lastdot && firstdot != nullptr && lastdot != nullptr) {
55  ++firstdot;
56  *fontname = firstdot;
57  fontname->truncate_at(lastdot - firstdot);
58  }
59  }
60 }
61 
62 
63 /*---------------------------------------------------------------------------*/
64 
65 #ifndef DISABLED_LEGACY_ENGINE
66 
67 // Extracts features from the given blob and saves them in the tr_file_data_
68 // member variable.
69 // fontname: Name of font that this blob was printed in.
70 // cn_denorm: Character normalization transformation to apply to the blob.
71 // fx_info: Character normalization parameters computed with cn_denorm.
72 // blob_text: Ground truth text for the blob.
73 void Classify::LearnBlob(const STRING& fontname, TBLOB* blob,
74  const DENORM& cn_denorm,
75  const INT_FX_RESULT_STRUCT& fx_info,
76  const char* blob_text) {
78  CharDesc->FeatureSets[0] = ExtractMicros(blob, cn_denorm);
79  CharDesc->FeatureSets[1] = ExtractCharNormFeatures(fx_info);
80  CharDesc->FeatureSets[2] = ExtractIntCNFeatures(*blob, fx_info);
81  CharDesc->FeatureSets[3] = ExtractIntGeoFeatures(*blob, fx_info);
82 
83  if (ValidCharDescription(feature_defs_, CharDesc)) {
84  // Label the features with a class name and font name.
85  tr_file_data_ += "\n";
86  tr_file_data_ += fontname;
87  tr_file_data_ += " ";
88  tr_file_data_ += blob_text;
89  tr_file_data_ += "\n";
90 
91  // write micro-features to file and clean up
92  WriteCharDescription(feature_defs_, CharDesc, &tr_file_data_);
93  } else {
94  tprintf("Blob learned was invalid!\n");
95  }
96  FreeCharDescription(CharDesc);
97 } // LearnBlob
98 
99 // Writes stored training data to a .tr file based on the given filename.
100 // Returns false on error.
101 bool Classify::WriteTRFile(const STRING& filename) {
102  bool result = false;
103  STRING tr_filename = filename + ".tr";
104  FILE* fp = fopen(tr_filename.string(), "wb");
105  if (fp) {
106  result =
107  tesseract::Serialize(fp, &tr_file_data_[0], tr_file_data_.length());
108  fclose(fp);
109  }
110  tr_file_data_.truncate_at(0);
111  return result;
112 }
113 
114 #endif // ndef DISABLED_LEGACY_ENGINE
115 
116 } // namespace tesseract.
bool WriteTRFile(const STRING &filename)
Definition: blobclass.cpp:101
Definition: strngs.h:45
FEATURE_DEFS_STRUCT feature_defs_
Definition: classify.h:547
int32_t length() const
Definition: strngs.cpp:189
void ExtractFontName(const STRING &filename, STRING *fontname)
Definition: blobclass.cpp:46
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
Definition: blobs.h:263
FEATURE_SET ExtractIntGeoFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:247
void truncate_at(int32_t index)
Definition: strngs.cpp:265
FEATURE_SET ExtractMicros(TBLOB *Blob, const DENORM &cn_denorm)
Definition: mf.cpp:43
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:148
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:129
#define STRING_VAR(name, val, comment)
Definition: params.h:309
const char * string() const
Definition: strngs.cpp:194
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:174
FEATURE_SET ExtractCharNormFeatures(const INT_FX_RESULT_STRUCT &fx_info)
Definition: normfeat.cpp:61
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:73
FEATURE_SET ExtractIntCNFeatures(const TBLOB &blob, const INT_FX_RESULT_STRUCT &fx_info)
Definition: picofeat.cpp:217
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:195