Skip to content

Commit

Permalink
unittest: Add baseapi_test
Browse files Browse the repository at this point in the history
* Add Abseil sources to build process.

* Add copyright comment.

* InitConfigOnlyTest no longer tests
  hin.traineddata because it is LSTM only.

* Fix std::string.

* Deactivate tests with missing test data.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 11, 2018
1 parent db16fea commit 11f82f5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 35 deletions.
22 changes: 22 additions & 0 deletions unittest/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ AM_CPPFLAGS += -I$(top_srcdir)/src/training
endif
AM_CPPFLAGS += -I$(top_srcdir)/src/viewer
AM_CPPFLAGS += -I$(top_srcdir)/src/wordrec
AM_CPPFLAGS += -I$(top_srcdir)/abseil

# Build googletest:
check_LTLIBRARIES = libgtest.la libgtest_main.la libgmock.la libgmock_main.la
Expand All @@ -34,6 +35,22 @@ libgtest_la_CPPFLAGS = -I$(top_srcdir)/googletest/googletest/include -I$(top_src
libgtest_main_la_SOURCES = ../googletest/googletest/src/gtest_main.cc
## libgtest_main_la_LIBADD = libgtest.la

# Build Abseil (needed for some unit tests).
check_LTLIBRARIES += libabseil.la
libabseil_la_SOURCES =
libabseil_la_SOURCES += ../abseil/absl/base/internal/cycleclock.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/raw_logging.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/spinlock.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/spinlock_wait.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/sysinfo.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/throw_delegate.cc
libabseil_la_SOURCES += ../abseil/absl/base/internal/unscaledcycleclock.cc
libabseil_la_SOURCES += ../abseil/absl/numeric/int128.cc
libabseil_la_SOURCES += ../abseil/absl/strings/ascii.cc
libabseil_la_SOURCES += ../abseil/absl/strings/str_cat.cc
libabseil_la_SOURCES += ../abseil/absl/time/clock.cc
libabseil_la_SOURCES += ../abseil/absl/time/duration.cc

GMOCK_INCLUDES = -I$(top_srcdir)/googletest/googlemock/include \
-I$(top_srcdir)/googletest/googlemock \
-I$(top_srcdir)/googletest/googletest/include \
Expand All @@ -47,6 +64,7 @@ libgmock_main_la_CPPFLAGS = $(GMOCK_INCLUDES) \
-pthread

# Build unittests
ABSEIL_LIBS = libabseil.la
GTEST_LIBS = libgtest.la libgtest_main.la
GMOCK_LIBS = libgmock.la libgmock_main.la
TESS_LIBS = $(top_builddir)/src/api/libtesseract.la
Expand All @@ -59,6 +77,7 @@ AM_CPPFLAGS += -isystem $(top_srcdir)/googletest/googletest/include \
check_PROGRAMS = \
apiexample_test \
applybox_test \
baseapi_test \
bitvector_test \
cleanapi_test \
colpartition_test \
Expand Down Expand Up @@ -97,6 +116,9 @@ apiexample_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
applybox_test_SOURCES = applybox_test.cc
applybox_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)

baseapi_test_SOURCES = baseapi_test.cc
baseapi_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)

bitvector_test_SOURCES = bitvector_test.cc
bitvector_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

Expand Down
94 changes: 59 additions & 35 deletions unittest/baseapi_test.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
// (C) Copyright 2017, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tesseract/api/baseapi.h"
#include <memory>
#include <string>
#include <vector>
#include "leptonica/include/allheaders.h"
#include "tesseract/ccstruct/pageres.h"

#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "allheaders.h"

#include "include_gunit.h"
#include "gmock/gmock-matchers.h"

#include "baseapi.h"
#include "cycletimer.h" // for CycleTimer
#include "log.h" // for LOG
#include "ocrblock.h" // for class BLOCK
#include "pageres.h"

