1
+ // (C) Copyright 2017, Google Inc.
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ // http://www.apache.org/licenses/LICENSE-2.0
6
+ // Unless required by applicable law or agreed to in writing, software
7
+ // distributed under the License is distributed on an "AS IS" BASIS,
8
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ // See the License for the specific language governing permissions and
10
+ // limitations under the License.
1
11
2
- #include " tesseract/api/baseapi.h"
3
12
#include < memory>
4
13
#include < string>
5
14
#include < vector>
6
- #include " leptonica/include/allheaders.h"
7
- #include " tesseract/ccstruct/pageres.h"
15
+
16
+ #include " absl/strings/ascii.h"
17
+ #include " absl/strings/str_cat.h"
18
+ #include " allheaders.h"
19
+
20
+ #include " include_gunit.h"
21
+ #include " gmock/gmock-matchers.h"
22
+
23
+ #include " baseapi.h"
24
+ #include " cycletimer.h" // for CycleTimer
25
+ #include " log.h" // for LOG
26
+ #include " ocrblock.h" // for class BLOCK
27
+ #include " pageres.h"
8
28
9
29
namespace {
10
30
11
31
using ::testing::ContainsRegex;
12
32
using ::testing::HasSubstr;
13
33
14
- const char * langs[] = {" eng" , " vie" , " hin" , " ara" , nullptr };
15
- const char * image_files[] = {" HelloGoogle.tif" , " viet.tif" , " raaj.tif" ,
34
+ static const char * langs[] = {" eng" , " vie" , " hin" , " ara" , nullptr };
35
+ static const char * image_files[] = {" HelloGoogle.tif" , " viet.tif" , " raaj.tif" ,
16
36
" arabic.tif" , nullptr };
17
- const char * gt_text[] = {" Hello Google" , " \x74\x69\xe1\xba\xbf\x6e\x67 " ,
37
+ static const char * gt_text[] = {" Hello Google" , " \x74\x69\xe1\xba\xbf\x6e\x67 " ,
18
38
" \xe0\xa4\xb0\xe0\xa4\xbe\xe0\xa4\x9c " ,
19
39
" \xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a " ,
20
40
nullptr };
@@ -23,10 +43,10 @@ class FriendlyTessBaseAPI : public tesseract::TessBaseAPI {
23
43
FRIEND_TEST (TesseractTest, LSTMGeometryTest);
24
44
};
25
45
26
- string GetCleanedTextResult (tesseract::TessBaseAPI* tess, Pix* pix) {
46
+ std:: string GetCleanedTextResult (tesseract::TessBaseAPI* tess, Pix* pix) {
27
47
tess->SetImage (pix);
28
48
char * result = tess->GetUTF8Text ();
29
- string ocr_result = result;
49
+ std:: string ocr_result = result;
30
50
delete[] result;
31
51
absl::StripAsciiWhitespace (&ocr_result);
32
52
return ocr_result;
@@ -35,11 +55,11 @@ string GetCleanedTextResult(tesseract::TessBaseAPI* tess, Pix* pix) {
35
55
// The fixture for testing Tesseract.
36
56
class TesseractTest : public testing ::Test {
37
57
protected:
38
- string TestDataNameToPath (const string& name) {
39
- return file::JoinPath (FLAGS_test_srcdir, " testdata/ " + name);
58
+ static std:: string TestDataNameToPath (const std:: string& name) {
59
+ return file::JoinPath (TESTING_DIR, name);
40
60
}
41
- string TessdataPath () {
42
- return file::JoinPath (FLAGS_test_srcdir, " tessdata " ) ;
61
+ static std:: string TessdataPath () {
62
+ return TESSDATA_DIR ;
43
63
}
44
64
};
45
65
@@ -54,8 +74,8 @@ TEST_F(TesseractTest, ArraySizeTest) {
54
74
// Tests that Tesseract gets exactly the right answer on phototest.
55
75
TEST_F (TesseractTest, BasicTesseractTest) {
56
76
tesseract::TessBaseAPI api;
57
- string truth_text;
58
- string ocr_text;
77
+ std:: string truth_text;
78
+ std:: string ocr_text;
59
79
api.Init (TessdataPath ().c_str (), " eng" , tesseract::OEM_TESSERACT_ONLY);
60
80
Pix* src_pix = pixRead (TestDataNameToPath (" phototest.tif" ).c_str ());
61
81
CHECK (src_pix);
@@ -74,6 +94,7 @@ TEST_F(TesseractTest, IteratesParagraphsEvenIfNotDetected) {
74
94
api.Init (TessdataPath ().c_str (), " eng" , tesseract::OEM_TESSERACT_ONLY);
75
95
api.SetPageSegMode (tesseract::PSM_SINGLE_BLOCK);
76
96
api.SetVariable (" paragraph_debug_level" , " 3" );
97
+ #if 0 // TODO: b622.png is missing
77
98
Pix* src_pix = pixRead(TestDataNameToPath("b622.png").c_str());
78
99
CHECK(src_pix);
79
100
api.SetImage(src_pix);
@@ -88,6 +109,7 @@ TEST_F(TesseractTest, IteratesParagraphsEvenIfNotDetected) {
88
109
boxaDestroy(&block_boxes);
89
110
boxaDestroy(¶_boxes);
90
111
pixDestroy(&src_pix);
112
+ #endif
91
113
}
92
114
93
115
// We should get hOCR output and not seg fault, even if the api caller doesn't
@@ -130,6 +152,7 @@ TEST_F(TesseractTest, HOCRContainsBaseline) {
130
152
TEST_F (TesseractTest, RickSnyderNotFuckSnyder) {
131
153
tesseract::TessBaseAPI api;
132
154
api.Init (TessdataPath ().c_str (), " eng" , tesseract::OEM_TESSERACT_ONLY);
155
+ #if 0 // TODO: rick_snyder.jpeg is missing
133
156
Pix* src_pix = pixRead(TestDataNameToPath("rick_snyder.jpeg").c_str());
134
157
CHECK(src_pix);
135
158
api.SetImage(src_pix);
@@ -138,6 +161,7 @@ TEST_F(TesseractTest, RickSnyderNotFuckSnyder) {
138
161
EXPECT_THAT(result, Not(HasSubstr("FUCK")));
139
162
delete[] result;
140
163
pixDestroy(&src_pix);
164
+ #endif
141
165
}
142
166
143
167
// Tests that Tesseract gets exactly the right answer on some page numbers.
@@ -152,14 +176,14 @@ TEST_F(TesseractTest, AdaptToWordStrTest) {
152
176
static const char * kTestPages [] = {" 324.tif" , " 433.tif" , " 12.tif" , nullptr };
153
177
static const char * kTestText [] = {" 324" , " 433" , " 12" , nullptr };
154
178
tesseract::TessBaseAPI api;
155
- string truth_text;
156
- string ocr_text;
179
+ std:: string truth_text;
180
+ std:: string ocr_text;
157
181
api.Init (TessdataPath ().c_str (), " eng" , tesseract::OEM_TESSERACT_ONLY);
158
182
api.SetVariable (" matcher_sufficient_examples_for_prototyping" , " 1" );
159
183
api.SetVariable (" classify_class_pruner_threshold" , " 220" );
160
184
// Train on the training text.
161
185
for (int i = 0 ; kTrainingPages [i] != nullptr ; ++i) {
162
- string image_file = TestDataNameToPath (kTrainingPages [i]);
186
+ std:: string image_file = TestDataNameToPath (kTrainingPages [i]);
163
187
Pix* src_pix = pixRead (image_file.c_str ());
164
188
CHECK (src_pix);
165
189
api.SetImage (src_pix);
@@ -185,8 +209,8 @@ TEST_F(TesseractTest, AdaptToWordStrTest) {
185
209
// Tests that LSTM gets exactly the right answer on phototest.
186
210
TEST_F (TesseractTest, BasicLSTMTest) {
187
211
tesseract::TessBaseAPI api;
188
- string truth_text;
189
- string ocr_text;
212
+ std:: string truth_text;
213
+ std:: string ocr_text;
190
214
api.Init (TessdataPath ().c_str (), " eng" , tesseract::OEM_LSTM_ONLY);
191
215
Pix* src_pix = pixRead (TestDataNameToPath (" phototest_2.tif" ).c_str ());
192
216
CHECK (src_pix);
@@ -247,7 +271,7 @@ TEST_F(TesseractTest, LSTMGeometryTest) {
247
271
248
272
TEST_F (TesseractTest, InitConfigOnlyTest) {
249
273
// Languages for testing initialization.
250
- const char * langs[] = {" eng" , " chi_tra" , " jpn" , " vie" , " hin " };
274
+ const char * langs[] = {" eng" , " chi_tra" , " jpn" , " vie" };
251
275
std::unique_ptr<tesseract::TessBaseAPI> api;
252
276
CycleTimer timer;
253
277
for (int i = 0 ; i < ARRAYSIZE (langs); ++i) {
@@ -286,24 +310,24 @@ TEST(TesseractInstanceTest, TestMultipleTessInstances) {
286
310
int num_langs = 0 ;
287
311
while (langs[num_langs] != nullptr ) ++num_langs;
288
312
289
- const string kTessdataPath = file::JoinPath (FLAGS_test_srcdir, " tessdata " ) ;
313
+ const std:: string kTessdataPath = TESSDATA_DIR ;
290
314
291
315
// Preload images and verify that OCR is correct on them individually.
292
316
std::vector<Pix*> pix (num_langs);
293
317
for (int i = 0 ; i < num_langs; ++i) {
294
318
SCOPED_TRACE (absl::StrCat (" Single instance test with lang = " , langs[i]));
295
- string path = FLAGS_test_srcdir + " /testdata/ " + image_files[i];
319
+ std:: string path = file::JoinPath (TESTING_DIR, image_files[i]) ;
296
320
pix[i] = pixRead (path.c_str ());
297
321
QCHECK (pix[i] != nullptr ) << " Could not read " << path;
298
322
299
323
tesseract::TessBaseAPI tess;
300
324
EXPECT_EQ (0 , tess.Init (kTessdataPath .c_str (), langs[i]));
301
- string ocr_result = GetCleanedTextResult (&tess, pix[i]);
325
+ std:: string ocr_result = GetCleanedTextResult (&tess, pix[i]);
302
326
EXPECT_STREQ (gt_text[i], ocr_result.c_str ());
303
327
}
304
328
305
329
// Process the images in all pairwise combinations of associated languages.
306
- string ocr_result[2 ];
330
+ std:: string ocr_result[2 ];
307
331
for (int i = 0 ; i < num_langs; ++i) {
308
332
for (int j = i + 1 ; j < num_langs; ++j) {
309
333
tesseract::TessBaseAPI tess1, tess2;
@@ -324,21 +348,21 @@ TEST(TesseractInstanceTest, TestMultipleTessInstances) {
324
348
325
349
// Tests whether Tesseract parameters are correctly set for the two instances.
326
350
TEST (TesseractInstanceTest, TestMultipleTessInstanceVariables) {
327
- string illegal_name = " an_illegal_name" ;
328
- string langs[2 ] = {" eng" , " hin" };
329
- string int_param_name = " tessedit_pageseg_mode" ;
351
+ std:: string illegal_name = " an_illegal_name" ;
352
+ std:: string langs[2 ] = {" eng" , " hin" };
353
+ std:: string int_param_name = " tessedit_pageseg_mode" ;
330
354
int int_param[2 ] = {1 , 2 };
331
- string int_param_str[2 ] = {" 1" , " 2" };
332
- string bool_param_name = " tessedit_ambigs_training" ;
355
+ std:: string int_param_str[2 ] = {" 1" , " 2" };
356
+ std:: string bool_param_name = " tessedit_ambigs_training" ;
333
357
bool bool_param[2 ] = {false , true };
334
- string bool_param_str[2 ] = {" F" , " T" };
335
- string str_param_name = " tessedit_char_blacklist" ;
336
- string str_param[2 ] = {" abc" , " def" };
337
- string double_param_name = " segment_penalty_dict_frequent_word" ;
338
- string double_param_str[2 ] = {" 0.01" , " 2" };
358
+ std:: string bool_param_str[2 ] = {" F" , " T" };
359
+ std:: string str_param_name = " tessedit_char_blacklist" ;
360
+ std:: string str_param[2 ] = {" abc" , " def" };
361
+ std:: string double_param_name = " segment_penalty_dict_frequent_word" ;
362
+ std:: string double_param_str[2 ] = {" 0.01" , " 2" };
339
363
double double_param[2 ] = {0.01 , 2 };
340
364
341
- const string kTessdataPath = file::JoinPath (FLAGS_test_srcdir, " tessdata " ) ;
365
+ const std:: string kTessdataPath = TESSDATA_DIR ;
342
366
343
367
tesseract::TessBaseAPI tess1, tess2;
344
368
for (int i = 0 ; i < 2 ; ++i) {
0 commit comments