2017-08-19 21:12:06 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-08-19 21:16:23 +08:00
|
|
|
// File: apiexample_test.cc
|
2018-03-04 21:52:27 +08:00
|
|
|
// Description: Api Test for Tesseract using text fixtures and parameters.
|
2017-08-19 21:12:06 +08:00
|
|
|
// Author: ShreeDevi Kumar
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-03-04 21:52:27 +08:00
|
|
|
|
|
|
|
// expects clone of tessdata_fast repo in ../../tessdata_fast
|
|
|
|
|
|
|
|
//#include "log.h"
|
|
|
|
#include "include_gunit.h"
|
2017-09-09 00:34:31 +08:00
|
|
|
#include "baseapi.h"
|
2017-08-19 21:12:06 +08:00
|
|
|
#include "leptonica/allheaders.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <locale>
|
2018-03-04 21:52:27 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class QuickTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
|
|
|
start_time_ = time(NULL);
|
|
|
|
}
|
|
|
|
virtual void TearDown() {
|
|
|
|
const time_t end_time = time(NULL);
|
|
|
|
EXPECT_TRUE(end_time - start_time_ <=25) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
|
|
|
|
}
|
|
|
|
time_t start_time_;
|
|
|
|
};
|
2017-08-19 21:12:06 +08:00
|
|
|
|
2018-03-04 21:52:27 +08:00
|
|
|
void OCRTester(const char* imgname, const char* groundtruth, const char* tessdatadir, const char* lang) {
|
|
|
|
//log.info() << tessdatadir << " for language: " << lang << std::endl;
|
2017-08-19 21:12:06 +08:00
|
|
|
char *outText;
|
2017-09-13 19:24:31 +08:00
|
|
|
std::locale loc("C"); // You can also use "" for the default system locale
|
2018-03-04 21:52:27 +08:00
|
|
|
std::ifstream file(groundtruth);
|
2017-08-19 21:16:23 +08:00
|
|
|
file.imbue(loc); // Use it for file input
|
2018-03-04 21:52:27 +08:00
|
|
|
std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
2017-08-19 21:12:06 +08:00
|
|
|
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
|
2018-03-04 21:52:27 +08:00
|
|
|
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
|
|
|
|
Pix *image = pixRead(imgname);
|
2017-09-13 19:43:49 +08:00
|
|
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
2017-08-19 21:12:06 +08:00
|
|
|
api->SetImage(image);
|
|
|
|
outText = api->GetUTF8Text();
|
2018-03-04 21:52:27 +08:00
|
|
|
EXPECT_EQ(gtText,outText) << "Phototest.tif OCR does not match ground truth for " << ::testing::PrintToString(lang);
|
2017-08-19 21:12:06 +08:00
|
|
|
api->End();
|
|
|
|
delete [] outText;
|
|
|
|
pixDestroy(&image);
|
2018-03-04 21:52:27 +08:00
|
|
|
}
|
2017-08-19 21:16:23 +08:00
|
|
|
|
2018-03-04 21:52:27 +08:00
|
|
|
class MatchGroundTruth : public QuickTest ,
|
|
|
|
public ::testing::WithParamInterface<const char*> {
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(MatchGroundTruth, FastPhototestOCR) {
|
2018-03-14 04:15:44 +08:00
|
|
|
OCRTester(TESTING_DIR "/phototest.tif",
|
|
|
|
TESTING_DIR "/phototest.txt",
|
|
|
|
TESSDATA_DIR "_fast", GetParam());
|
2018-03-04 21:52:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P( EngLatinDevaArabLang, MatchGroundTruth,
|
2018-03-14 04:15:44 +08:00
|
|
|
::testing::Values("eng", "script/Latin", "script/Devanagari", "script/Arabic") );
|
2017-08-19 21:12:06 +08:00
|
|
|
|
2018-03-04 21:52:27 +08:00
|
|
|
class EuroText : public QuickTest {
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(EuroText, FastOCR) {
|
2018-03-14 04:15:44 +08:00
|
|
|
OCRTester(TESTING_DIR "/eurotext.tif",
|
|
|
|
TESTING_DIR "/eurotext.txt",
|
|
|
|
TESSDATA_DIR "_fast", "script/Latin");
|
2018-03-04 21:52:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|