namespace {

using ::testing::ContainsRegex;
using ::testing::HasSubstr;

const char* langs[] = {"eng", "vie", "hin", "ara", nullptr};
const char* image_files[] = {"HelloGoogle.tif", "viet.tif", "raaj.tif",
static const char* langs[] = {"eng", "vie", "hin", "ara", nullptr};
static const char* image_files[] = {"HelloGoogle.tif", "viet.tif", "raaj.tif",
"arabic.tif", nullptr};
const char* gt_text[] = {"Hello Google", "\x74\x69\xe1\xba\xbf\x6e\x67",
static const char* gt_text[] = {"Hello Google", "\x74\x69\xe1\xba\xbf\x6e\x67",
"\xe0\xa4\xb0\xe0\xa4\xbe\xe0\xa4\x9c",
"\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a",
nullptr};
Expand All @@ -23,10 +43,10 @@ class FriendlyTessBaseAPI : public tesseract::TessBaseAPI {
FRIEND_TEST(TesseractTest, LSTMGeometryTest);
};

string GetCleanedTextResult(tesseract::TessBaseAPI* tess, Pix* pix) {
std::string GetCleanedTextResult(tesseract::TessBaseAPI* tess, Pix* pix) {
tess->SetImage(pix);
char* result = tess->GetUTF8Text();
string ocr_result = result;
std::string ocr_result = result;
delete[] result;
absl::StripAsciiWhitespace(&ocr_result);
return ocr_result;
Expand All @@ -35,11 +55,11 @@ string GetCleanedTextResult(tesseract::TessBaseAPI* tess, Pix* pix) {
// The fixture for testing Tesseract.
class TesseractTest : public testing::Test {
protected:
string TestDataNameToPath(const string& name) {
return file::JoinPath(FLAGS_test_srcdir, "testdata/" + name);
static std::string TestDataNameToPath(const std::string& name) {
return file::JoinPath(TESTING_DIR, name);
}
string TessdataPath() {
return file::JoinPath(FLAGS_test_srcdir, "tessdata");
static std::string TessdataPath() {
return TESSDATA_DIR;
}
};

Expand All @@ -54,8 +74,8 @@ TEST_F(TesseractTest, ArraySizeTest) {
// Tests that Tesseract gets exactly the right answer on phototest.
TEST_F(TesseractTest, BasicTesseractTest) {
tesseract::TessBaseAPI api;
string truth_text;
string ocr_text;
std::string truth_text;
std::string ocr_text;
api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY);
Pix* src_pix = pixRead(TestDataNameToPath("phototest.tif").c_str());
CHECK(src_pix);
Expand All @@ -74,6 +94,7 @@ TEST_F(TesseractTest, IteratesParagraphsEvenIfNotDetected) {
api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY);
api.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
api.SetVariable("paragraph_debug_level", "3");
#if 0 // TODO: b622.png is missing
Pix* src_pix = pixRead(TestDataNameToPath("b622.png").c_str());
CHECK(src_pix);
api.SetImage(src_pix);
Expand All @@ -88,6 +109,7 @@ TEST_F(TesseractTest, IteratesParagraphsEvenIfNotDetected) {
boxaDestroy(&block_boxes);
boxaDestroy(&para_boxes);
pixDestroy(&src_pix);
#endif
}

// We should get hOCR output and not seg fault, even if the api caller doesn't
Expand Down Expand Up @@ -130,6 +152,7 @@ TEST_F(TesseractTest, HOCRContainsBaseline) {
TEST_F(TesseractTest, RickSnyderNotFuckSnyder) {
tesseract::TessBaseAPI api;
api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY);
#if 0 // TODO: rick_snyder.jpeg is missing
Pix* src_pix = pixRead(TestDataNameToPath("rick_snyder.jpeg").c_str());
CHECK(src_pix);
api.SetImage(src_pix);
Expand All @@ -138,6 +161,7 @@ TEST_F(TesseractTest, RickSnyderNotFuckSnyder) {
EXPECT_THAT(result, Not(HasSubstr("FUCK")));
delete[] result;
pixDestroy(&src_pix);
#endif
}

// Tests that Tesseract gets exactly the right answer on some page numbers.
Expand All @@ -152,14 +176,14 @@ TEST_F(TesseractTest, AdaptToWordStrTest) {
static const char* kTestPages[] = {"324.tif", "433.tif", "12.tif", nullptr};
static const char* kTestText[] = {"324", "433", "12", nullptr};
tesseract::TessBaseAPI api;
string truth_text;
string ocr_text;
std::string truth_text;
std::string ocr_text;
api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY);
api.SetVariable("matcher_sufficient_examples_for_prototyping", "1");
api.SetVariable("classify_class_pruner_threshold", "220");
// Train on the training text.
for (int i = 0; kTrainingPages[i] != nullptr; ++i) {
string image_file = TestDataNameToPath(kTrainingPages[i]);
std::string image_file = TestDataNameToPath(kTrainingPages[i]);
Pix* src_pix = pixRead(image_file.c_str());
CHECK(src_pix);
api.SetImage(src_pix);
Expand All @@ -185,8 +209,8 @@ TEST_F(TesseractTest, AdaptToWordStrTest) {
// Tests that LSTM gets exactly the right answer on phototest.
TEST_F(TesseractTest, BasicLSTMTest) {
tesseract::TessBaseAPI api;
string truth_text;
string ocr_text;
std::string truth_text;
std::string ocr_text;
api.Init(TessdataPath().c_str(), "eng", tesseract::OEM_LSTM_ONLY);
Pix* src_pix = pixRead(TestDataNameToPath("phototest_2.tif").c_str());
CHECK(src_pix);
Expand Down Expand Up @@ -247,7 +271,7 @@ TEST_F(TesseractTest, LSTMGeometryTest) {

TEST_F(TesseractTest, InitConfigOnlyTest) {
// Languages for testing initialization.
const char* langs[] = {"eng", "chi_tra", "jpn", "vie", "hin"};
const char* langs[] = {"eng", "chi_tra", "jpn", "vie"};
std::unique_ptr<tesseract::TessBaseAPI> api;
CycleTimer timer;
for (int i = 0; i < ARRAYSIZE(langs); ++i) {
Expand Down Expand Up @@ -286,24 +310,24 @@ TEST(TesseractInstanceTest, TestMultipleTessInstances) {
int num_langs = 0;
while (langs[num_langs] != nullptr) ++num_langs;

const string kTessdataPath = file::JoinPath(FLAGS_test_srcdir, "tessdata");
const std::string kTessdataPath = TESSDATA_DIR;

// Preload images and verify that OCR is correct on them individually.
std::vector<Pix*> pix(num_langs);
for (int i = 0; i < num_langs; ++i) {
SCOPED_TRACE(absl::StrCat("Single instance test with lang = ", langs[i]));
string path = FLAGS_test_srcdir + "/testdata/" + image_files[i];
std::string path = file::JoinPath(TESTING_DIR, image_files[i]);
pix[i] = pixRead(path.c_str());
QCHECK(pix[i] != nullptr) << "Could not read " << path;

tesseract::TessBaseAPI tess;
EXPECT_EQ(0, tess.Init(kTessdataPath.c_str(), langs[i]));
string ocr_result = GetCleanedTextResult(&tess, pix[i]);
std::string ocr_result = GetCleanedTextResult(&tess, pix[i]);
EXPECT_STREQ(gt_text[i], ocr_result.c_str());
}

// Process the images in all pairwise combinations of associated languages.
string ocr_result[2];
std::string ocr_result[2];
for (int i = 0; i < num_langs; ++i) {
for (int j = i + 1; j < num_langs; ++j) {
tesseract::TessBaseAPI tess1, tess2;
Expand All @@ -324,21 +348,21 @@ TEST(TesseractInstanceTest, TestMultipleTessInstances) {

// Tests whether Tesseract parameters are correctly set for the two instances.
TEST(TesseractInstanceTest, TestMultipleTessInstanceVariables) {
string illegal_name = "an_illegal_name";
string langs[2] = {"eng", "hin"};
string int_param_name = "tessedit_pageseg_mode";
std::string illegal_name = "an_illegal_name";
std::string langs[2] = {"eng", "hin"};
std::string int_param_name = "tessedit_pageseg_mode";
int int_param[2] = {1, 2};
string int_param_str[2] = {"1", "2"};
string bool_param_name = "tessedit_ambigs_training";
std::string int_param_str[2] = {"1", "2"};
std::string bool_param_name = "tessedit_ambigs_training";
bool bool_param[2] = {false, true};
string bool_param_str[2] = {"F", "T"};
string str_param_name = "tessedit_char_blacklist";
string str_param[2] = {"abc", "def"};
string double_param_name = "segment_penalty_dict_frequent_word";
string double_param_str[2] = {"0.01", "2"};
std::string bool_param_str[2] = {"F", "T"};
std::string str_param_name = "tessedit_char_blacklist";
std::string str_param[2] = {"abc", "def"};
std::string double_param_name = "segment_penalty_dict_frequent_word";
std::string double_param_str[2] = {"0.01", "2"};
double double_param[2] = {0.01, 2};

const string kTessdataPath = file::JoinPath(FLAGS_test_srcdir, "tessdata");
const std::string kTessdataPath = TESSDATA_DIR;

tesseract::TessBaseAPI tess1, tess2;
for (int i = 0; i < 2; ++i) {
Expand Down

0 comments on commit 11f82f5

Please sign in to comment.