tesseract  4.0.0-beta.1-59-g2cc4
apiexample_test.cc
Go to the documentation of this file.
1 // File: apiexample_test.cc
3 // Description: Api Test for Tesseract using text fixtures and parameters.
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 // expects clone of tessdata_fast repo in ../../tessdata_fast
18 
19 //#include "log.h"
20 #include "include_gunit.h"
21 #include "baseapi.h"
22 #include "leptonica/allheaders.h"
23 #include <iostream>
24 #include <string>
25 #include <fstream>
26 #include <locale>
27 #include <limits.h>
28 #include <time.h>
29 
30 namespace {
31 
32 class QuickTest : public testing::Test {
33  protected:
34  virtual void SetUp() {
35  start_time_ = time(NULL);
36  }
37  virtual void TearDown() {
38  const time_t end_time = time(NULL);
39  EXPECT_TRUE(end_time - start_time_ <=25) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
40  }
41  time_t start_time_;
42  };
43 
44  void OCRTester(const char* imgname, const char* groundtruth, const char* tessdatadir, const char* lang) {
45  //log.info() << tessdatadir << " for language: " << lang << std::endl;
46  char *outText;
47  std::locale loc("C"); // You can also use "" for the default system locale
48  std::ifstream file(groundtruth);
49  file.imbue(loc); // Use it for file input
50  std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
52  ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
53  Pix *image = pixRead(imgname);
54  ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
55  api->SetImage(image);
56  outText = api->GetUTF8Text();
57  EXPECT_EQ(gtText,outText) << "Phototest.tif OCR does not match ground truth for " << ::testing::PrintToString(lang);
58  api->End();
59  delete [] outText;
60  pixDestroy(&image);
61  }
62 
63  class MatchGroundTruth : public QuickTest ,
64  public ::testing::WithParamInterface<const char*> {
65  };
66 
67  TEST_P(MatchGroundTruth, FastPhototestOCR) {
68  OCRTester(TESTING_DIR "/phototest.tif",
69  TESTING_DIR "/phototest.txt",
70  TESSDATA_DIR "_fast", GetParam());
71  }
72 
73  INSTANTIATE_TEST_CASE_P( EngLatinDevaArabLang, MatchGroundTruth,
74  ::testing::Values("eng", "script/Latin", "script/Devanagari", "script/Arabic") );
75 
76  class EuroText : public QuickTest {
77  };
78 
79  TEST_F(EuroText, FastOCR) {
80  OCRTester(TESTING_DIR "/eurotext.tif",
81  TESTING_DIR "/eurotext.txt",
82  TESSDATA_DIR "_fast", "script/Latin");
83  }
84 
85 } // namespace
struct TessBaseAPI TessBaseAPI
Definition: capi.h:83
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const GenericVector< STRING > *vars_vec, const GenericVector< STRING > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:326
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:555