tesseract  5.0.0-alpha-619-ge9db
tesseract::ShapeClassifier Class Referenceabstract

#include <shapeclassifier.h>

Inheritance diagram for tesseract::ShapeClassifier:
tesseract::MockClassifier tesseract::TessClassifier

Public Member Functions

virtual ~ShapeClassifier ()=default
 
virtual int UnicharClassifySample (const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< UnicharRating > *results)
 
virtual int BestShapeForUnichar (const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id, ShapeRating *result)
 
virtual const ShapeTableGetShapeTable () const =0
 
virtual const UNICHARSETGetUnicharset () const
 
virtual void DebugDisplay (const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id)
 
virtual int DisplayClassifyAs (const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id, int index, PointerVector< ScrollView > *windows)
 
virtual void UnicharPrintResults (const char *context, const GenericVector< UnicharRating > &results) const
 
virtual void PrintResults (const char *context, const GenericVector< ShapeRating > &results) const
 

Protected Member Functions

virtual int ClassifySample (const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< ShapeRating > *results)
 
void FilterDuplicateUnichars (GenericVector< ShapeRating > *results) const
 

Detailed Description

Definition at line 43 of file shapeclassifier.h.

Constructor & Destructor Documentation

◆ ~ShapeClassifier()

virtual tesseract::ShapeClassifier::~ShapeClassifier ( )
virtualdefault

Member Function Documentation

◆ BestShapeForUnichar()

int tesseract::ShapeClassifier::BestShapeForUnichar ( const TrainingSample sample,
Pix *  page_pix,
UNICHAR_ID  unichar_id,
ShapeRating result 
)
virtual

Definition at line 69 of file shapeclassifier.cpp.

71  {
73  const ShapeTable* shapes = GetShapeTable();
74  int num_results = ClassifySample(sample, page_pix, 0, unichar_id, &results);
75  for (int r = 0; r < num_results; ++r) {
76  if (shapes->GetShape(results[r].shape_id).ContainsUnichar(unichar_id)) {
77  if (result != nullptr)
78  *result = results[r];
79  return results[r].shape_id;
80  }
81  }
82  return -1;
83 }

◆ ClassifySample()

int tesseract::ShapeClassifier::ClassifySample ( const TrainingSample sample,
Pix *  page_pix,
int  debug,
UNICHAR_ID  keep_this,
GenericVector< ShapeRating > *  results 
)
protectedvirtual

Reimplemented in tesseract::MockClassifier.

Definition at line 58 of file shapeclassifier.cpp.

60  {
61  ASSERT_HOST("Must implement ClassifySample!" == nullptr);
62  return 0;
63 }

◆ DebugDisplay()

void tesseract::ShapeClassifier::DebugDisplay ( const TrainingSample sample,
Pix *  page_pix,
UNICHAR_ID  unichar_id 
)
virtual

Definition at line 96 of file shapeclassifier.cpp.

98  {
99 #ifndef GRAPHICS_DISABLED
100  static ScrollView* terminator = nullptr;
101  if (terminator == nullptr) {
102  terminator = new ScrollView("XIT", 0, 0, 50, 50, 50, 50, true);
103  }
104  ScrollView* debug_win = CreateFeatureSpaceWindow("ClassifierDebug", 0, 0);
105  // Provide a right-click menu to choose the class.
106  auto* popup_menu = new SVMenuNode();
107  popup_menu->AddChild("Choose class to debug", 0, "x", "Class to debug");
108  popup_menu->BuildMenu(debug_win, false);
109  // Display the features in green.
110  const INT_FEATURE_STRUCT* features = sample.features();
111  uint32_t num_features = sample.num_features();
112  for (uint32_t f = 0; f < num_features; ++f) {
113  RenderIntFeature(debug_win, &features[f], ScrollView::GREEN);
114  }
115  debug_win->Update();
117  // Debug classification until the user quits.
118  const UNICHARSET& unicharset = GetUnicharset();
119  SVEvent* ev;
120  SVEventType ev_type;
121  do {
122  PointerVector<ScrollView> windows;
123  if (unichar_id >= 0) {
124  tprintf("Debugging class %d = %s\n",
125  unichar_id, unicharset.id_to_unichar(unichar_id));
126  UnicharClassifySample(sample, page_pix, 1, unichar_id, &results);
127  DisplayClassifyAs(sample, page_pix, unichar_id, 1, &windows);
128  } else {
129  tprintf("Invalid unichar_id: %d\n", unichar_id);
130  UnicharClassifySample(sample, page_pix, 1, -1, &results);
131  }
132  if (unichar_id >= 0) {
133  tprintf("Debugged class %d = %s\n",
134  unichar_id, unicharset.id_to_unichar(unichar_id));
135  }
136  tprintf("Right-click in ClassifierDebug window to choose debug class,");
137  tprintf(" Left-click or close window to quit...\n");
138  UNICHAR_ID old_unichar_id;
139  do {
140  old_unichar_id = unichar_id;
141  ev = debug_win->AwaitEvent(SVET_ANY);
142  ev_type = ev->type;
143  if (ev_type == SVET_POPUP) {
144  if (unicharset.contains_unichar(ev->parameter)) {
145  unichar_id = unicharset.unichar_to_id(ev->parameter);
146  } else {
147  tprintf("Char class '%s' not found in unicharset", ev->parameter);
148  }
149  }
150  delete ev;
151  } while (unichar_id == old_unichar_id &&
152  ev_type != SVET_CLICK && ev_type != SVET_DESTROY);
153  } while (ev_type != SVET_CLICK && ev_type != SVET_DESTROY);
154  delete debug_win;
155 #endif // GRAPHICS_DISABLED
156 }

