comparison mupdf-source/thirdparty/tesseract/src/training/lstmeval.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 ///////////////////////////////////////////////////////////////////////
2 // File: lstmeval.cpp
3 // Description: Evaluation program for LSTM-based networks.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2016, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 ///////////////////////////////////////////////////////////////////////
17
18 #include "commontraining.h"
19 #include "lstmtester.h"
20 #include "tprintf.h"
21
22 using namespace tesseract;
23
24 static STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)");
25 static STRING_PARAM_FLAG(traineddata, "",
26 "If model is a training checkpoint, then traineddata must "
27 "be the traineddata file that was given to the trainer");
28 static STRING_PARAM_FLAG(eval_listfile, "", "File listing sample files in lstmf training format.");
29 static INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.");
30 static INT_PARAM_FLAG(verbosity, 1, "Amount of diagnosting information to output (0-2).");
31
32 int main(int argc, char **argv) {
33 tesseract::CheckSharedLibraryVersion();
34 ParseArguments(&argc, &argv);
35 if (FLAGS_model.empty()) {
36 tprintf("Must provide a --model!\n");
37 return EXIT_FAILURE;
38 }
39 if (FLAGS_eval_listfile.empty()) {
40 tprintf("Must provide a --eval_listfile!\n");
41 return EXIT_FAILURE;
42 }
43 tesseract::TessdataManager mgr;
44 if (!mgr.Init(FLAGS_model.c_str())) {
45 if (FLAGS_traineddata.empty()) {
46 tprintf("Must supply --traineddata to eval a training checkpoint!\n");
47 return EXIT_FAILURE;
48 }
49 tprintf("%s is not a recognition model, trying training checkpoint...\n", FLAGS_model.c_str());
50 if (!mgr.Init(FLAGS_traineddata.c_str())) {
51 tprintf("Failed to load language model from %s!\n", FLAGS_traineddata.c_str());
52 return EXIT_FAILURE;
53 }
54 std::vector<char> model_data;
55 if (!tesseract::LoadDataFromFile(FLAGS_model.c_str(), &model_data)) {
56 tprintf("Failed to load model from: %s\n", FLAGS_model.c_str());
57 return EXIT_FAILURE;
58 }
59 mgr.OverwriteEntry(tesseract::TESSDATA_LSTM, &model_data[0], model_data.size());
60 }
61 tesseract::LSTMTester tester(static_cast<int64_t>(FLAGS_max_image_MB) * 1048576);
62 if (!tester.LoadAllEvalData(FLAGS_eval_listfile.c_str())) {
63 tprintf("Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str());
64 return EXIT_FAILURE;
65 }
66 double errs = 0.0;
67 std::string result = tester.RunEvalSync(0, &errs, mgr,
68 /*training_stage (irrelevant)*/ 0, FLAGS_verbosity);
69 tprintf("%s\n", result.c_str());
70 return EXIT_SUCCESS;
71 } /* main */