Coverage Report

Created: 2025-06-13 07:15

/src/tesseract/src/classify/tessclassifier.cpp
Line
Count
Source (jump to first uncovered line)
1
///////////////////////////////////////////////////////////////////////
2
// File:        tessclassifier.cpp
3
// Description: Tesseract implementation of a ShapeClassifier.
4
// Author:      Ray Smith
5
//
6
// (C) Copyright 2011, Google Inc.
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
///////////////////////////////////////////////////////////////////////
18
19
#include "tessclassifier.h"
20
21
#include "classify.h"
22
#include "trainingsample.h"
23
24
namespace tesseract {
25
26
// Classifies the given [training] sample, writing to results.
27
// See ShapeClassifier for a full description.
28
int TessClassifier::UnicharClassifySample(const TrainingSample &sample, Image page_pix, int debug,
29
                                          UNICHAR_ID keep_this,
30
1.20M
                                          std::vector<UnicharRating> *results) {
31
1.20M
  const int old_matcher_level = classify_->matcher_debug_level;
32
1.20M
  const int old_matcher_flags = classify_->matcher_debug_flags;
33
1.20M
  const int old_classify_level = classify_->classify_debug_level;
34
1.20M
  if (debug) {
35
    // Explicitly set values of various control parameters to generate debug
36
    // output if required, restoring the old values after classifying.
37
0
    classify_->matcher_debug_level.set_value(2);
38
0
    classify_->matcher_debug_flags.set_value(25);
39
0
    classify_->classify_debug_level.set_value(3);
40
0
  }
41
1.20M
  classify_->CharNormTrainingSample(pruner_only_, keep_this, sample, results);
42
1.20M
  if (debug) {
43
0
    classify_->matcher_debug_level.set_value(old_matcher_level);
44
0
    classify_->matcher_debug_flags.set_value(old_matcher_flags);
45
0
    classify_->classify_debug_level.set_value(old_classify_level);
46
0
  }
47
1.20M
  return results->size();
48
1.20M
}
49
50
// Provides access to the ShapeTable that this classifier works with.
51
0
const ShapeTable *TessClassifier::GetShapeTable() const {
52
0
  return classify_->shape_table();
53
0
}
54
// Provides access to the UNICHARSET that this classifier works with.
55
// Only needs to be overridden if GetShapeTable() can return nullptr.
56
0
const UNICHARSET &TessClassifier::GetUnicharset() const {
57
0
  return classify_->unicharset;
58
0
}
59
60
// Displays classification as the given shape_id. Creates as many windows
61
// as it feels fit, using index as a guide for placement. Adds any created
62
// windows to the windows output and returns a new index that may be used
63
// by any subsequent classifiers. Caller waits for the user to view and
64
// then destroys the windows by clearing the vector.
65
int TessClassifier::DisplayClassifyAs(const TrainingSample &sample, Image page_pix, int unichar_id,
66
0
                                      int index, std::vector<ScrollView *> &windows) {
67
0
  int shape_id = unichar_id;
68
  // TODO(rays) Fix this so it works with both flat and real shapetables.
69
  //  if (GetShapeTable() != nullptr)
70
  //  shape_id = BestShapeForUnichar(sample, page_pix, unichar_id, nullptr);
71
0
  if (shape_id < 0) {
72
0
    return index;
73
0
  }
74
0
  if (UnusedClassIdIn(classify_->PreTrainedTemplates, shape_id)) {
75
0
    tprintf("No built-in templates for class/shape %d\n", shape_id);
76
0
    return index;
77
0
  }
78
#ifndef GRAPHICS_DISABLED
79
  classify_->ShowBestMatchFor(shape_id, sample.features(), sample.num_features());
80
#endif
81
0
  return index;
82
0
}
83
84
} // namespace tesseract