tesseract  4.00.00dev
apiexample_test.cc
Go to the documentation of this file.
1 // File: apiexample_test.cc
3 // Description: Api Example 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 #include "gtest/gtest.h"
17 #include "baseapi.h"
18 #include "leptonica/allheaders.h"
19 #include <iostream>
20 #include <string>
21 #include <fstream>
22 #include <locale>
23 
24 TEST(TesseractTest, ApiExample)
25 {
26  char *outText;
27  std::locale loc("C"); // You can also use "" for the default system locale
28  std::ifstream file("../testing/phototest.txt");
29  file.imbue(loc); // Use it for file input
30  std::string gtText((std::istreambuf_iterator<char>(file)),
31  std::istreambuf_iterator<char>());
32 
34  // Initialize tesseract-ocr with English, without specifying tessdata path
35  ASSERT_FALSE(api->Init(nullptr, "eng")) << "Could not initialize tesseract.";
36 
37  // Open input image with leptonica library
38  Pix *image = pixRead("../testing/phototest.tif");
39  ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
40  api->SetImage(image);
41  // Get OCR result
42  outText = api->GetUTF8Text();
43 
44  ASSERT_EQ(gtText,outText) << "Phototest.tif with default values OCR does not match ground truth";
45 
46  // Destroy used object and release memory
47  api->End();
48  delete [] outText;
49  pixDestroy(&image);
50 
51 }
52 
53 int main(int argc, char **argv) {
54  ::testing::InitGoogleTest(&argc, argv);
55  return RUN_ALL_TESTS();
56 }
TEST(TesseractTest, ApiExample)
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:332
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:561
int main(int argc, char **argv)