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