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.
|
2018-08-30 04:28:20 +08:00
|
|
|
// Tests for Devanagari, Latin and Arabic scripts are disabled by default.
|
2018-09-29 15:19:13 +08:00
|
|
|
// Disabled tests can be run when required by using the
|
|
|
|
// --gtest_also_run_disabled_tests argument.
|
2018-08-30 04:28:20 +08:00
|
|
|
// ./unittest/apiexample_test --gtest_also_run_disabled_tests
|
|
|
|
//
|
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"
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <allheaders.h>
|
|
|
|
#include <tesseract/baseapi.h>
|
2018-03-04 21:52:27 +08:00
|
|
|
#include <time.h>
|
2018-09-29 15:19:13 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <locale>
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <memory> // std::unique_ptr
|
2018-09-29 15:19:13 +08:00
|
|
|
#include <string>
|
|
|
|
#include "include_gunit.h"
|
2021-04-01 03:39:43 +08:00
|
|
|
#include "image.h"
|
2018-03-04 21:52:27 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-03-04 21:52:27 +08:00
|
|
|
|
|
|
|
class QuickTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2021-03-22 15:26:05 +08:00
|
|
|
void SetUp() override {
|
2021-03-13 05:06:34 +08:00
|
|
|
start_time_ = time(nullptr);
|
|
|
|
}
|
2021-03-22 15:26:05 +08:00
|
|
|
void TearDown() override {
|
2020-12-28 06:11:13 +08:00
|
|
|
#ifndef NDEBUG
|
2019-01-14 22:39:18 +08:00
|
|
|
// Debug builds can be very slow, so allow 4 min for OCR of a test image.
|
|
|
|
// apitest_example including disabled tests takes about 18 min on ARMv7.
|
|
|
|
const time_t MAX_SECONDS_FOR_TEST = 240;
|
|
|
|
#else
|
|
|
|
// Release builds typically need less than 10 s for OCR of a test image,
|
|
|
|
// apitest_example including disabled tests takes about 90 s on ARMv7.
|
|
|
|
const time_t MAX_SECONDS_FOR_TEST = 55;
|
|
|
|
#endif
|
2018-04-22 23:15:22 +08:00
|
|
|
const time_t end_time = time(nullptr);
|
2019-01-14 22:39:18 +08:00
|
|
|
EXPECT_TRUE(end_time - start_time_ <= MAX_SECONDS_FOR_TEST)
|
2021-03-13 05:06:34 +08:00
|
|
|
<< "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
|
2018-03-04 21:52:27 +08:00
|
|
|
}
|
|
|
|
time_t start_time_;
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
2017-08-19 21:12:06 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void OCRTester(const char *imgname, const char *groundtruth, const char *tessdatadir,
|
|
|
|
const char *lang) {
|
2018-09-29 15:19:13 +08:00
|
|
|
// log.info() << tessdatadir << " for language: " << lang << std::endl;
|
2021-03-13 05:06:34 +08:00
|
|
|
char *outText;
|
|
|
|
std::locale loc("C"); // You can also use "" for the default system locale
|
2018-09-29 15:19:13 +08:00
|
|
|
std::ifstream file(groundtruth);
|
2021-03-13 05:06:34 +08:00
|
|
|
file.imbue(loc); // Use it for file input
|
|
|
|
std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
2021-03-15 04:06:19 +08:00
|
|
|
auto api = std::make_unique<tesseract::TessBaseAPI>();
|
2021-03-13 05:06:34 +08:00
|
|
|
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
|
2021-04-01 03:39:43 +08:00
|
|
|
Image image = pixRead(imgname);
|
2018-09-29 15:19:13 +08:00
|
|
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
|
|
|
api->SetImage(image);
|
|
|
|
outText = api->GetUTF8Text();
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_EQ(gtText, outText) << "Phototest.tif OCR does not match ground truth for "
|
|
|
|
<< ::testing::PrintToString(lang);
|
2018-09-29 15:19:13 +08:00
|
|
|
api->End();
|
2021-05-17 15:27:41 +08:00
|
|
|
api->ClearPersistentCache();
|
2018-09-29 15:19:13 +08:00
|
|
|
delete[] outText;
|
2021-04-01 03:39:43 +08:00
|
|
|
image.destroy();
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
2017-08-19 21:16:23 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
class MatchGroundTruth : public QuickTest, public ::testing::WithParamInterface<const char *> {};
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
TEST_P(MatchGroundTruth, FastPhototestOCR) {
|
2021-03-13 05:06:34 +08:00
|
|
|
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", TESSDATA_DIR "_fast",
|
|
|
|
GetParam());
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
2017-08-19 21:12:06 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
TEST_P(MatchGroundTruth, BestPhototestOCR) {
|
2021-03-13 05:06:34 +08:00
|
|
|
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", TESSDATA_DIR "_best",
|
|
|
|
GetParam());
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
TEST_P(MatchGroundTruth, TessPhototestOCR) {
|
2021-03-13 05:06:34 +08:00
|
|
|
OCRTester(TESTING_DIR "/phototest.tif", TESTING_DIR "/phototest.txt", TESSDATA_DIR, GetParam());
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2021-01-21 02:03:05 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Eng, MatchGroundTruth, ::testing::Values("eng"));
|
2021-03-13 05:06:34 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(DISABLED_Latin, MatchGroundTruth, ::testing::Values("script/Latin"));
|
|
|
|
INSTANTIATE_TEST_SUITE_P(DISABLED_Deva, MatchGroundTruth, ::testing::Values("script/Devanagari"));
|
|
|
|
INSTANTIATE_TEST_SUITE_P(DISABLED_Arabic, MatchGroundTruth, ::testing::Values("script/Arabic"));
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
class EuroText : public QuickTest {};
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
TEST_F(EuroText, FastLatinOCR) {
|
2021-03-13 05:06:34 +08:00
|
|
|
OCRTester(TESTING_DIR "/eurotext.tif", TESTING_DIR "/eurotext.txt", TESSDATA_DIR "_fast",
|
|
|
|
"script/Latin");
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
2018-06-19 02:53:01 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
// script/Latin for eurotext.tif does not match groundtruth
|
|
|
|
// for tessdata & tessdata_best.
|
|
|
|
// so do not test these here.
|
2017-09-17 00:47:04 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|