Skip to content

Commit

Permalink
Merge pull request #1863 from stweil/unittest
Browse files Browse the repository at this point in the history
Add Google code for more unittests
  • Loading branch information
egorpugin authored Aug 25, 2018
2 parents f5705da + aec9ce7 commit 4620674
Show file tree
Hide file tree
Showing 60 changed files with 9,639 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ccutil/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pkginclude_HEADERS = \

noinst_HEADERS = \
ambigs.h basedir.h bits16.h bitvector.h ccutil.h clst.h doubleptr.h elst2.h \
elst.h errcode.h fileerr.h genericheap.h globaloc.h \
elst.h errcode.h fileerr.h fileio.h \
genericheap.h globaloc.h \
indexmapbidi.h kdpair.h lsterr.h \
object_cache.h params.h qrsequence.h sorthelper.h \
scanutils.h tessdatamanager.h tprintf.h \
Expand All @@ -31,6 +32,7 @@ libtesseract_ccutil_la_SOURCES = \
ambigs.cpp basedir.cpp bitvector.cpp \
ccutil.cpp clst.cpp \
elst2.cpp elst.cpp errcode.cpp \
fileio.cpp \
globaloc.cpp indexmapbidi.cpp \
mainblk.cpp \
serialis.cpp strngs.cpp scanutils.cpp \
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions src/training/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ endif()
########################################

set(unicharset_training_src
fileio.cpp
fileio.h
icuerrorcode.h
lang_model_helpers.cpp
lang_model_helpers.h
Expand Down
2 changes: 0 additions & 2 deletions src/training/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ noinst_HEADERS = \
commandlineflags.h \
commontraining.h \
degradeimage.h \
fileio.h \
icuerrorcode.h \
lang_model_helpers.h \
ligature_table.h \
Expand Down Expand Up @@ -66,7 +65,6 @@ libtesseract_training_la_SOURCES = \
commandlineflags.cpp \
commontraining.cpp \
degradeimage.cpp \
fileio.cpp \
lang_model_helpers.cpp \
ligature_table.cpp \
lstmtester.cpp \
Expand Down
31 changes: 29 additions & 2 deletions unittest/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,49 @@ AM_CPPFLAGS += -isystem $(top_srcdir)/googletest/googletest/include \

check_PROGRAMS = \
apiexample_test \
bitvector_test \
cleanapi_test \
colpartition_test \
denorm_test \
heap_test \
indexmapbidi_test \
intfeaturemap_test \
progress_test \
intsimdmatrix_test \
matrix_test \
osd_test \
loadlang_test \
tesseracttests


TESTS = $(check_PROGRAMS)

#List of source files needed to build the executable:
# List of source files needed to build the executable:

apiexample_test_SOURCES = apiexample_test.cc
apiexample_test_LDFLAGS = $(OPENCL_LDFLAGS) $(LEPTONICA_LIBS)
apiexample_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)

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

cleanapi_test_SOURCES = cleanapi_test.cc
cleanapi_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

colpartition_test_SOURCES = colpartition_test.cc
colpartition_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

denorm_test_SOURCES = denorm_test.cc
denorm_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

heap_test_SOURCES = heap_test.cc
heap_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

indexmapbidi_test_SOURCES = indexmapbidi_test.cc
indexmapbidi_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

intfeaturemap_test_SOURCES = intfeaturemap_test.cc
intfeaturemap_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

