layout_test.cc 8.5 KB

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