2019-04-18 23:06:02 +08:00
|
|
|
// (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.
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <string> // std::string
|
2018-08-24 21:07:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2019-04-18 23:06:02 +08:00
|
|
|
#include "include_gunit.h"
|
|
|
|
#include "params_model.h"
|
2021-03-13 05:06:34 +08:00
|
|
|
#include "serialis.h" // TFile
|
|
|
|
#include "tprintf.h" // tprintf
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
// Test some basic I/O of params model files (automated learning of language
|
|
|
|
// model weights).
|
2019-05-17 00:07:32 +08:00
|
|
|
#ifndef DISABLED_LEGACY_ENGINE
|
2021-03-13 05:06:34 +08:00
|
|
|
static bool LoadFromFile(tesseract::ParamsModel &model, const char *lang, const char *full_path) {
|
2019-04-18 23:06:02 +08:00
|
|
|
tesseract::TFile fp;
|
|
|
|
if (!fp.Open(full_path, nullptr)) {
|
|
|
|
tprintf("Error opening file %s\n", full_path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return model.LoadFromFp(lang, &fp);
|
|
|
|
}
|
2019-05-17 00:07:32 +08:00
|
|
|
#endif
|
2019-04-18 23:06:02 +08:00
|
|
|
|
2018-08-24 21:07:48 +08:00
|
|
|
class ParamsModelTest : public testing::Test {
|
2019-05-17 00:07:32 +08:00
|
|
|
#ifndef DISABLED_LEGACY_ENGINE
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2019-05-17 00:07:32 +08:00
|
|
|
void SetUp() override {
|
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string TestDataNameToPath(const std::string &name) const {
|
2019-04-18 23:06:02 +08:00
|
|
|
return file::JoinPath(TESTDATA_DIR, name);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
std::string OutputNameToPath(const std::string &name) const {
|
2018-08-24 21:07:48 +08:00
|
|
|
return file::JoinPath(FLAGS_test_tmpdir, name);
|
|
|
|
}
|
|
|
|
// Test that we are able to load a params model, save it, reload it,
|
|
|
|
// and verify that the re-serialized version is the same as the original.
|
2021-03-13 05:06:34 +08:00
|
|
|
void TestParamsModelRoundTrip(const std::string ¶ms_model_filename) const {
|
2018-08-24 21:07:48 +08:00
|
|
|
tesseract::ParamsModel orig_model;
|
|
|
|
tesseract::ParamsModel duplicate_model;
|
2020-12-31 01:17:58 +08:00
|
|
|
file::MakeTmpdir();
|
2019-04-18 23:06:02 +08:00
|
|
|
std::string orig_file = TestDataNameToPath(params_model_filename);
|
|
|
|
std::string out_file = OutputNameToPath(params_model_filename);
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2019-04-18 23:06:02 +08:00
|
|
|
EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
|
2018-09-29 15:19:13 +08:00
|
|
|
|
2019-04-18 23:06:02 +08:00
|
|
|
EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
|
|
|
|
}
|
2019-05-17 00:07:32 +08:00
|
|
|
#endif
|
2018-08-24 21:07:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ParamsModelTest, TestEngParamsModelIO) {
|
2019-05-17 00:07:32 +08:00
|
|
|
#ifdef DISABLED_LEGACY_ENGINE
|
|
|
|
// Skip test because ParamsModel::LoadFromFp is missing.
|
|
|
|
GTEST_SKIP();
|
|
|
|
#else
|
2018-08-24 21:07:48 +08:00
|
|
|
TestParamsModelRoundTrip("eng.params_model");
|
2019-05-17 00:07:32 +08:00
|
|
|
#endif
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|