tesseract  5.0.0
tesseract::ResultIteratorTest Class Reference
Inheritance diagram for tesseract::ResultIteratorTest:

Protected Member Functions

std::string TestDataNameToPath (const std::string &name)
 
std::string TessdataPath ()
 
std::string OutputNameToPath (const std::string &name)
 
 ResultIteratorTest ()
 
 ~ResultIteratorTest () override=default
 
void SetImage (const char *filename)
 
void VerifyRebuild (int max_diff, PageIteratorLevel level, PageIterator *it)
 
void VerifyIteratorText (const std::string &truth, PageIteratorLevel level, ResultIterator *it)
 
void VerifyRebuilds (int block_limit, int para_limit, int line_limit, int word_limit, int symbol_limit, PageIterator *it, PageIteratorLevel maxlevel=tesseract::RIL_SYMBOL)
 
void VerifyAllText (const std::string &truth, ResultIterator *it)
 
void ExpectTextlineReadingOrder (bool in_ltr_context, const StrongScriptDirection *word_dirs, int num_words, int *expected_reading_order, int num_reading_order_entries) const
 
void VerifySaneTextlineOrder (bool in_ltr_context, const StrongScriptDirection *word_dirs, int num_words) const
 

Protected Attributes

Image src_pix_
 
std::string ocr_text_
 
tesseract::TessBaseAPI api_
 

Detailed Description

Definition at line 17 of file resultiterator_test.cc.

Constructor & Destructor Documentation

◆ ResultIteratorTest()

tesseract::ResultIteratorTest::ResultIteratorTest ( )
inlineprotected

Definition at line 30 of file resultiterator_test.cc.

30  {
31  src_pix_ = nullptr;
32  }

◆ ~ResultIteratorTest()

tesseract::ResultIteratorTest::~ResultIteratorTest ( )
overrideprotecteddefault

Member Function Documentation

◆ ExpectTextlineReadingOrder()

void tesseract::ResultIteratorTest::ExpectTextlineReadingOrder ( bool  in_ltr_context,
const StrongScriptDirection word_dirs,
int  num_words,
int *  expected_reading_order,
int  num_reading_order_entries 
) const
inlineprotected

Definition at line 156 of file resultiterator_test.cc.

158  {
159  std::vector<StrongScriptDirection> gv_word_dirs;
160  for (int i = 0; i < num_words; i++) {
161  gv_word_dirs.push_back(word_dirs[i]);
162  }
163 
164  std::vector<int> calculated_order;
165  ResultIterator::CalculateTextlineOrder(in_ltr_context, gv_word_dirs, &calculated_order);
166  // STL vector can be used with EXPECT_EQ, so convert...
167  std::vector<int> correct_order(expected_reading_order,
168  expected_reading_order + num_reading_order_entries);
169  EXPECT_EQ(correct_order, calculated_order);
170  }
static void CalculateTextlineOrder(bool paragraph_is_ltr, const std::vector< StrongScriptDirection > &word_dirs, std::vector< int > *reading_order)

◆ OutputNameToPath()

std::string tesseract::ResultIteratorTest::OutputNameToPath ( const std::string &  name)
inlineprotected

Definition at line 25 of file resultiterator_test.cc.

25  {
27  return file::JoinPath(FLAGS_test_tmpdir, name);
28  }
static void MakeTmpdir()
Definition: include_gunit.h:38
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:65

◆ SetImage()

void tesseract::ResultIteratorTest::SetImage ( const char *  filename)
inlineprotected

Definition at line 35 of file resultiterator_test.cc.

35  {
36  src_pix_ = pixRead(TestDataNameToPath(filename).c_str());
38  // if (!FLAGS_tess_config.empty())
39  // api_.ReadConfigFile(FLAGS_tess_config.c_str());
42  src_pix_.destroy();
44  }
@ OEM_TESSERACT_ONLY
Definition: publictypes.h:266
@ PSM_AUTO
Fully automatic page segmentation, but no OSD.
Definition: publictypes.h:164
void SetPageSegMode(PageSegMode mode)
Definition: baseapi.cpp:508
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:365
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:573
void destroy()
Definition: image.cpp:32
std::string TestDataNameToPath(const std::string &name)