◆ DisplayClassifyAs()

int tesseract::ShapeClassifier::DisplayClassifyAs ( const TrainingSample sample,
Pix *  page_pix,
UNICHAR_ID  unichar_id,
int  index,
PointerVector< ScrollView > *  windows 
)
virtual

Reimplemented in tesseract::TessClassifier.

Definition at line 163 of file shapeclassifier.cpp.

166  {
167  // Does nothing in the default implementation.
168  return index;
169 }

◆ FilterDuplicateUnichars()

void tesseract::ShapeClassifier::FilterDuplicateUnichars ( GenericVector< ShapeRating > *  results) const
protected

Definition at line 202 of file shapeclassifier.cpp.

203  {
204  GenericVector<ShapeRating> filtered_results;
205  // Copy results to filtered results and knock out duplicate unichars.
206  const ShapeTable* shapes = GetShapeTable();
207  for (int r = 0; r < results->size(); ++r) {
208  if (r > 0) {
209  const Shape& shape_r = shapes->GetShape((*results)[r].shape_id);
210  int c;
211  for (c = 0; c < shape_r.size(); ++c) {
212  int unichar_id = shape_r[c].unichar_id;
213  int s;
214  for (s = 0; s < r; ++s) {
215  const Shape& shape_s = shapes->GetShape((*results)[s].shape_id);
216  if (shape_s.ContainsUnichar(unichar_id))
217  break; // We found unichar_id.
218  }
219  if (s == r)
220  break; // We didn't find unichar_id.
221  }
222  if (c == shape_r.size())
223  continue; // We found all the unichar ids in previous answers.
224  }
225  filtered_results.push_back((*results)[r]);
226  }
227  *results = filtered_results;
228 }

◆ GetShapeTable()

virtual const ShapeTable* tesseract::ShapeClassifier::GetShapeTable ( ) const
pure virtual

◆ GetUnicharset()

const UNICHARSET & tesseract::ShapeClassifier::GetUnicharset ( ) const
virtual

Reimplemented in tesseract::TessClassifier.

Definition at line 87 of file shapeclassifier.cpp.

87  {
88  return GetShapeTable()->unicharset();
89 }

◆ PrintResults()

void tesseract::ShapeClassifier::PrintResults ( const char *  context,
const GenericVector< ShapeRating > &  results 
) const
virtual

Definition at line 187 of file shapeclassifier.cpp.

188  {
189  tprintf("%s\n", context);
190  for (int i = 0; i < results.size(); ++i) {
191  tprintf("%g:", results[i].rating);
192  if (results[i].joined)
193  tprintf("[J]");
194  if (results[i].broken)
195  tprintf("[B]");
196  tprintf(" %s\n", GetShapeTable()->DebugStr(results[i].shape_id).c_str());
197  }
198 }

◆ UnicharClassifySample()

int tesseract::ShapeClassifier::UnicharClassifySample ( const TrainingSample sample,
Pix *  page_pix,
int  debug,
UNICHAR_ID  keep_this,
GenericVector< UnicharRating > *  results 
)
virtual

