|
2 | 2 | // File: apiexample_test.cc
|
3 | 3 | // Description: Api Test for Tesseract using text fixtures and parameters.
|
4 | 4 | // Tests for Devanagari, Latin and Arabic scripts are disabled by default.
|
5 |
| -// Disabled tests can be run when required by using the --gtest_also_run_disabled_tests argument. |
| 5 | +// Disabled tests can be run when required by using the |
| 6 | +// --gtest_also_run_disabled_tests argument. |
6 | 7 | // ./unittest/apiexample_test --gtest_also_run_disabled_tests
|
7 | 8 | //
|
8 | 9 | // Author: ShreeDevi Kumar
|
|
21 | 22 | // expects clone of tessdata_fast repo in ../../tessdata_fast
|
22 | 23 |
|
23 | 24 | //#include "log.h"
|
24 |
| -#include "include_gunit.h" |
25 |
| -#include "baseapi.h" |
26 |
| -#include "leptonica/allheaders.h" |
27 |
| -#include <iostream> |
28 |
| -#include <string> |
29 |
| -#include <fstream> |
30 |
| -#include <locale> |
31 | 25 | #include <limits.h>
|
32 | 26 | #include <time.h>
|
| 27 | +#include <fstream> |
| 28 | +#include <iostream> |
| 29 | +#include <locale> |
| 30 | +#include <string> |
| 31 | +#include "baseapi.h" |
| 32 | +#include "include_gunit.h" |
| 33 | +#include "leptonica/allheaders.h" |
33 | 34 |
|
34 | 35 | namespace {
|
35 | 36 |
|
36 | 37 | class QuickTest : public testing::Test {
|
37 | 38 | protected:
|
38 |
| - virtual void SetUp() { |
39 |
| - start_time_ = time(nullptr); |
40 |
| - } |
| 39 | + virtual void SetUp() { start_time_ = time(nullptr); } |
41 | 40 | virtual void TearDown() {
|
42 | 41 | const time_t end_time = time(nullptr);
|
43 |
| - EXPECT_TRUE(end_time - start_time_ <=55) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_); |
| 42 | + EXPECT_TRUE(end_time - start_time_ <= 55) |
| 43 | + << "The test took too long - " |
| 44 | + << ::testing::PrintToString(end_time - start_time_); |
44 | 45 | }
|
45 | 46 | time_t start_time_;
|
46 |
| - }; |
| 47 | +}; |
47 | 48 |
|
48 |
| - void OCRTester(const char* imgname, const char* groundtruth, const char* tessdatadir, const char* lang) { |
49 |
| - //log.info() << tessdatadir << " for language: " << lang << std::endl; |
50 |
| - char *outText; |
51 |
| - std::locale loc("C"); // You can also use "" for the default system locale |
52 |
| - std::ifstream file(groundtruth); |
53 |
| - file.imbue(loc); // Use it for file input |
54 |
| - std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
55 |
| - tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); |
56 |
| - ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract."; |
57 |
| - Pix *image = pixRead(imgname); |
58 |
| - ASSERT_TRUE(image != nullptr) << "Failed to read test image."; |
59 |
| - api->SetImage(image); |
60 |
| - outText = api->GetUTF8Text(); |
61 |
| - EXPECT_EQ(gtText,outText) << "Phototest.tif OCR does not match ground truth for " << ::testing::PrintToString(lang); |
62 |
| - api->End(); |
63 |
| - delete [] outText; |
64 |
| - pixDestroy(&image); |
65 |
| - } |
| 49 | +void OCRTester(const char* imgname, const char* groundtruth, |
| 50 | + const char* tessdatadir, const char* lang) { |
| 51 | + // log.info() << tessdatadir << " for language: " << lang << std::endl; |
| 52 | + char* outText; |
| 53 | + std::locale loc("C"); // You can also use "" for the default system locale |
| 54 | + std::ifstream file(groundtruth); |
| 55 | + file.imbue(loc); // Use it for file input |
| 56 | + std::string gtText((std::istreambuf_iterator<char>(file)), |
| 57 | + std::istreambuf_iterator<char>()); |
| 58 | + tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI(); |
| 59 | + ASSERT_FALSE(api->Init(tessdatadir, lang)) |
| 60 | + << "Could not initialize tesseract."; |
| 61 | + Pix* image = pixRead(imgname); |
| 62 | + ASSERT_TRUE(image != nullptr) << "Failed to read test image."; |
| 63 | + api->SetImage(image); |
| 64 | + outText = api->GetUTF8Text(); |
| 65 | + EXPECT_EQ(gtText, outText) |
| 66 | + << "Phototest.tif OCR does not match ground truth for " |
| 67 | + << ::testing::PrintToString(lang); |
| 68 | + api->End(); |
| 69 | + delete[] outText; |
| 70 | + pixDestroy(&image); |
| 71 | +} |
66 | 72 |
|
67 |
| - class MatchGroundTruth : public QuickTest , |
68 |
| - public ::testing::WithParamInterface<const char*> { |
69 |
| - }; |
| 73 | +class MatchGroundTruth : public QuickTest, |
| 74 | + public ::testing::WithParamInterface<const char*> {}; |
70 | 75 |
|
71 |
| - TEST_P(MatchGroundTruth, FastPhototestOCR) { |
72 |
| - OCRTester(TESTING_DIR "/phototest.tif", |
73 |
| - TESTING_DIR "/phototest.txt", |
74 |
| - TESSDATA_DIR "_fast", GetParam()); |
75 |
| - } |
| 76 | +TEST_P(MatchGroundTruth, FastPhototestOCR) { |
| 77 | + OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", |
| 78 | + TESSDATA_DIR "_fast", GetParam()); |
| 79 | +} |
76 | 80 |
|
77 |
| - TEST_P(MatchGroundTruth, BestPhototestOCR) { |
78 |
| - OCRTester(TESTING_DIR "/phototest.tif", |
79 |
| - TESTING_DIR "/phototest.txt", |
80 |
| - TESSDATA_DIR "_best", GetParam()); |
81 |
| - } |
| 81 | +TEST_P(MatchGroundTruth, BestPhototestOCR) { |
| 82 | + OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", |
| 83 | + TESSDATA_DIR "_best", GetParam()); |
| 84 | +} |
82 | 85 |
|
83 |
| - TEST_P(MatchGroundTruth, TessPhototestOCR) { |
84 |
| - OCRTester(TESTING_DIR "/phototest.tif", |
85 |
| - TESTING_DIR "/phototest.txt", |
86 |
| - TESSDATA_DIR , GetParam()); |
87 |
| - } |
| 86 | +TEST_P(MatchGroundTruth, TessPhototestOCR) { |
| 87 | + OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", |
| 88 | + TESSDATA_DIR, GetParam()); |
| 89 | +} |
88 | 90 |
|
89 |
| - INSTANTIATE_TEST_CASE_P( Eng, MatchGroundTruth, |
90 |
| - ::testing::Values("eng") ); |
91 |
| - INSTANTIATE_TEST_CASE_P( DISABLED_Latin, MatchGroundTruth, |
92 |
| - ::testing::Values("script/Latin") ); |
93 |
| - INSTANTIATE_TEST_CASE_P( DISABLED_Deva, MatchGroundTruth, |
94 |
| - ::testing::Values("script/Devanagari") ); |
95 |
| - INSTANTIATE_TEST_CASE_P( DISABLED_Arabic, MatchGroundTruth, |
96 |
| - ::testing::Values("script/Arabic") ); |
| 91 | +INSTANTIATE_TEST_CASE_P(Eng, MatchGroundTruth, ::testing::Values("eng")); |
| 92 | +INSTANTIATE_TEST_CASE_P(DISABLED_Latin, MatchGroundTruth, |
| 93 | + ::testing::Values("script/Latin")); |
| 94 | +INSTANTIATE_TEST_CASE_P(DISABLED_Deva, MatchGroundTruth, |
| 95 | + ::testing::Values("script/Devanagari")); |
| 96 | +INSTANTIATE_TEST_CASE_P(DISABLED_Arabic, MatchGroundTruth, |
| 97 | + ::testing::Values("script/Arabic")); |
97 | 98 |
|
98 |
| - class EuroText : public QuickTest { |
99 |
| - }; |
| 99 | +class EuroText : public QuickTest {}; |
100 | 100 |
|
101 |
| - TEST_F(EuroText, FastLatinOCR) { |
102 |
| - OCRTester(TESTING_DIR "/eurotext.tif", |
103 |
| - TESTING_DIR "/eurotext.txt", |
104 |
| - TESSDATA_DIR "_fast", "script/Latin"); |
105 |
| - } |
| 101 | +TEST_F(EuroText, FastLatinOCR) { |
| 102 | + OCRTester(TESTING_DIR "/eurotext.tif", TESTING_DIR "/eurotext.txt", |
| 103 | + TESSDATA_DIR "_fast", "script/Latin"); |
| 104 | +} |
106 | 105 |
|
107 |
| - // script/Latin for eurotext.tif does not match groundtruth |
108 |
| - // for tessdata & tessdata_best. |
109 |
| - // so do not test these here. |
| 106 | +// script/Latin for eurotext.tif does not match groundtruth |
| 107 | +// for tessdata & tessdata_best. |
| 108 | +// so do not test these here. |
110 | 109 |
|
111 | 110 | } // namespace
|
0 commit comments