progress_test_SOURCES = progress_test.cc
progress_test_LDFLAGS = $(OPENCL_LDFLAGS) $(LEPTONICA_LIBS)
progress_test_LDADD = $(GTEST_LIBS) $(GMOCK_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
Expand Down
122 changes: 122 additions & 0 deletions unittest/applybox_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@

#include <string>
#include "leptonica/include/allheaders.h"
#include "tesseract/api/baseapi.h"
#include "tesseract/ccmain/resultiterator.h"
#include "tesseract/ccstruct/boxread.h"
#include "tesseract/ccstruct/rect.h"

namespace {

using tesseract::ResultIterator;

const char* kTruthTextWords = "To simple burn running of goods lately.\n\n";
const char* kTruthTextLine = "Tosimpleburnrunningofgoodslately.\n\n";

// The fixture for testing Tesseract.
class ApplyBoxTest : public testing::Test {
protected:
string TestDataNameToPath(const string& name) {
return file::JoinPath(FLAGS_test_srcdir,
"testdata/" + name);
}
string TessdataPath() {
return file::JoinPath(FLAGS_test_srcdir,
"tessdata");
}
string OutputNameToPath(const string& name) {
return file::JoinPath(FLAGS_test_tmpdir, name);
}

ApplyBoxTest() {
src_pix_ = NULL;
}
~ApplyBoxTest() {
pixDestroy(&src_pix_);
}

void SetImage(const char* filename) {
pixDestroy(&src_pix_);
src_pix_ = pixRead(TestDataNameToPath(filename).c_str());
api_.Init(TessdataPath().c_str(), "eng", tesseract::OEM_TESSERACT_ONLY);
api_.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
api_.SetImage(src_pix_);
api_.SetVariable("tessedit_make_boxes_from_boxes", "1");
api_.SetInputName(TestDataNameToPath(filename).c_str());
}

// Runs ApplyBoxes (via setting the appropriate variables and Recognize)
// and checks that the output ocr text matches the truth_str, and that
// the boxes match the given box file well enough.
// If line_mode is true, ApplyBoxes is run in line segmentation mode,
// otherwise the input box file is assumed to have character-level boxes.
void VerifyBoxesAndText(const char* imagefile, const char* truth_str,
const char* target_box_file, bool line_mode) {
SetImage(imagefile);
if (line_mode)
api_.SetVariable("tessedit_resegment_from_line_boxes", "1");
else
api_.SetVariable("tessedit_resegment_from_boxes", "1");
api_.Recognize(NULL);
char* ocr_text = api_.GetUTF8Text();
EXPECT_STREQ(truth_str, ocr_text);
delete [] ocr_text;
// Test the boxes by reading the target box file in parallel with the
// bounding boxes in the ocr output.
string box_filename = TestDataNameToPath(target_box_file);
FILE* box_file = OpenBoxFile(STRING(box_filename.c_str()));
ASSERT_TRUE(box_file != NULL);
int height = pixGetHeight(src_pix_);
ResultIterator* it = api_.GetIterator();
do {
int left, top, right, bottom;
EXPECT_TRUE(it->BoundingBox(tesseract::RIL_SYMBOL,
&left, &top, &right, &bottom));
TBOX ocr_box(ICOORD(left, height - bottom),
ICOORD(right, height - top));
int line_number;
TBOX truth_box;
STRING box_text;
EXPECT_TRUE(ReadNextBox(0, &line_number, box_file, &box_text,
&truth_box));
// Testing for major overlap is a bit weak, but if they all
// major overlap successfully, then it has to be fairly close.
EXPECT_TRUE(ocr_box.major_overlap(truth_box));
// Also check that the symbol text matches the box text.
char* symbol_text = it->GetUTF8Text(tesseract::RIL_SYMBOL);
EXPECT_STREQ(box_text.string(), symbol_text);
delete [] symbol_text;
} while (it->Next(tesseract::RIL_SYMBOL));
delete it;
}

Pix* src_pix_;
string ocr_text_;
tesseract::TessBaseAPI api_;
};

// Tests character-level applyboxes on normal Times New Roman.
TEST_F(ApplyBoxTest, TimesCharLevel) {
VerifyBoxesAndText("trainingtimes.tif", kTruthTextWords,
"trainingtimes.box", false);
}

// Tests character-level applyboxes on italic Times New Roman.
TEST_F(ApplyBoxTest, ItalicCharLevel) {
VerifyBoxesAndText("trainingital.tif", kTruthTextWords,
"trainingital.box", false);
}

// Tests line-level applyboxes on normal Times New Roman.
TEST_F(ApplyBoxTest, TimesLineLevel) {
VerifyBoxesAndText("trainingtimesline.tif", kTruthTextLine,
"trainingtimes.box", true);
}

// Tests line-level applyboxes on italic Times New Roman.
TEST_F(ApplyBoxTest, ItalLineLevel) {
VerifyBoxesAndText("trainingitalline.tif", kTruthTextLine,
"trainingital.box", true);
}

} // namespace
Loading

0 comments on commit 4620674

Please sign in to comment.