Reimplemented in tesseract::TessClassifier.

Definition at line 39 of file shapeclassifier.cpp.

41  {
42  results->truncate(0);
43  GenericVector<ShapeRating> shape_results;
44  int num_shape_results = ClassifySample(sample, page_pix, debug, keep_this,
45  &shape_results);
46  const ShapeTable* shapes = GetShapeTable();
47  GenericVector<int> unichar_map;
48  unichar_map.init_to_size(shapes->unicharset().size(), -1);
49  for (int r = 0; r < num_shape_results; ++r) {
50  shapes->AddShapeToResults(shape_results[r], &unichar_map, results);
51  }
52  return results->size();
53 }

◆ UnicharPrintResults()

void tesseract::ShapeClassifier::UnicharPrintResults ( const char *  context,
const GenericVector< UnicharRating > &  results 
) const
virtual

Definition at line 172 of file shapeclassifier.cpp.

173  {
174  tprintf("%s\n", context);
175  for (int i = 0; i < results.size(); ++i) {
176  tprintf("%g: c_id=%d=%s", results[i].rating, results[i].unichar_id,
177  GetUnicharset().id_to_unichar(results[i].unichar_id));
178  if (!results[i].fonts.empty()) {
179  tprintf(" Font Vector:");
180  for (int f = 0; f < results[i].fonts.size(); ++f) {
181  tprintf(" %d", results[i].fonts[f].fontinfo_id);
182  }
183  }
184  tprintf("\n");
185  }
186 }

The documentation for this class was generated from the following files:
ScrollView
Definition: scrollview.h:97
SVET_DESTROY
Definition: scrollview.h:45
SVEventType
SVEventType
Definition: scrollview.h:44
SVET_CLICK
Definition: scrollview.h:47
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
SVET_POPUP
Definition: scrollview.h:53
tesseract::ShapeClassifier::GetShapeTable
virtual const ShapeTable * GetShapeTable() const =0
tesseract::ShapeClassifier::UnicharClassifySample
virtual int UnicharClassifySample(const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< UnicharRating > *results)
Definition: shapeclassifier.cpp:39
tesseract::ShapeClassifier::DisplayClassifyAs
virtual int DisplayClassifyAs(const TrainingSample &sample, Pix *page_pix, UNICHAR_ID unichar_id, int index, PointerVector< ScrollView > *windows)
Definition: shapeclassifier.cpp:163
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::ShapeTable::unicharset
const UNICHARSET & unicharset() const
Definition: shapetable.h:277
SVEvent::parameter
char * parameter
Definition: scrollview.h:65
UNICHARSET::unichar_to_id
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:209
UNICHARSET
Definition: unicharset.h:145
CreateFeatureSpaceWindow
ScrollView * CreateFeatureSpaceWindow(const char *name, int xpos, int ypos)
Definition: intproto.cpp:1764
tesseract::ShapeClassifier::ClassifySample
virtual int ClassifySample(const TrainingSample &sample, Pix *page_pix, int debug, UNICHAR_ID keep_this, GenericVector< ShapeRating > *results)
Definition: shapeclassifier.cpp:58
SVEvent::type
SVEventType type
Definition: scrollview.h:63
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
sample
Definition: cluster.h:31
GenericVector
Definition: baseapi.h:40
SVET_ANY
Definition: scrollview.h:55
ScrollView::AwaitEvent
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:443
INT_FEATURE_STRUCT
Definition: intproto.h:131
SVMenuNode
Definition: svmnode.h:35
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
SVEvent
Definition: scrollview.h:60
UNICHARSET::contains_unichar
bool contains_unichar(const char *const unichar_repr) const
Definition: unicharset.cpp:670
ScrollView::GREEN
Definition: scrollview.h:106
tesseract::ShapeClassifier::GetUnicharset
virtual const UNICHARSET & GetUnicharset() const
Definition: shapeclassifier.cpp:87
GenericVector::init_to_size
void init_to_size(int size, const T &t)
Definition: genericvector.h:706
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
ScrollView::Update
static void Update()
Definition: scrollview.cpp:708
UNICHARSET::id_to_unichar
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:290
GenericVector::size
int size() const
Definition: genericvector.h:71
RenderIntFeature
void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT *Feature, ScrollView::Color color)
Definition: intproto.cpp:1603