tesseract  5.0.0-alpha-619-ge9db
applybox_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include <string>
13 #include "allheaders.h"
14 #include <tesseract/baseapi.h>
15 #include "boxread.h"
16 #include "rect.h"
18 
19 #include "include_gunit.h"
20 
21 namespace {
22 
24 
25 const char* kTruthTextWords = "To simple burn running of goods lately.\n";
26 const char* kTruthTextLine = "Tosimpleburnrunningofgoodslately.\n";
27 
28 // The fixture for testing Tesseract.
29 class ApplyBoxTest : public testing::Test {
30  protected:
31  std::string TestDataNameToPath(const std::string& name) {
32  return file::JoinPath(TESTING_DIR, name);
33  }
34  std::string TessdataPath() { return TESSDATA_DIR; }
35 
36  ApplyBoxTest() { src_pix_ = nullptr; }
37  ~ApplyBoxTest() { pixDestroy(&src_pix_); }
38 
39  bool SetImage(const char* filename) {
40  bool found = false;
41  pixDestroy(&src_pix_);
42  src_pix_ = pixRead(TestDataNameToPath(filename).c_str());
43  if (api_.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY) != -1) {
44  api_.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
45  api_.SetImage(src_pix_);
46  api_.SetVariable("tessedit_make_boxes_from_boxes", "1");
47  api_.SetInputName(TestDataNameToPath(filename).c_str());
48  found = true;
49  }
50  return found;
51  }
52 
53  // Runs ApplyBoxes (via setting the appropriate variables and Recognize)
54  // and checks that the output ocr text matches the truth_str, and that
55  // the boxes match the given box file well enough.
56  // If line_mode is true, ApplyBoxes is run in line segmentation mode,
57  // otherwise the input box file is assumed to have character-level boxes.
58  void VerifyBoxesAndText(const char* imagefile, const char* truth_str,
59  const char* target_box_file, bool line_mode) {
60  if (!SetImage(imagefile)) {
61  // eng.traineddata not found or other problem during Init.
62  GTEST_SKIP();
63  return;
64  }
65  if (line_mode)
66  api_.SetVariable("tessedit_resegment_from_line_boxes", "1");
67  else
68  api_.SetVariable("tessedit_resegment_from_boxes", "1");
69  api_.Recognize(nullptr);
70  char* ocr_text = api_.GetUTF8Text();
71  EXPECT_STREQ(truth_str, ocr_text);
72  delete[] ocr_text;
73  // Test the boxes by reading the target box file in parallel with the
74  // bounding boxes in the ocr output.
75  std::string box_filename = TestDataNameToPath(target_box_file);
76  FILE* box_file = OpenBoxFile(STRING(box_filename.c_str()));
77  ASSERT_TRUE(box_file != nullptr);
78  int height = pixGetHeight(src_pix_);
79  ResultIterator* it = api_.GetIterator();
80  do {
81  int left, top, right, bottom;
82  EXPECT_TRUE(
83  it->BoundingBox(tesseract::RIL_SYMBOL, &left, &top, &right, &bottom));
84  TBOX ocr_box(ICOORD(left, height - bottom), ICOORD(right, height - top));
85  int line_number = 0;
86  TBOX truth_box;
87  STRING box_text;
88  EXPECT_TRUE(
89  ReadNextBox(0, &line_number, box_file, &box_text, &truth_box));
90  // Testing for major overlap is a bit weak, but if they all
91  // major overlap successfully, then it has to be fairly close.
92  EXPECT_TRUE(ocr_box.major_overlap(truth_box));
93  // Also check that the symbol text matches the box text.
94  char* symbol_text = it->GetUTF8Text(tesseract::RIL_SYMBOL);
95  EXPECT_STREQ(box_text.c_str(), symbol_text);
96  delete[] symbol_text;
97  } while (it->Next(tesseract::RIL_SYMBOL));
98  delete it;
99  }
100 
101  Pix* src_pix_;
102  std::string ocr_text_;
104 };
105 
106 // Tests character-level applyboxes on normal Times New Roman.
107 TEST_F(ApplyBoxTest, TimesCharLevel) {
108  VerifyBoxesAndText("trainingtimes.tif", kTruthTextWords, "trainingtimes.box",
109  false);
110 }
111 
112 // Tests character-level applyboxes on italic Times New Roman.
113 TEST_F(ApplyBoxTest, ItalicCharLevel) {
114  VerifyBoxesAndText("trainingital.tif", kTruthTextWords, "trainingital.box",
115  false);
116 }
117 
118 // Tests line-level applyboxes on normal Times New Roman.
119 TEST_F(ApplyBoxTest, TimesLineLevel) {
120  VerifyBoxesAndText("trainingtimesline.tif", kTruthTextLine,
121  "trainingtimes.box", true);
122 }
123 
124 // Tests line-level applyboxes on italic Times New Roman.
125 TEST_F(ApplyBoxTest, ItalLineLevel) {
126  VerifyBoxesAndText("trainingitalline.tif", kTruthTextLine, "trainingital.box",
127  true);
128 }
129 
130 } // namespace
file::JoinPath
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:43
string
std::string string
Definition: equationdetect_test.cc:21
boxread.h
OpenBoxFile
FILE * OpenBoxFile(const STRING &fname)
Definition: boxread.cpp:54
ICOORD
integer coordinate
Definition: points.h:30
STRING
Definition: strngs.h:45
tesseract::PSM_SINGLE_BLOCK
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:168
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
rect.h
ReadNextBox
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
Definition: boxread.cpp:134
resultiterator.h
tesseract::RIL_SYMBOL
Definition: publictypes.h:221
baseapi.h
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::TessBaseAPI
Definition: baseapi.h:98
tesseract::ResultIterator
Definition: resultiterator.h:44
tesseract::OEM_TESSERACT_ONLY
Definition: publictypes.h:266
TBOX
Definition: rect.h:33