tesseract  5.0.0-alpha-619-ge9db
pagesegmode_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 #if defined(_WIN32)
13 #include <io.h> // for _access
14 #else
15 #include <unistd.h> // for access
16 #endif
17 #include <string>
18 #include "allheaders.h"
19 #include <tesseract/baseapi.h>
20 #include <tesseract/helpers.h>
21 #include "log.h"
22 #include "include_gunit.h"
23 
24 namespace {
25 
26 // Replacement for std::filesystem::exists (C++-17)
27 static bool file_exists(const char* filename) {
28 #if defined(_WIN32)
29  return _access(filename, 0) == 0;
30 #else
31  return access(filename, 0) == 0;
32 #endif
33 }
34 
35 // The fixture for testing Tesseract.
36 class PageSegModeTest : public testing::Test {
37  protected:
38  PageSegModeTest() = default;
39  ~PageSegModeTest() {
40  pixDestroy(&src_pix_);
41  }
42 
43  void SetUp() override {
44  static std::locale system_locale("");
45  std::locale::global(system_locale);
46  }
47 
48  void SetImage(const char* filename) {
49  pixDestroy(&src_pix_);
50  src_pix_ = pixRead(filename);
51  api_.Init(TESSDATA_DIR, "eng", tesseract::OEM_TESSERACT_ONLY);
52  api_.SetImage(src_pix_);
53  }
54 
55  // Tests that the given rectangle produces exactly the given text in the
56  // given segmentation mode (after chopping off the last 2 newlines.)
57  void VerifyRectText(tesseract::PageSegMode mode, const char* str,
58  int left, int top, int width, int height) {
59  api_.SetPageSegMode(mode);
60  api_.SetRectangle(left, top, width, height);
61  char* result = api_.GetUTF8Text();
62  chomp_string(result);
63  chomp_string(result);
64  EXPECT_STREQ(str, result);
65  delete[] result;
66  }
67 
68  // Tests that the given rectangle does NOT produce the given text in the
69  // given segmentation mode.
70  void NotRectText(tesseract::PageSegMode mode, const char* str,
71  int left, int top, int width, int height) {
72  api_.SetPageSegMode(mode);
73  api_.SetRectangle(left, top, width, height);
74  char* result = api_.GetUTF8Text();
75  EXPECT_STRNE(str, result);
76  delete[] result;
77  }
78 
79  Pix* src_pix_ = nullptr;
80  std::string ocr_text_;
82 };
83 
84 // Tests the single-word segmentation mode, and that it performs correctly
85 // and differently to line and block mode.
86 TEST_F(PageSegModeTest, WordTest) {
87  std::string filename = file::JoinPath(TESTING_DIR, "segmodeimg.tif");
88  if (!file_exists(filename.c_str())) {
89  LOG(INFO) << "Skip test because of missing " << filename << '\n';
90  GTEST_SKIP();
91  } else {
92  SetImage(filename.c_str());
93  // Test various rectangles around the inverse page number.
94  VerifyRectText(tesseract::PSM_SINGLE_WORD, "183", 1419, 264, 69, 34);
95  VerifyRectText(tesseract::PSM_SINGLE_WORD, "183", 1411, 252, 78, 62);
96  VerifyRectText(tesseract::PSM_SINGLE_WORD, "183", 1396, 218, 114, 102);
97  // Test a random pair of words as a line
98  VerifyRectText(tesseract::PSM_SINGLE_LINE,
99  "What should", 237, 393, 256, 36);
100  // Test a random pair of words as a word
101  VerifyRectText(tesseract::PSM_SINGLE_WORD,
102  "Whatshould", 237, 393, 256, 36);
103  // Test single block mode.
104  VerifyRectText(tesseract::PSM_SINGLE_BLOCK,
105  "both the\nfrom the", 237, 450, 172, 94);
106  // But doesn't work in line or word mode.
107  NotRectText(tesseract::PSM_SINGLE_LINE,
108  "both the\nfrom the", 237, 450, 172, 94);
109  NotRectText(tesseract::PSM_SINGLE_WORD,
110  "both the\nfrom the", 237, 450, 172, 94);
111  }
112 }
113 
114 } // 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
INFO
Definition: log.h:29
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
tesseract::PSM_SINGLE_LINE
Treat the image as a single text line.
Definition: publictypes.h:169
tesseract::PSM_SINGLE_WORD
Treat the image as a single word.
Definition: publictypes.h:170
baseapi.h
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
tesseract::TessBaseAPI
Definition: baseapi.h:98
helpers.h
tesseract::PageSegMode
PageSegMode
Definition: publictypes.h:159
log.h
LOG
Definition: cleanapi_test.cc:19
tesseract::OEM_TESSERACT_ONLY
Definition: publictypes.h:266