◆ TessdataPath()

std::string tesseract::ResultIteratorTest::TessdataPath ( )
inlineprotected

Definition at line 22 of file resultiterator_test.cc.

22  {
23  return file::JoinPath(TESSDATA_DIR, "");
24  }

◆ TestDataNameToPath()

std::string tesseract::ResultIteratorTest::TestDataNameToPath ( const std::string &  name)
inlineprotected

Definition at line 19 of file resultiterator_test.cc.

19  {
20  return file::JoinPath(TESTING_DIR, name);
21  }

◆ VerifyAllText()

void tesseract::ResultIteratorTest::VerifyAllText ( const std::string &  truth,
ResultIterator it 
)
inlineprotected

Definition at line 143 of file resultiterator_test.cc.

143  {
149  }
void VerifyIteratorText(const std::string &truth, PageIteratorLevel level, ResultIterator *it)

◆ VerifyIteratorText()

void tesseract::ResultIteratorTest::VerifyIteratorText ( const std::string &  truth,
PageIteratorLevel  level,
ResultIterator it 
)
inlineprotected

Definition at line 108 of file resultiterator_test.cc.

108  {
109  LOG(INFO) << "Text Test Level " << level << "\n";
110  it->Begin();
111  std::string result;
112  do {
113  char *text = it->GetUTF8Text(level);
114  result += text;
115  delete[] text;
116  if ((level == tesseract::RIL_WORD || level == tesseract::RIL_SYMBOL) &&
117  it->IsAtFinalElement(tesseract::RIL_WORD, level)) {
118  if (it->IsAtFinalElement(tesseract::RIL_TEXTLINE, level)) {
119  result += '\n';
120  } else {
121  result += ' ';
122  }
123  if (it->IsAtFinalElement(tesseract::RIL_PARA, level) &&
124  !(it->IsAtFinalElement(tesseract::RIL_BLOCK, level))) {
125  result += '\n';
126  }
127  }
128  } while (it->Next(level));
129  EXPECT_STREQ(truth.c_str(), result.c_str()) << "Rebuild failed at Text Level " << level;
130  }
@ LOG
@ INFO
Definition: log.h:28

◆ VerifyRebuild()

void tesseract::ResultIteratorTest::VerifyRebuild ( int  max_diff,
PageIteratorLevel  level,
PageIterator it 
)
inlineprotected

Definition at line 49 of file resultiterator_test.cc.

49  {
50  it->Begin();
51  int width = pixGetWidth(src_pix_);
52  int height = pixGetHeight(src_pix_);
53  int depth = pixGetDepth(src_pix_);
54  Image pix = pixCreate(width, height, depth);
55  EXPECT_TRUE(depth == 1 || depth == 8);
56  if (depth == 8) {
57  pixSetAll(pix);
58  }
59  do {
60  int left, top, right, bottom;
61  PageIteratorLevel im_level = level;
62  // If the return is false, it is a non-text block so get the block image.
63  if (!it->BoundingBox(level, &left, &top, &right, &bottom)) {
64  im_level = tesseract::RIL_BLOCK;
65  EXPECT_TRUE(it->BoundingBox(im_level, &left, &top, &right, &bottom));
66  }
67  LOG(INFO) << "BBox: [L:" << left << ", T:" << top << ", R:" << right << ", B:" << bottom
68  << "]"
69  << "\n";
70  Image block_pix;
71  if (depth == 1) {
72  block_pix = it->GetBinaryImage(im_level);
73  pixRasterop(pix, left, top, right - left, bottom - top, PIX_SRC ^ PIX_DST, block_pix, 0, 0);
74  } else {
75  block_pix = it->GetImage(im_level, 2, src_pix_, &left, &top);
76  pixRasterop(pix, left, top, pixGetWidth(block_pix), pixGetHeight(block_pix),
77  PIX_SRC & PIX_DST, block_pix, 0, 0);
78  }
79  CHECK(block_pix != nullptr);
80  block_pix.destroy();
81  } while (it->Next(level));
82  // if (base::GetFlag(FLAGS_v) >= 1)
83  // pixWrite(OutputNameToPath("rebuilt.png").c_str(), pix, IFF_PNG);
84  pixRasterop(pix, 0, 0, width, height, PIX_SRC ^ PIX_DST, src_pix_, 0, 0);
85  if (depth == 8) {
86  Image binary_pix = pixThresholdToBinary(pix, 128);
87  pix.destroy();
88  pixInvert(binary_pix, binary_pix);
89  pix = binary_pix;
90  }
91  // if (base::GetFlag(FLAGS_v) >= 1)
92  // pixWrite(OutputNameToPath("rebuiltxor.png").c_str(), pix, IFF_PNG);
93  l_int32 pixcount;
94  pixCountPixels(pix, &pixcount, nullptr);
95  if (pixcount > max_diff) {
96  std::string outfile = OutputNameToPath("failedxor.png");
97  LOG(INFO) << "outfile = " << outfile << "\n";
98  pixWrite(outfile.c_str(), pix, IFF_PNG);
99  }
100  pix.destroy();
101  LOG(INFO) << "At level " << level << ": pix diff = " << pixcount << "\n";
102  EXPECT_LE(pixcount, max_diff);
103  // if (base::GetFlag(FLAGS_v) > 1) CHECK_LE(pixcount, max_diff);
104  }
#define CHECK(condition)
Definition: include_gunit.h:76
std::string OutputNameToPath(const std::string &name)

