tesseract v5.3.3.20231005
layout_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 <utility>
14
15#include "include_gunit.h"
16
17#include <allheaders.h>
18#include <tesseract/baseapi.h>
20#include "coutln.h"
21#include "log.h" // for LOG
22#include "mutableiterator.h"
23#include "ocrblock.h" // for class BLOCK
24#include "pageres.h"
25#include "polyblk.h"
26#include "stepblob.h"
27
28namespace tesseract {
29
31static const char *kPolyBlockNames[] = {
32 "Unknown",
33 "Flowing Text",
34 "Heading Text",
35 "Pullout Text",
36 "Equation",
37 "Inline Equation",
38 "Table",
39 "Vertical Text",
40 "Caption Text",
41 "Flowing Image",
42 "Heading Image",
43 "Pullout Image",
44 "Horizontal Line",
45 "Vertical Line",
46 "Noise",
47 "" // End marker for testing that sizes match.
48};
49
50const char *kStrings8087_054[] = {"dat", "Dalmatian", "", "DAMAGED DURING", "margarine,", nullptr};
53
54// The fixture for testing Tesseract.
55class LayoutTest : public testing::Test {
56protected:
57 std::string TestDataNameToPath(const std::string &name) {
58 return file::JoinPath(TESTING_DIR, "/" + name);
59 }
60 std::string TessdataPath() {
61 return file::JoinPath(TESSDATA_DIR, "");
62 }
63
65 src_pix_ = nullptr;
66 }
67 ~LayoutTest() override {
69 }
70
71 void SetImage(const char *filename, const char *lang) {
73 src_pix_ = pixRead(TestDataNameToPath(filename).c_str());
77 }
78
79 // Tests reading order and block finding (very roughly) by iterating
80 // over the blocks, expecting that they contain the strings in order,
81 // allowing for other blocks in between.
82 // An empty string should match an image block, and a nullptr string
83 // indicates the end of the array.
84 void VerifyBlockTextOrder(const char *strings[], const PolyBlockType *blocks,
85 ResultIterator *it) {
86 it->Begin();
87 int string_index = 0;
88 int block_index = 0;
89 do {
90 char *block_text = it->GetUTF8Text(tesseract::RIL_BLOCK);
91 if (block_text != nullptr && it->BlockType() == blocks[string_index] &&
92 strstr(block_text, strings[string_index]) != nullptr) {
93 LOG(INFO) << "Found string " << strings[string_index] << " in block " << block_index
94 << " of type " << kPolyBlockNames[blocks[string_index]] << "\n";
95 // Found this one.
96 ++string_index;
97 } else if (it->BlockType() == blocks[string_index] && block_text == nullptr &&
98 strings[string_index][0] == '\0') {
99 LOG(INFO) << "Found block of type " << kPolyBlockNames[blocks[string_index]] << " at block "
100 << block_index << "\n";
101 // Found this one.
102 ++string_index;
103 } else {
104 LOG(INFO) << "No match found in block with text:\n" << block_text;
105 }
106 delete[] block_text;
107 ++block_index;
108 if (strings[string_index] == nullptr) {
109 break;
110 }
111 } while (it->Next(tesseract::RIL_BLOCK));
112 EXPECT_TRUE(strings[string_index] == nullptr);
113 }
114
115 // Tests that approximate order of the biggest text blocks is correct.
116 // Correctness is tested by the following simple rules:
117 // If a block overlaps its predecessor in x, then it must be below it.
118 // otherwise, if the block is not below its predecessor, then it must
119 // be to the left of it if right_to_left is true, or to the right otherwise.
120 void VerifyRoughBlockOrder(bool right_to_left, ResultIterator *it) {
121 int prev_left = 0;
122 int prev_right = 0;
123 int prev_bottom = 0;
124 it->Begin();
125 do {
126 int left, top, right, bottom;
127 if (it->BoundingBox(tesseract::RIL_BLOCK, &left, &top, &right, &bottom) &&
128 PTIsTextType(it->BlockType()) && right - left > 800 && bottom - top > 200) {
129 if (prev_right > prev_left) {
130 if (std::min(right, prev_right) > std::max(left, prev_left)) {
131 EXPECT_GE(top, prev_bottom) << "Overlapping block should be below";
132 } else if (top < prev_bottom) {
133 if (right_to_left) {
134 EXPECT_GE(prev_left, right) << "Block should be to the left";
135 } else {
136 EXPECT_GE(left, prev_right) << "Block should be to the right";
137 }
138 }
139 }
140 prev_left = left;
141 prev_right = right;
142 prev_bottom = bottom;
143 }
144 } while (it->Next(tesseract::RIL_BLOCK));
145 }
146
147 // Tests that every blob assigned to the biggest text blocks is contained
148 // fully within its block by testing that the block polygon winds around
149 // the center of the bounding boxes of the outlines in the blob.
150 void VerifyTotalContainment(int winding_target, MutableIterator *it) {
151 it->Begin();
152 do {
153 int left, top, right, bottom;
154 if (it->BoundingBox(tesseract::RIL_BLOCK, &left, &top, &right, &bottom) &&
155 PTIsTextType(it->BlockType()) && right - left > 800 && bottom - top > 200) {
156 const PAGE_RES_IT *pr_it = it->PageResIt();
157 POLY_BLOCK *pb = pr_it->block()->block->pdblk.poly_block();
158 CHECK(pb != nullptr);
159 FCOORD skew = pr_it->block()->block->skew();
160 EXPECT_GT(skew.x(), 0.0f);
161 EXPECT_GT(skew.y(), 0.0f);
162 // Iterate the words in the block.
163 MutableIterator word_it = *it;
164 do {
165 const PAGE_RES_IT *w_it = word_it.PageResIt();
166 // Iterate the blobs in the word.
167 C_BLOB_IT b_it(w_it->word()->word->cblob_list());
168 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
169 C_BLOB *blob = b_it.data();
170 // Iterate the outlines in the blob.
171 C_OUTLINE_IT ol_it(blob->out_list());
172 for (ol_it.mark_cycle_pt(); !ol_it.cycled_list(); ol_it.forward()) {
173 C_OUTLINE *ol = ol_it.data();
174 TBOX box = ol->bounding_box();
175 ICOORD middle((box.left() + box.right()) / 2, (box.top() + box.bottom()) / 2);
176 EXPECT_EQ(winding_target, pb->winding_number(middle));
177 }
178 }
179 } while (word_it.Next(tesseract::RIL_WORD) &&
181 }
182 } while (it->Next(tesseract::RIL_BLOCK));
183 }
184
186 std::string ocr_text_;
188};
189
190// Tests that array sizes match their intended size.
191TEST_F(LayoutTest, ArraySizeTest) {
192 int size = 0;
193 for (size = 0; kPolyBlockNames[size][0] != '\0'; ++size) {
194 ;
195 }
196 EXPECT_EQ(size, PT_COUNT);
197}
198
199// Tests that Tesseract gets the important blocks and in the right order
200// on a UNLV page numbered 8087_054.3B.tif. (Dubrovnik)
201TEST_F(LayoutTest, UNLV8087_054) {
202 SetImage("8087_054.3B.tif", "eng");
203 // Just run recognition.
204 EXPECT_EQ(api_.Recognize(nullptr), 0);
205 // Check iterator position.
206 tesseract::ResultIterator *it = api_.GetIterator();
207 VerifyBlockTextOrder(kStrings8087_054, kBlocks8087_054, it);
208 delete it;
209}
210
211// Tests that Tesseract gets the important blocks and in the right order
212// on GOOGLE:13510798882202548:74:84.sj-79.tif (Hebrew image)
213// TODO: replace hebrew.png by Google image referred above
214TEST_F(LayoutTest, HebrewOrderingAndSkew) {
215 SetImage("hebrew.png", "eng");
216 // Just run recognition.
217 EXPECT_EQ(api_.Recognize(nullptr), 0);
218 tesseract::MutableIterator *it = api_.GetMutableIterator();
219 // In eng mode, block order should not be RTL.
220 VerifyRoughBlockOrder(false, it);
221 VerifyTotalContainment(1, it);
222 delete it;
223 // Now try again using Hebrew.
224 SetImage("hebrew.png", "heb");
225 // Just run recognition.
226 EXPECT_EQ(api_.Recognize(nullptr), 0);
227 it = api_.GetMutableIterator();
228 // In heb mode, block order should be RTL.
229 VerifyRoughBlockOrder(true, it);
230 // And blobs should still be fully contained.
231 VerifyTotalContainment(-1, it);
232 delete it;
233}
234
235} // namespace tesseract
@ LOG
@ INFO
Definition: log.h:28
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define EXPECT_GT(val1, val2)
Definition: gtest.h:2053
#define EXPECT_GE(val1, val2)
Definition: gtest.h:2051
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define CHECK(condition)
Definition: include_gunit.h:76
@ OEM_TESSERACT_ONLY
Definition: publictypes.h:264
@ PSM_AUTO
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:162
const char * kStrings8087_054[]
Definition: layout_test.cc:50
const PolyBlockType kBlocks8087_054[]
Definition: layout_test.cc:51
bool PTIsTextType(PolyBlockType type)
Definition: publictypes.h:80
TEST_F(EuroText, FastLatinOCR)
@ PT_PULLOUT_IMAGE
Definition: publictypes.h:63
@ PT_CAPTION_TEXT
Definition: publictypes.h:60
@ PT_HEADING_TEXT
Definition: publictypes.h:54
@ PT_FLOWING_TEXT
Definition: publictypes.h:53
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:511
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:368
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:576
PolyBlockType BlockType() const
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
virtual char * GetUTF8Text(PageIteratorLevel level) const
bool IsAtBeginningOf(PageIteratorLevel level) const override
bool Next(PageIteratorLevel level) override
const PAGE_RES_IT * PageResIt() const
const TBOX & bounding_box() const
Definition: coutln.h:113
void destroy()
Definition: image.cpp:32
PDBLK pdblk
Page Description Block.
Definition: ocrblock.h:185
FCOORD skew() const
Definition: ocrblock.h:141
BLOCK_RES * block() const
Definition: pageres.h:769
WERD_RES * word() const
Definition: pageres.h:763
POLY_BLOCK * poly_block() const
Definition: pdblock.h:59
integer coordinate
Definition: points.h:36
float y() const
Definition: points.h:209
float x() const
Definition: points.h:206
int16_t winding_number(const ICOORD &test_pt)
Definition: polyblk.cpp:106
TDimension left() const
Definition: rect.h:82
TDimension top() const
Definition: rect.h:68
TDimension right() const
Definition: rect.h:89
TDimension bottom() const
Definition: rect.h:75
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:70
C_BLOB_LIST * cblob_list()
Definition: werd.h:96
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:65
tesseract::TessBaseAPI api_
Definition: layout_test.cc:187
void SetImage(const char *filename, const char *lang)
Definition: layout_test.cc:71
void VerifyRoughBlockOrder(bool right_to_left, ResultIterator *it)
Definition: layout_test.cc:120
void VerifyBlockTextOrder(const char *strings[], const PolyBlockType *blocks, ResultIterator *it)
Definition: layout_test.cc:84
~LayoutTest() override
Definition: layout_test.cc:67
std::string TestDataNameToPath(const std::string &name)
Definition: layout_test.cc:57
std::string TessdataPath()
Definition: layout_test.cc:60
void VerifyTotalContainment(int winding_target, MutableIterator *it)
Definition: layout_test.cc:150