tesseract  5.0.0-alpha-619-ge9db
osd_test.cc
Go to the documentation of this file.
1 // File: osd_test.cc
3 // Description: OSD Tests for Tesseract.
4 // Author: ShreeDevi Kumar
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 
17 // based on https://gist.github.com/amitdo/7c7a522004dd79b398340c9595b377e1
18 
19 // expects clones of tessdata, tessdata_fast and tessdata_best repos
20 
21 //#include "log.h"
22 #include <iostream>
23 #include <memory> // std::unique_ptr
24 #include <string>
25 #include <tesseract/baseapi.h>
26 #include "include_gunit.h"
27 #include "leptonica/allheaders.h"
28 
29 namespace {
30 
31 class TestClass : public testing::Test {
32  protected:
33 };
34 
35 #ifndef DISABLED_LEGACY_ENGINE
36 static void OSDTester(int expected_deg, const char* imgname, const char* tessdatadir) {
37  // log.info() << tessdatadir << " for image: " << imgname << std::endl;
38  std::unique_ptr<tesseract::TessBaseAPI> api(new tesseract::TessBaseAPI());
39  ASSERT_FALSE(api->Init(tessdatadir, "osd"))
40  << "Could not initialize tesseract.";
41  Pix* image = pixRead(imgname);
42  ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
43  api->SetImage(image);
44  int orient_deg;
45  float orient_conf;
46  const char* script_name;
47  float script_conf;
48  bool detected = api->DetectOrientationScript(&orient_deg, &orient_conf,
49  &script_name, &script_conf);
50  ASSERT_FALSE(!detected) << "Failed to detect OSD.";
51  printf(
52  "************ Orientation in degrees: %d, Orientation confidence: %.2f\n"
53  " Script: %s, Script confidence: %.2f\n",
54  orient_deg, orient_conf, script_name, script_conf);
55  EXPECT_EQ(expected_deg, orient_deg);
56  api->End();
57  pixDestroy(&image);
58 }
59 #endif
60 
61 class OSDTest : public TestClass,
62  public ::testing::WithParamInterface<
63  std::tuple<int, const char*, const char*>> {};
64 
65 TEST_P(OSDTest, MatchOrientationDegrees) {
66 #ifdef DISABLED_LEGACY_ENGINE
67  // Skip test because TessBaseAPI::DetectOrientationScript is missing.
68  GTEST_SKIP();
69 #else
70  OSDTester(std::get<0>(GetParam()), std::get<1>(GetParam()),
71  std::get<2>(GetParam()));
72 #endif
73 }
74 
75 INSTANTIATE_TEST_CASE_P(
76  TessdataEngEuroHebrew, OSDTest,
77  ::testing::Combine(::testing::Values(0),
78  ::testing::Values(TESTING_DIR "/phototest.tif",
79  TESTING_DIR "/eurotext.tif",
80  TESTING_DIR "/hebrew.png"),
81  ::testing::Values(TESSDATA_DIR)));
82 
83 INSTANTIATE_TEST_CASE_P(
84  TessdataBestEngEuroHebrew, OSDTest,
85  ::testing::Combine(::testing::Values(0),
86  ::testing::Values(TESTING_DIR "/phototest.tif",
87  TESTING_DIR "/eurotext.tif",
88  TESTING_DIR "/hebrew.png"),
89  ::testing::Values(TESSDATA_DIR "_best")));
90 
91 INSTANTIATE_TEST_CASE_P(
92  TessdataFastEngEuroHebrew, OSDTest,
93  ::testing::Combine(::testing::Values(0),
94  ::testing::Values(TESTING_DIR "/phototest.tif",
95  TESTING_DIR "/eurotext.tif",
96  TESTING_DIR "/hebrew.png"),
97  ::testing::Values(TESSDATA_DIR "_fast")));
98 
99 INSTANTIATE_TEST_CASE_P(
100  TessdataFastRotated90, OSDTest,
101  ::testing::Combine(::testing::Values(90),
102  ::testing::Values(TESTING_DIR
103  "/phototest-rotated-R.png"),
104  ::testing::Values(TESSDATA_DIR "_fast")));
105 
106 INSTANTIATE_TEST_CASE_P(
107  TessdataFastRotated180, OSDTest,
108  ::testing::Combine(::testing::Values(180),
109  ::testing::Values(TESTING_DIR
110  "/phototest-rotated-180.png"),
111  ::testing::Values(TESSDATA_DIR "_fast")));
112 
113 INSTANTIATE_TEST_CASE_P(
114  TessdataFastRotated270, OSDTest,
115  ::testing::Combine(::testing::Values(270),
116  ::testing::Values(TESTING_DIR
117  "/phototest-rotated-L.png"),
118  ::testing::Values(TESSDATA_DIR "_fast")));
119 
120 INSTANTIATE_TEST_CASE_P(
121  TessdataFastDevaRotated270, OSDTest,
122  ::testing::Combine(::testing::Values(270),
123  ::testing::Values(TESTING_DIR
124  "/devatest-rotated-270.png"),
125  ::testing::Values(TESSDATA_DIR "_fast")));
126 
127 INSTANTIATE_TEST_CASE_P(
128  TessdataFastDeva, OSDTest,
129  ::testing::Combine(::testing::Values(0),
130  ::testing::Values(TESTING_DIR "/devatest.png"),
131  ::testing::Values(TESSDATA_DIR "_fast")));
132 
133 } // namespace
include_gunit.h
baseapi.h
tesseract::TessBaseAPI
Definition: baseapi.h:98