tesseract  5.0.0
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 <allheaders.h>
23 #include <tesseract/baseapi.h>
24 #include <iostream>
25 #include <memory> // std::unique_ptr
26 #include <string>
27 #include "include_gunit.h"
28 #include "image.h"
29 
30 namespace tesseract {
31 
32 class TestClass : public testing::Test {
33 protected:
34 };
35 
36 #ifndef DISABLED_LEGACY_ENGINE
37 static void OSDTester(int expected_deg, const char *imgname, const char *tessdatadir) {
38  // log.info() << tessdatadir << " for image: " << imgname << std::endl;
39  auto api = std::make_unique<tesseract::TessBaseAPI>();
40  ASSERT_FALSE(api->Init(tessdatadir, "osd")) << "Could not initialize tesseract.";
41  Image 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 =
49  api->DetectOrientationScript(&orient_deg, &orient_conf, &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  image.destroy();
58 }
59 #endif
60 
61 class OSDTest : public TestClass,
62  public ::testing::WithParamInterface<std::tuple<int, const char *, const char *>> {
63 };
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()), std::get<2>(GetParam()));
71 #endif
72 }
73 
74 INSTANTIATE_TEST_SUITE_P(TessdataEngEuroHebrew, OSDTest,
75  ::testing::Combine(::testing::Values(0),
76  ::testing::Values(TESTING_DIR "/phototest.tif",
77  TESTING_DIR "/eurotext.tif",
78  TESTING_DIR "/hebrew.png"),
79  ::testing::Values(TESSDATA_DIR)));
80 
81 INSTANTIATE_TEST_SUITE_P(TessdataBestEngEuroHebrew, OSDTest,
82  ::testing::Combine(::testing::Values(0),
83  ::testing::Values(TESTING_DIR "/phototest.tif",
84  TESTING_DIR "/eurotext.tif",
85  TESTING_DIR "/hebrew.png"),
86  ::testing::Values(TESSDATA_DIR "_best")));
87 
88 INSTANTIATE_TEST_SUITE_P(TessdataFastEngEuroHebrew, OSDTest,
89  ::testing::Combine(::testing::Values(0),
90  ::testing::Values(TESTING_DIR "/phototest.tif",
91  TESTING_DIR "/eurotext.tif",
92  TESTING_DIR "/hebrew.png"),
93  ::testing::Values(TESSDATA_DIR "_fast")));
94 
95 INSTANTIATE_TEST_SUITE_P(TessdataFastRotated90, OSDTest,
96  ::testing::Combine(::testing::Values(90),
97  ::testing::Values(TESTING_DIR
98  "/phototest-rotated-R.png"),
99  ::testing::Values(TESSDATA_DIR "_fast")));
100 
101 INSTANTIATE_TEST_SUITE_P(TessdataFastRotated180, OSDTest,
102  ::testing::Combine(::testing::Values(180),
103  ::testing::Values(TESTING_DIR
104  "/phototest-rotated-180.png"),
105  ::testing::Values(TESSDATA_DIR "_fast")));
106 
107 INSTANTIATE_TEST_SUITE_P(TessdataFastRotated270, OSDTest,
108  ::testing::Combine(::testing::Values(270),
109  ::testing::Values(TESTING_DIR
110  "/phototest-rotated-L.png"),
111  ::testing::Values(TESSDATA_DIR "_fast")));
112 
113 INSTANTIATE_TEST_SUITE_P(TessdataFastDevaRotated270, OSDTest,
114  ::testing::Combine(::testing::Values(270),
115  ::testing::Values(TESTING_DIR
116  "/devatest-rotated-270.png"),
117  ::testing::Values(TESSDATA_DIR "_fast")));
118 
120  ::testing::Combine(::testing::Values(0),
121  ::testing::Values(TESTING_DIR "/devatest.png"),
122  ::testing::Values(TESSDATA_DIR "_fast")));
123 
124 } // namespace tesseract
INSTANTIATE_TEST_SUITE_P(Eng, MatchGroundTruth, ::testing::Values("eng"))
TEST_P(MatchGroundTruth, FastPhototestOCR)
void destroy()
Definition: image.cpp:32