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