◆ VerifyRebuilds()

void tesseract::ResultIteratorTest::VerifyRebuilds ( int  block_limit,
int  para_limit,
int  line_limit,
int  word_limit,
int  symbol_limit,
PageIterator it,
PageIteratorLevel  maxlevel = tesseract::RIL_SYMBOL 
)
inlineprotected

Definition at line 132 of file resultiterator_test.cc.

133  {
134  VerifyRebuild(block_limit, tesseract::RIL_BLOCK, it);
135  VerifyRebuild(para_limit, tesseract::RIL_PARA, it);
136  VerifyRebuild(line_limit, tesseract::RIL_TEXTLINE, it);
137  VerifyRebuild(word_limit, tesseract::RIL_WORD, it);
138  if (maxlevel == tesseract::RIL_SYMBOL) {
139  VerifyRebuild(symbol_limit, maxlevel, it);
140  }
141  }
void VerifyRebuild(int max_diff, PageIteratorLevel level, PageIterator *it)

◆ VerifySaneTextlineOrder()

void tesseract::ResultIteratorTest::VerifySaneTextlineOrder ( bool  in_ltr_context,
const StrongScriptDirection word_dirs,
int  num_words 
) const
inlineprotected

Definition at line 176 of file resultiterator_test.cc.

177  {
178  std::vector<StrongScriptDirection> gv_word_dirs;
179  for (int i = 0; i < num_words; i++) {
180  gv_word_dirs.push_back(word_dirs[i]);
181  }
182 
183  std::vector<int> output;
184  ResultIterator::CalculateTextlineOrder(in_ltr_context, gv_word_dirs, &output);
185  ASSERT_GE(output.size(), num_words);
186  std::vector<int> output_copy(output);
187  std::sort(output_copy.begin(), output_copy.end());
188  bool sane = true;
189  unsigned j = 0;
190  while (j < output_copy.size() && output_copy[j] < 0) {
191  j++;
192  }
193  for (int i = 0; i < num_words; i++, j++) {
194  if (output_copy[j] != i) {
195  sane = false;
196  break;
197  }
198  }
199  if (j != output_copy.size()) {
200  sane = false;
201  }
202  if (!sane) {
203  std::vector<int> empty;
204  EXPECT_EQ(output, empty) << " permutation of 0.." << num_words - 1 << " not found in "
205  << (in_ltr_context ? "ltr" : "rtl") << " context.";
206  }
207  }

Member Data Documentation

◆ api_

tesseract::TessBaseAPI tesseract::ResultIteratorTest::api_
protected

Definition at line 212 of file resultiterator_test.cc.

◆ ocr_text_

std::string tesseract::ResultIteratorTest::ocr_text_
protected

Definition at line 211 of file resultiterator_test.cc.

◆ src_pix_

Image tesseract::ResultIteratorTest::src_pix_
protected

Definition at line 210 of file resultiterator_test.cc.


The documentation for this class was generated from the following file: