Skip to content

Commit

Permalink
Merge pull request #1479 from stweil/version
Browse files Browse the repository at this point in the history
training: Add initial support for --version argument and check library version
  • Loading branch information
zdenop committed Apr 15, 2018
2 parents 9f4f715 + a440bd8 commit a07ee5c
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 29 deletions.
4 changes: 4 additions & 0 deletions api/capi.h
Expand Up @@ -18,6 +18,10 @@
#ifndef API_CAPI_H_
#define API_CAPI_H_

#if defined(TESSERACT_API_BASEAPI_H_) && !defined(TESS_CAPI_INCLUDE_BASEAPI)
# define TESS_CAPI_INCLUDE_BASEAPI
#endif

#ifdef TESS_CAPI_INCLUDE_BASEAPI
# include "baseapi.h"
# include "pageiterator.h"
Expand Down
2 changes: 2 additions & 0 deletions training/ambiguous_words.cpp
Expand Up @@ -24,12 +24,14 @@
#include <stdio.h>

#include "baseapi.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "helpers.h"
#include "strngs.h"
#include "dict.h"
#include "tesseractclass.h"

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();

// Parse input arguments.
if (argc != 4 && (argc != 6 || strcmp(argv[1], "-l") != 0)) {
Expand Down
1 change: 1 addition & 0 deletions training/classifier_tester.cpp
Expand Up @@ -111,6 +111,7 @@ static tesseract::ShapeClassifier* InitializeClassifier(
// full : Tesseract full classifier.
// with an input trainer.)
int main(int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();
ParseArguments(&argc, &argv);
STRING file_prefix;
tesseract::MasterTrainer* trainer =
Expand Down
9 changes: 2 additions & 7 deletions training/cntraining.cpp
Expand Up @@ -41,13 +41,6 @@

DECLARE_STRING_PARAM_FLAG(D);

/*----------------------------------------------------------------------------
Public Function Prototypes
----------------------------------------------------------------------------*/
int main (
int argc,
char **argv);

/*----------------------------------------------------------------------------
Private Function Prototypes
----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -131,6 +124,8 @@ CLUSTERCONFIG CNConfig =
* @note History: Fri Aug 18 08:56:17 1989, DSJ, Created.
*/
int main(int argc, char *argv[]) {
tesseract::CheckSharedLibraryVersion();

// Set the global Config parameters before parsing the command line.
Config = CNConfig;

Expand Down
3 changes: 3 additions & 0 deletions training/combine_lang_model.cpp
Expand Up @@ -15,7 +15,9 @@
// 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.

#include "commandlineflags.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "lang_model_helpers.h"
#include "tprintf.h"
#include "unicharset_training_utils.h"
Expand All @@ -38,6 +40,7 @@ BOOL_PARAM_FLAG(pass_through_recoder, false,
" unicharset. Otherwise, potentially a compression of it");

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();
tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);

// Check validity of input flags.
Expand Down
3 changes: 3 additions & 0 deletions training/combine_tessdata.cpp
Expand Up @@ -18,6 +18,7 @@
//
///////////////////////////////////////////////////////////////////////

#include "commontraining.h" // CheckSharedLibraryVersion
#include "lstmrecognizer.h"
#include "tessdatamanager.h"

Expand Down Expand Up @@ -65,6 +66,8 @@
// components from tessdata/eng.traineddata.
//
int main(int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();

int i;
tesseract::TessdataManager tm;
if (argc == 2) {
Expand Down
9 changes: 6 additions & 3 deletions training/commandlineflags.cpp
Expand Up @@ -7,6 +7,8 @@
// 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.

#include "baseapi.h" // TessBaseAPI::Version
#include "commandlineflags.h"

#ifndef GOOGLE_TESSERACT
Expand Down Expand Up @@ -55,7 +57,6 @@ bool StringFlagExists(const char* flag_name, const char** value) {
return p != nullptr;
}


void SetIntFlagValue(const char* flag_name, const int32_t new_val) {
STRING full_flag_name("FLAGS_");
full_flag_name += flag_name;
Expand Down Expand Up @@ -149,14 +150,16 @@ void PrintCommandLineFlags() {
}
}


void ParseCommandLineFlags(const char* usage,
int* argc, char*** argv,
const bool remove_flags) {
if (*argc == 1) {
printf("USAGE: %s\n", usage);
PrintCommandLineFlags();
exit(0);
} else if (*argc > 1 && !strcmp((*argv)[1], "--version")) {
printf("%s\n", TessBaseAPI::Version());
exit(0);
}

unsigned int i = 1;
Expand All @@ -174,7 +177,7 @@ void ParseCommandLineFlags(const char* usage,
// If this is asking for usage, print the help message and abort.
if (!strcmp(current_arg, "help") ||
!strcmp(current_arg, "helpshort")) {
tprintf("USAGE: %s\n", usage);
printf("USAGE: %s\n", usage);
PrintCommandLineFlags();
exit(0);
}
Expand Down
2 changes: 2 additions & 0 deletions training/commontraining.cpp
Expand Up @@ -87,6 +87,8 @@ void ParseArguments(int* argc, char ***argv) {
STRING usage;
if (*argc) {
usage += (*argv)[0];
usage += " --version | ";
usage += (*argv)[0];
}
usage += " [.tr files ...]";
tesseract::ParseCommandLineFlags(usage.c_str(), argc, argv, true);
Expand Down
19 changes: 19 additions & 0 deletions training/commontraining.h
Expand Up @@ -14,6 +14,10 @@
#ifndef TESSERACT_TRAINING_COMMONTRAINING_H_
#define TESSERACT_TRAINING_COMMONTRAINING_H_

#ifdef HAVE_CONFIG_H
#include "config_auto.h"
#include "baseapi.h"
#endif
#include "cluster.h"
#include "commandlineflags.h"
#include "featdefs.h"
Expand Down Expand Up @@ -63,6 +67,21 @@ void ParseArguments(int* argc, char*** argv);

namespace tesseract {

// Check whether the shared tesseract library is the right one.
// This function must be inline because otherwise it would be part of
// the shared library, so it could not compare the versions.
static inline void CheckSharedLibraryVersion()
{
#ifdef HAVE_CONFIG_H
if (!!strcmp(TESSERACT_VERSION_STR, TessBaseAPI::Version())) {
tprintf("ERROR: shared library version mismatch (was %s, expected %s\n"
"Did you use a wrong shared tesseract library?\n",
TessBaseAPI::Version(), TESSERACT_VERSION_STR);
exit(1);
}
#endif
}

// Helper loads shape table from the given file.
ShapeTable* LoadShapeTable(const STRING& file_prefix);
// Helper to write the shape_table.
Expand Down
3 changes: 3 additions & 0 deletions training/dawg2wordlist.cpp
Expand Up @@ -17,6 +17,7 @@
//
///////////////////////////////////////////////////////////////////////

#include "commontraining.h" // CheckSharedLibraryVersion
#include "dawg.h"
#include "host.h"
#include "serialis.h"
Expand Down Expand Up @@ -72,6 +73,8 @@ int WriteDawgAsWordlist(const UNICHARSET &unicharset,
}

int main(int argc, char *argv[]) {
tesseract::CheckSharedLibraryVersion();

if (argc != 4) {
tprintf("Print all the words in a given dawg.\n");
tprintf("Usage: %s <unicharset> <dawgfile> <wordlistfile>\n",
Expand Down
1 change: 1 addition & 0 deletions training/lstmeval.cpp
Expand Up @@ -36,6 +36,7 @@ INT_PARAM_FLAG(verbosity, 1,
"Amount of diagnosting information to output (0-2).");

int main(int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();
ParseArguments(&argc, &argv);
if (FLAGS_model.empty()) {
tprintf("Must provide a --model!\n");
Expand Down
1 change: 1 addition & 0 deletions training/lstmtraining.cpp
Expand Up @@ -71,6 +71,7 @@ const int kNumPagesPerBatch = 100;
// The program iterates over the inputs, feeding the data to the network,
// until the error rate reaches a specified target or max_iterations is reached.
int main(int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();
ParseArguments(&argc, &argv);
// Purify the model name in case it is based on the network string.
if (FLAGS_model_output.empty()) {
Expand Down
4 changes: 3 additions & 1 deletion training/merge_unicharsets.cpp
Expand Up @@ -17,10 +17,12 @@
//
///////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include "commontraining.h" // CheckSharedLibraryVersion
#include "unicharset.h"

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();

// Print usage
if (argc < 4) {
printf("Usage: %s unicharset-in-1 ... unicharset-in-n unicharset-out\n",
Expand Down
10 changes: 2 additions & 8 deletions training/mftraining.cpp
Expand Up @@ -76,14 +76,6 @@ const int kMaxShapeLabelLength = 10;

DECLARE_STRING_PARAM_FLAG(test_ch);

/*----------------------------------------------------------------------------
Public Function Prototypes
----------------------------------------------------------------------------*/
int main (
int argc,
char **argv);


/*----------------------------------------------------------------------------
Public Code
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -236,6 +228,8 @@ static void SetupConfigMap(ShapeTable* shape_table, IndexMapBiDi* config_map) {
* @note History: Mon May 18 1998, Christy Russson, Revistion started.
*/
int main (int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();

ParseArguments(&argc, &argv);

ShapeTable* shape_table = nullptr;
Expand Down
6 changes: 2 additions & 4 deletions training/set_unicharset_properties.cpp
Expand Up @@ -12,11 +12,8 @@
// object, fills it with properties about the unichars it contains and writes
// the result back to a file.

#include <stdlib.h>
#include <string.h>
#include <string>

#include "commandlineflags.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "tprintf.h"
#include "unicharset_training_utils.h"

Expand All @@ -30,6 +27,7 @@ DECLARE_STRING_PARAM_FLAG(O);
DECLARE_STRING_PARAM_FLAG(X);

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();
tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);

// Check validity of input flags.
Expand Down
2 changes: 2 additions & 0 deletions training/shapeclustering.cpp
Expand Up @@ -45,6 +45,8 @@ STRING_PARAM_FLAG(canonical_class2, "", "Class to show ambigs for");
// Otherwise, if FLAGS_canonical_class1 is set, prints a table of font-wise
// cluster distances between FLAGS_canonical_class1 and FLAGS_canonical_class2.
int main(int argc, char **argv) {
tesseract::CheckSharedLibraryVersion();

ParseArguments(&argc, &argv);

STRING file_prefix;
Expand Down
6 changes: 4 additions & 2 deletions training/text2image.cpp
Expand Up @@ -40,6 +40,7 @@
#include "allheaders.h" // from leptonica
#include "boxchar.h"
#include "commandlineflags.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "degradeimage.h"
#include "errcode.h"
#include "fileio.h"
Expand Down Expand Up @@ -412,7 +413,7 @@ using tesseract::SpanUTF8NotWhitespace;
using tesseract::SpanUTF8Whitespace;
using tesseract::StringRenderer;

int Main() {
static int Main() {
if (FLAGS_list_available_fonts) {
const std::vector<std::string>& all_fonts = FontUtils::ListAvailableFonts();
for (unsigned int i = 0; i < all_fonts.size(); ++i) {
Expand Down Expand Up @@ -672,6 +673,7 @@ int Main() {
}

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();
tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
Main();
return Main();
}
5 changes: 3 additions & 2 deletions training/unicharset_extractor.cpp
Expand Up @@ -24,11 +24,11 @@
#include <cstdlib>
#include "boxread.h"
#include "commandlineflags.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "genericvector.h"
#include "lang_model_helpers.h"
#include "normstrngs.h"
#include "strngs.h"
#include "tprintf.h"
#include "unicharset.h"
#include "unicharset_training_utils.h"

Expand Down Expand Up @@ -61,7 +61,7 @@ static void AddStringsToUnicharset(const GenericVector<STRING>& strings,
}
}

int Main(int argc, char** argv) {
static int Main(int argc, char** argv) {
UNICHARSET unicharset;
// Load input files
for (int arg = 1; arg < argc; ++arg) {
Expand Down Expand Up @@ -95,6 +95,7 @@ int Main(int argc, char** argv) {
} // namespace tesseract

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();
if (argc > 1) {
tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
}
Expand Down
5 changes: 3 additions & 2 deletions training/wordlist2dawg.cpp
Expand Up @@ -20,9 +20,8 @@
// Given a file that contains a list of words (one word per line) this program
// generates the corresponding squished DAWG file.

#include <stdio.h>

#include "classify.h"
#include "commontraining.h" // CheckSharedLibraryVersion
#include "dawg.h"
#include "dict.h"
#include "emalloc.h"
Expand All @@ -32,6 +31,8 @@
#include "unicharset.h"

int main(int argc, char** argv) {
tesseract::CheckSharedLibraryVersion();

if (!(argc == 4 || (argc == 5 && strcmp(argv[1], "-t") == 0) ||
(argc == 6 && strcmp(argv[1], "-r") == 0))) {
printf("Usage: %s [-t | -r [reverse policy] ] word_list_file"
Expand Down

0 comments on commit a07ee5c

Please sign in to comment.