2009-07-11 10:03:09 +08:00
|
|
|
/**********************************************************************
|
2017-07-15 00:30:14 +08:00
|
|
|
* File: tesseractmain.cpp (Formerly tessedit.c)
|
|
|
|
* Description: Main program for merge of tess and editor.
|
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Tue Jan 07 15:21:46 GMT 1992
|
|
|
|
*
|
|
|
|
* (C) Copyright 1992, Hewlett-Packard Ltd.
|
|
|
|
** 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.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
// Include automatically generated configuration file if running autoconf
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
2011-08-30 14:34:41 +08:00
|
|
|
|
2014-01-10 09:38:00 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2009-07-11 10:03:09 +08:00
|
|
|
#include "allheaders.h"
|
2010-11-30 08:58:30 +08:00
|
|
|
#include "baseapi.h"
|
2014-01-10 01:58:55 +08:00
|
|
|
#include "basedir.h"
|
2017-05-13 17:34:40 +08:00
|
|
|
#include "dict.h"
|
2017-01-26 08:20:19 +08:00
|
|
|
#include "openclwrapper.h"
|
|
|
|
#include "osdetect.h"
|
2013-09-21 03:39:59 +08:00
|
|
|
#include "renderer.h"
|
2016-12-28 20:34:45 +08:00
|
|
|
#include "simddetect.h"
|
2010-11-30 08:58:30 +08:00
|
|
|
#include "strngs.h"
|
2012-03-05 06:27:16 +08:00
|
|
|
#include "tprintf.h"
|
2009-07-11 10:03:09 +08:00
|
|
|
|
2016-07-17 18:07:58 +08:00
|
|
|
#if defined(HAVE_TIFFIO_H) && defined(_WIN32)
|
2016-07-16 05:00:00 +08:00
|
|
|
|
|
|
|
#include <tiffio.h>
|
|
|
|
|
|
|
|
static void Win32WarningHandler(const char* module, const char* fmt,
|
|
|
|
va_list ap) {
|
2016-11-08 02:46:33 +08:00
|
|
|
if (module != NULL) {
|
|
|
|
fprintf(stderr, "%s: ", module);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Warning, ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, ".\n");
|
2016-07-16 05:00:00 +08:00
|
|
|
}
|
|
|
|
|
2016-07-17 18:07:58 +08:00
|
|
|
#endif /* HAVE_TIFFIO_H && _WIN32 */
|
2016-07-16 05:00:00 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
void PrintVersionInfo() {
|
2016-11-08 02:46:33 +08:00
|
|
|
char* versionStrP;
|
2012-03-05 06:27:16 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
printf("tesseract %s\n", tesseract::TessBaseAPI::Version());
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
versionStrP = getLeptonicaVersion();
|
|
|
|
printf(" %s\n", versionStrP);
|
|
|
|
lept_free(versionStrP);
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
versionStrP = getImagelibVersions();
|
|
|
|
printf(" %s\n", versionStrP);
|
|
|
|
lept_free(versionStrP);
|
2012-03-05 06:27:16 +08:00
|
|
|
|
2013-12-14 16:35:14 +08:00
|
|
|
#ifdef USE_OPENCL
|
2017-01-26 08:20:19 +08:00
|
|
|
cl_platform_id platform[4];
|
|
|
|
cl_uint num_platforms;
|
2013-12-14 16:35:14 +08:00
|
|
|
|
2017-01-26 08:20:19 +08:00
|
|
|
printf(" OpenCL info:\n");
|
|
|
|
if (clGetPlatformIDs(4, platform, &num_platforms) == CL_SUCCESS) {
|
|
|
|
printf(" Found %u platform(s).\n", num_platforms);
|
|
|
|
for (unsigned n = 0; n < num_platforms; n++) {
|
|
|
|
char info[256];
|
|
|
|
if (clGetPlatformInfo(platform[n], CL_PLATFORM_NAME, 256, info, 0) ==
|
|
|
|
CL_SUCCESS) {
|
|
|
|
printf(" Platform %u name: %s.\n", n + 1, info);
|
|
|
|
}
|
|
|
|
if (clGetPlatformInfo(platform[n], CL_PLATFORM_VERSION, 256, info, 0) ==
|
|
|
|
CL_SUCCESS) {
|
|
|
|
printf(" Version: %s.\n", info);
|
|
|
|
}
|
|
|
|
cl_device_id devices[2];
|
|
|
|
cl_uint num_devices;
|
|
|
|
if (clGetDeviceIDs(platform[n], CL_DEVICE_TYPE_ALL, 2, devices,
|
|
|
|
&num_devices) == CL_SUCCESS) {
|
|
|
|
printf(" Found %u device(s).\n", num_devices);
|
|
|
|
for (unsigned i = 0; i < num_devices; ++i) {
|
|
|
|
if (clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 256, info, 0) ==
|
|
|
|
CL_SUCCESS) {
|
|
|
|
printf(" Device %u name: %s.\n", i + 1, info);
|
2016-11-02 03:05:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-14 16:35:14 +08:00
|
|
|
}
|
2017-01-26 08:20:19 +08:00
|
|
|
}
|
2013-12-14 16:35:14 +08:00
|
|
|
#endif
|
2017-01-26 08:20:19 +08:00
|
|
|
if (SIMDDetect::IsAVXAvailable()) printf(" Found AVX\n");
|
|
|
|
if (SIMDDetect::IsSSEAvailable()) printf(" Found SSE\n");
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintUsage(const char* program) {
|
2016-03-17 19:10:02 +08:00
|
|
|
printf(
|
2015-11-26 17:36:16 +08:00
|
|
|
"Usage:\n"
|
2016-11-09 06:01:04 +08:00
|
|
|
" %s --help | --help-psm | --help-oem | --version\n"
|
2015-11-26 17:36:16 +08:00
|
|
|
" %s --list-langs [--tessdata-dir PATH]\n"
|
|
|
|
" %s --print-parameters [options...] [configfile...]\n"
|
2015-11-29 17:26:40 +08:00
|
|
|
" %s imagename|stdin outputbase|stdout [options...] [configfile...]\n",
|
2015-11-26 17:36:16 +08:00
|
|
|
program, program, program, program);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintHelpForPSM() {
|
2015-11-29 17:26:40 +08:00
|
|
|
const char* msg =
|
|
|
|
"Page segmentation modes:\n"
|
2016-11-08 02:46:33 +08:00
|
|
|
" 0 Orientation and script detection (OSD) only.\n"
|
|
|
|
" 1 Automatic page segmentation with OSD.\n"
|
|
|
|
" 2 Automatic page segmentation, but no OSD, or OCR.\n"
|
|
|
|
" 3 Fully automatic page segmentation, but no OSD. (Default)\n"
|
|
|
|
" 4 Assume a single column of text of variable sizes.\n"
|
|
|
|
" 5 Assume a single uniform block of vertically aligned text.\n"
|
|
|
|
" 6 Assume a single uniform block of text.\n"
|
|
|
|
" 7 Treat the image as a single text line.\n"
|
|
|
|
" 8 Treat the image as a single word.\n"
|
|
|
|
" 9 Treat the image as a single word in a circle.\n"
|
|
|
|
" 10 Treat the image as a single character.\n"
|
|
|
|
" 11 Sparse text. Find as much text as possible in no"
|
|
|
|
" particular order.\n"
|
|
|
|
" 12 Sparse text with OSD.\n"
|
|
|
|
" 13 Raw line. Treat the image as a single text line,\n"
|
|
|
|
"\t\t\tbypassing hacks that are Tesseract-specific.\n";
|
2015-11-26 17:36:16 +08:00
|
|
|
|
2016-03-17 19:10:02 +08:00
|
|
|
printf("%s", msg);
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 06:01:04 +08:00
|
|
|
void PrintHelpForOEM() {
|
|
|
|
const char* msg =
|
|
|
|
"OCR Engine modes:\n"
|
|
|
|
" 0 Original Tesseract only.\n"
|
2016-12-06 06:41:43 +08:00
|
|
|
" 1 Neural nets LSTM only.\n"
|
|
|
|
" 2 Tesseract + LSTM.\n"
|
|
|
|
" 3 Default, based on what is available.\n";
|
2016-11-09 06:01:04 +08:00
|
|
|
|
|
|
|
printf("%s", msg);
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
void PrintHelpMessage(const char* program) {
|
|
|
|
PrintUsage(program);
|
|
|
|
|
2015-11-29 17:26:40 +08:00
|
|
|
const char* ocr_options =
|
|
|
|
"OCR options:\n"
|
|
|
|
" --tessdata-dir PATH Specify the location of tessdata path.\n"
|
|
|
|
" --user-words PATH Specify the location of user words file.\n"
|
|
|
|
" --user-patterns PATH Specify the location of user patterns file.\n"
|
|
|
|
" -l LANG[+LANG] Specify language(s) used for OCR.\n"
|
|
|
|
" -c VAR=VALUE Set value for config variables.\n"
|
|
|
|
" Multiple -c arguments are allowed.\n"
|
2016-12-01 05:23:08 +08:00
|
|
|
" --psm NUM Specify page segmentation mode.\n"
|
2016-12-01 05:14:23 +08:00
|
|
|
" --oem NUM Specify OCR Engine mode.\n"
|
2016-11-08 02:46:33 +08:00
|
|
|
"NOTE: These options must occur before any configfile.\n";
|
2015-11-26 17:36:16 +08:00
|
|
|
|
2016-03-17 19:10:02 +08:00
|
|
|
printf("\n%s\n", ocr_options);
|
2015-11-26 17:36:16 +08:00
|
|
|
PrintHelpForPSM();
|
2016-11-09 06:01:04 +08:00
|
|
|
PrintHelpForOEM();
|
2015-11-29 17:26:40 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
const char* single_options =
|
2015-11-29 17:26:40 +08:00
|
|
|
"Single options:\n"
|
|
|
|
" -h, --help Show this help message.\n"
|
|
|
|
" --help-psm Show page segmentation modes.\n"
|
2016-11-09 06:01:04 +08:00
|
|
|
" --help-oem Show OCR Engine modes.\n"
|
2015-11-29 17:26:40 +08:00
|
|
|
" -v, --version Show version information.\n"
|
|
|
|
" --list-langs List available languages for tesseract engine.\n"
|
2017-03-24 04:02:50 +08:00
|
|
|
" --print-parameters Print tesseract parameters.\n";
|
2015-11-29 17:26:40 +08:00
|
|
|
|
2016-03-17 19:10:02 +08:00
|
|
|
printf("\n%s", single_options);
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
void SetVariablesFromCLArgs(tesseract::TessBaseAPI* api, int argc,
|
|
|
|
char** argv) {
|
2015-11-26 17:36:16 +08:00
|
|
|
char opt1[256], opt2[255];
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
|
|
|
|
strncpy(opt1, argv[i + 1], 255);
|
|
|
|
opt1[255] = '\0';
|
2016-11-08 02:46:33 +08:00
|
|
|
char* p = strchr(opt1, '=');
|
2015-11-26 17:36:16 +08:00
|
|
|
if (!p) {
|
|
|
|
fprintf(stderr, "Missing = in configvar assignment\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
strncpy(opt2, strchr(argv[i + 1], '=') + 1, 255);
|
|
|
|
opt2[254] = 0;
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (!api->SetVariable(opt1, opt2)) {
|
|
|
|
fprintf(stderr, "Could not set option: %s=%s\n", opt1, opt2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintLangsList(tesseract::TessBaseAPI* api) {
|
|
|
|
GenericVector<STRING> languages;
|
|
|
|
api->GetAvailableLanguagesAsVector(&languages);
|
2016-03-17 19:10:02 +08:00
|
|
|
printf("List of available languages (%d):\n", languages.size());
|
2015-11-26 17:36:16 +08:00
|
|
|
for (int index = 0; index < languages.size(); ++index) {
|
|
|
|
STRING& string = languages[index];
|
2016-03-17 19:10:02 +08:00
|
|
|
printf("%s\n", string.string());
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
api->End();
|
|
|
|
}
|
|
|
|
|
2016-03-08 01:25:09 +08:00
|
|
|
void PrintBanner() {
|
2016-11-08 02:46:33 +08:00
|
|
|
tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n",
|
|
|
|
tesseract::TessBaseAPI::Version());
|
2016-03-08 01:25:09 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
/**
|
|
|
|
* We have 2 possible sources of pagesegmode: a config file and
|
|
|
|
* the command line. For backwards compatibility reasons, the
|
2015-12-12 01:06:49 +08:00
|
|
|
* default in tesseract is tesseract::PSM_SINGLE_BLOCK, but the
|
|
|
|
* default for this program is tesseract::PSM_AUTO. We will let
|
|
|
|
* the config file take priority, so the command-line default
|
|
|
|
* can take priority over the tesseract default, so we use the
|
|
|
|
* value from the command line only if the retrieved mode
|
|
|
|
* is still tesseract::PSM_SINGLE_BLOCK, indicating no change
|
|
|
|
* in any config file. Therefore the only way to force
|
|
|
|
* tesseract::PSM_SINGLE_BLOCK is from the command line.
|
|
|
|
* It would be simpler if we could set the value before Init,
|
|
|
|
* but that doesn't work.
|
2015-11-26 17:36:16 +08:00
|
|
|
*/
|
2015-11-29 17:26:40 +08:00
|
|
|
void FixPageSegMode(tesseract::TessBaseAPI* api,
|
2016-11-08 02:46:33 +08:00
|
|
|
tesseract::PageSegMode pagesegmode) {
|
2015-11-26 17:36:16 +08:00
|
|
|
if (api->GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK)
|
2016-11-08 02:46:33 +08:00
|
|
|
api->SetPageSegMode(pagesegmode);
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: arg_i is used here to avoid ugly *i so many times in this function
|
2016-11-08 02:46:33 +08:00
|
|
|
void ParseArgs(const int argc, char** argv, const char** lang,
|
|
|
|
const char** image, const char** outputbase,
|
|
|
|
const char** datapath, bool* list_langs, bool* print_parameters,
|
|
|
|
GenericVector<STRING>* vars_vec,
|
|
|
|
GenericVector<STRING>* vars_values, int* arg_i,
|
2016-11-09 06:01:04 +08:00
|
|
|
tesseract::PageSegMode* pagesegmode,
|
|
|
|
tesseract::OcrEngineMode* enginemode) {
|
2015-11-26 17:36:16 +08:00
|
|
|
if (argc == 1) {
|
|
|
|
PrintHelpMessage(argv[0]);
|
2011-07-30 05:55:49 +08:00
|
|
|
exit(0);
|
2010-11-30 08:58:30 +08:00
|
|
|
}
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
if (argc == 2) {
|
2016-11-08 02:46:33 +08:00
|
|
|
if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) {
|
2015-11-26 17:36:16 +08:00
|
|
|
PrintHelpMessage(argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if ((strcmp(argv[1], "--help-psm") == 0)) {
|
|
|
|
PrintHelpForPSM();
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-11-09 06:01:04 +08:00
|
|
|
if ((strcmp(argv[1], "--help-oem") == 0)) {
|
|
|
|
PrintHelpForOEM();
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-11-08 02:46:33 +08:00
|
|
|
if ((strcmp(argv[1], "-v") == 0) || (strcmp(argv[1], "--version") == 0)) {
|
2015-11-26 17:36:16 +08:00
|
|
|
PrintVersionInfo();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
bool noocr = false;
|
|
|
|
int i = 1;
|
|
|
|
while (i < argc && (*outputbase == NULL || argv[i][0] == '-')) {
|
|
|
|
if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
|
|
|
|
*lang = argv[i + 1];
|
|
|
|
++i;
|
|
|
|
} else if (strcmp(argv[i], "--tessdata-dir") == 0 && i + 1 < argc) {
|
|
|
|
*datapath = argv[i + 1];
|
|
|
|
++i;
|
|
|
|
} else if (strcmp(argv[i], "--user-words") == 0 && i + 1 < argc) {
|
|
|
|
vars_vec->push_back("user_words_file");
|
|
|
|
vars_values->push_back(argv[i + 1]);
|
|
|
|
++i;
|
|
|
|
} else if (strcmp(argv[i], "--user-patterns") == 0 && i + 1 < argc) {
|
|
|
|
vars_vec->push_back("user_patterns_file");
|
|
|
|
vars_values->push_back(argv[i + 1]);
|
|
|
|
++i;
|
|
|
|
} else if (strcmp(argv[i], "--list-langs") == 0) {
|
2013-11-11 04:59:11 +08:00
|
|
|
noocr = true;
|
2015-11-26 17:36:16 +08:00
|
|
|
*list_langs = true;
|
|
|
|
} else if (strcmp(argv[i], "-psm") == 0 && i + 1 < argc) {
|
2016-12-01 05:23:08 +08:00
|
|
|
// The parameter -psm is deprecated and was replaced by --psm.
|
|
|
|
// It is still supported for compatibility reasons.
|
|
|
|
*pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[i + 1]));
|
|
|
|
++i;
|
|
|
|
} else if (strcmp(argv[i], "--psm") == 0 && i + 1 < argc) {
|
2015-11-26 17:36:16 +08:00
|
|
|
*pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[i + 1]));
|
|
|
|
++i;
|
2016-12-01 05:14:23 +08:00
|
|
|
} else if (strcmp(argv[i], "--oem") == 0 && i + 1 < argc) {
|
2016-11-09 06:01:04 +08:00
|
|
|
*enginemode = static_cast<tesseract::OcrEngineMode>(atoi(argv[i + 1]));
|
|
|
|
++i;
|
2015-11-26 17:36:16 +08:00
|
|
|
} else if (strcmp(argv[i], "--print-parameters") == 0) {
|
2012-12-24 01:54:14 +08:00
|
|
|
noocr = true;
|
2015-11-26 17:36:16 +08:00
|
|
|
*print_parameters = true;
|
|
|
|
} else if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
|
2013-04-30 04:43:14 +08:00
|
|
|
// handled properly after api init
|
2015-11-26 17:36:16 +08:00
|
|
|
++i;
|
|
|
|
} else if (*image == NULL) {
|
|
|
|
*image = argv[i];
|
|
|
|
} else if (*outputbase == NULL) {
|
|
|
|
*outputbase = argv[i];
|
2010-11-30 08:58:30 +08:00
|
|
|
}
|
2015-11-26 17:36:16 +08:00
|
|
|
++i;
|
2010-11-30 08:58:30 +08:00
|
|
|
}
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
*arg_i = i;
|
|
|
|
|
2012-12-24 01:47:06 +08:00
|
|
|
if (argc == 2 && strcmp(argv[1], "--list-langs") == 0) {
|
2015-11-26 17:36:16 +08:00
|
|
|
*list_langs = true;
|
2012-12-24 01:47:06 +08:00
|
|
|
noocr = true;
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
if (*outputbase == NULL && noocr == false) {
|
|
|
|
PrintHelpMessage(argv[0]);
|
2010-11-30 08:58:30 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
void PreloadRenderers(
|
|
|
|
tesseract::TessBaseAPI* api,
|
|
|
|
tesseract::PointerVector<tesseract::TessResultRenderer>* renderers,
|
|
|
|
tesseract::PageSegMode pagesegmode, const char* outputbase) {
|
2015-11-26 17:36:16 +08:00
|
|
|
if (pagesegmode == tesseract::PSM_OSD_ONLY) {
|
|
|
|
renderers->push_back(new tesseract::TessOsdRenderer(outputbase));
|
|
|
|
} else {
|
|
|
|
bool b;
|
|
|
|
api->GetBoolVariable("tessedit_create_hocr", &b);
|
|
|
|
if (b) {
|
|
|
|
bool font_info;
|
|
|
|
api->GetBoolVariable("hocr_font_info", &font_info);
|
2015-12-12 01:06:49 +08:00
|
|
|
renderers->push_back(
|
2016-11-08 02:46:33 +08:00
|
|
|
new tesseract::TessHOcrRenderer(outputbase, font_info));
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
2015-12-12 01:06:49 +08:00
|
|
|
|
2016-03-02 02:41:19 +08:00
|
|
|
api->GetBoolVariable("tessedit_create_tsv", &b);
|
2014-08-19 14:33:53 +08:00
|
|
|
if (b) {
|
|
|
|
bool font_info;
|
|
|
|
api->GetBoolVariable("hocr_font_info", &font_info);
|
|
|
|
renderers->push_back(
|
2016-03-02 02:41:19 +08:00
|
|
|
new tesseract::TessTsvRenderer(outputbase, font_info));
|
2014-08-19 14:33:53 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
api->GetBoolVariable("tessedit_create_pdf", &b);
|
|
|
|
if (b) {
|
2017-01-21 04:26:34 +08:00
|
|
|
bool textonly;
|
|
|
|
api->GetBoolVariable("textonly_pdf", &textonly);
|
|
|
|
renderers->push_back(new tesseract::TessPDFRenderer(
|
|
|
|
outputbase, api->GetDatapath(), textonly));
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
2015-12-12 01:06:49 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
api->GetBoolVariable("tessedit_write_unlv", &b);
|
2015-12-12 01:06:49 +08:00
|
|
|
if (b) {
|
|
|
|
renderers->push_back(new tesseract::TessUnlvRenderer(outputbase));
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
api->GetBoolVariable("tessedit_create_boxfile", &b);
|
2015-12-12 01:06:49 +08:00
|
|
|
if (b) {
|
|
|
|
renderers->push_back(new tesseract::TessBoxTextRenderer(outputbase));
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
api->GetBoolVariable("tessedit_create_txt", &b);
|
2016-01-29 09:37:34 +08:00
|
|
|
if (b || renderers->empty()) {
|
2015-12-12 01:06:49 +08:00
|
|
|
renderers->push_back(new tesseract::TessTextRenderer(outputbase));
|
|
|
|
}
|
2015-11-26 17:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderers->empty()) {
|
|
|
|
// Since the PointerVector auto-deletes, null-out the renderers that are
|
|
|
|
// added to the root, and leave the root in the vector.
|
|
|
|
for (int r = 1; r < renderers->size(); ++r) {
|
|
|
|
(*renderers)[0]->insert((*renderers)[r]);
|
|
|
|
(*renderers)[r] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* main()
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2016-10-25 02:18:39 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
int main(int argc, char** argv) {
|
2015-11-26 17:36:16 +08:00
|
|
|
const char* lang = "eng";
|
|
|
|
const char* image = NULL;
|
|
|
|
const char* outputbase = NULL;
|
|
|
|
const char* datapath = NULL;
|
|
|
|
bool list_langs = false;
|
|
|
|
bool print_parameters = false;
|
|
|
|
int arg_i = 1;
|
|
|
|
tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;
|
2016-11-09 06:01:04 +08:00
|
|
|
tesseract::OcrEngineMode enginemode = tesseract::OEM_DEFAULT;
|
2016-10-25 02:18:39 +08:00
|
|
|
/* main() calls functions like ParseArgs which call exit().
|
|
|
|
* This results in memory leaks if vars_vec and vars_values are
|
|
|
|
* declared as auto variables (destructor is not called then). */
|
|
|
|
static GenericVector<STRING> vars_vec;
|
|
|
|
static GenericVector<STRING> vars_values;
|
2015-11-26 17:36:16 +08:00
|
|
|
|
2017-07-15 00:30:14 +08:00
|
|
|
#if !defined(DEBUG)
|
2016-12-05 03:04:10 +08:00
|
|
|
// Disable debugging and informational messages from Leptonica.
|
2017-01-26 08:05:59 +08:00
|
|
|
setMsgSeverity(L_SEVERITY_ERROR);
|
2016-12-05 03:04:10 +08:00
|
|
|
#endif
|
|
|
|
|
2016-07-17 18:07:58 +08:00
|
|
|
#if defined(HAVE_TIFFIO_H) && defined(_WIN32)
|
2016-07-16 05:00:00 +08:00
|
|
|
/* Show libtiff warnings on console (not in GUI). */
|
|
|
|
TIFFSetWarningHandler(Win32WarningHandler);
|
2016-07-17 18:07:58 +08:00
|
|
|
#endif /* HAVE_TIFFIO_H && _WIN32 */
|
2016-07-16 05:00:00 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
ParseArgs(argc, argv, &lang, &image, &outputbase, &datapath, &list_langs,
|
2016-11-09 06:01:04 +08:00
|
|
|
&print_parameters, &vars_vec, &vars_values, &arg_i, &pagesegmode,
|
|
|
|
&enginemode);
|
2015-11-26 17:36:16 +08:00
|
|
|
|
2016-03-08 01:25:09 +08:00
|
|
|
bool banner = false;
|
|
|
|
if (outputbase != NULL && strcmp(outputbase, "-") &&
|
|
|
|
strcmp(outputbase, "stdout")) {
|
|
|
|
banner = true;
|
|
|
|
}
|
|
|
|
|
2013-12-23 05:58:52 +08:00
|
|
|
PERF_COUNT_START("Tesseract:main")
|
2017-05-13 03:15:12 +08:00
|
|
|
|
2017-05-13 17:34:40 +08:00
|
|
|
// Call GlobalDawgCache here to create the global DawgCache object before
|
|
|
|
// the TessBaseAPI object. This fixes the order of destructor calls:
|
|
|
|
// first TessBaseAPI must be destructed, DawgCache must be the last object.
|
|
|
|
tesseract::Dict::GlobalDawgCache();
|
|
|
|
|
2017-07-15 00:30:14 +08:00
|
|
|
// Avoid memory leak caused by auto variable when return is called.
|
2017-05-13 03:15:12 +08:00
|
|
|
static tesseract::TessBaseAPI api;
|
2010-11-30 08:58:30 +08:00
|
|
|
|
2014-05-21 23:46:21 +08:00
|
|
|
api.SetOutputName(outputbase);
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2016-11-09 06:01:04 +08:00
|
|
|
int init_failed = api.Init(datapath, lang, enginemode, &(argv[arg_i]),
|
|
|
|
argc - arg_i, &vars_vec, &vars_values, false);
|
2015-11-26 17:36:16 +08:00
|
|
|
if (init_failed) {
|
2013-07-08 00:39:13 +08:00
|
|
|
fprintf(stderr, "Could not initialize tesseract.\n");
|
2017-05-13 14:42:43 +08:00
|
|
|
return EXIT_FAILURE;
|
2012-02-26 22:48:35 +08:00
|
|
|
}
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
SetVariablesFromCLArgs(&api, argc, argv);
|
2013-04-30 04:43:14 +08:00
|
|
|
|
2012-12-24 01:47:06 +08:00
|
|
|
if (list_langs) {
|
2016-11-08 02:46:33 +08:00
|
|
|
PrintLangsList(&api);
|
2017-05-13 14:42:43 +08:00
|
|
|
return EXIT_SUCCESS;
|
2012-12-24 01:47:06 +08:00
|
|
|
}
|
2012-12-24 01:54:14 +08:00
|
|
|
|
|
|
|
if (print_parameters) {
|
|
|
|
FILE* fout = stdout;
|
2013-07-08 00:39:13 +08:00
|
|
|
fprintf(stdout, "Tesseract parameters:\n");
|
2012-12-24 01:54:14 +08:00
|
|
|
api.PrintVariables(fout);
|
|
|
|
api.End();
|
2017-05-13 14:42:43 +08:00
|
|
|
return EXIT_SUCCESS;
|
2012-12-24 01:54:14 +08:00
|
|
|
}
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2015-11-26 17:36:16 +08:00
|
|
|
FixPageSegMode(&api, pagesegmode);
|
2011-08-19 00:41:51 +08:00
|
|
|
|
2015-10-31 02:09:00 +08:00
|
|
|
if (pagesegmode == tesseract::PSM_AUTO_ONLY) {
|
2017-05-13 14:42:43 +08:00
|
|
|
int ret_val = EXIT_SUCCESS;
|
2014-01-12 22:47:15 +08:00
|
|
|
|
2014-05-21 23:46:21 +08:00
|
|
|
Pix* pixs = pixRead(image);
|
2014-02-01 20:56:36 +08:00
|
|
|
if (!pixs) {
|
|
|
|
fprintf(stderr, "Cannot open input file: %s\n", image);
|
2017-05-13 14:42:43 +08:00
|
|
|
return 2;
|
2014-02-01 20:56:36 +08:00
|
|
|
}
|
2015-10-31 02:09:00 +08:00
|
|
|
|
2014-01-12 22:47:15 +08:00
|
|
|
api.SetImage(pixs);
|
2014-02-01 20:56:36 +08:00
|
|
|
|
2015-10-31 02:09:00 +08:00
|
|
|
tesseract::Orientation orientation;
|
|
|
|
tesseract::WritingDirection direction;
|
|
|
|
tesseract::TextlineOrder order;
|
|
|
|
float deskew_angle;
|
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
tesseract::PageIterator* it = api.AnalyseLayout();
|
2015-10-31 02:09:00 +08:00
|
|
|
if (it) {
|
|
|
|
it->Orientation(&orientation, &direction, &order, &deskew_angle);
|
2016-11-08 02:46:33 +08:00
|
|
|
tprintf(
|
|
|
|
"Orientation: %d\nWritingDirection: %d\nTextlineOrder: %d\n"
|
|
|
|
"Deskew angle: %.4f\n",
|
|
|
|
orientation, direction, order, deskew_angle);
|
2014-01-12 22:47:15 +08:00
|
|
|
} else {
|
2017-05-13 14:42:43 +08:00
|
|
|
ret_val = EXIT_FAILURE;
|
2014-01-12 22:47:15 +08:00
|
|
|
}
|
2015-11-29 17:26:40 +08:00
|
|
|
|
2015-10-31 02:09:00 +08:00
|
|
|
delete it;
|
2015-11-29 17:26:40 +08:00
|
|
|
|
2014-01-12 22:47:15 +08:00
|
|
|
pixDestroy(&pixs);
|
2017-05-13 14:42:43 +08:00
|
|
|
return ret_val;
|
2014-01-12 22:47:15 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 20:27:35 +08:00
|
|
|
// set in_training_mode to true when using one of these configs:
|
|
|
|
// ambigs.train, box.train, box.train.stderr, linebox, rebox
|
|
|
|
bool b = false;
|
|
|
|
bool in_training_mode =
|
2016-11-08 02:46:33 +08:00
|
|
|
(api.GetBoolVariable("tessedit_ambigs_training", &b) && b) ||
|
|
|
|
(api.GetBoolVariable("tessedit_resegment_from_boxes", &b) && b) ||
|
|
|
|
(api.GetBoolVariable("tessedit_make_boxes_from_boxes", &b) && b);
|
2016-01-17 20:27:35 +08:00
|
|
|
|
2017-05-13 03:15:12 +08:00
|
|
|
// Avoid memory leak caused by auto variable when exit() is called.
|
|
|
|
static tesseract::PointerVector<tesseract::TessResultRenderer> renderers;
|
2016-01-17 20:27:35 +08:00
|
|
|
|
2016-01-29 09:37:34 +08:00
|
|
|
if (in_training_mode) {
|
|
|
|
renderers.push_back(NULL);
|
|
|
|
} else {
|
|
|
|
PreloadRenderers(&api, &renderers, pagesegmode, outputbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderers.empty()) {
|
2016-03-08 01:25:09 +08:00
|
|
|
if (banner) PrintBanner();
|
2015-11-26 17:36:16 +08:00
|
|
|
bool succeed = api.ProcessPages(image, NULL, 0, renderers[0]);
|
|
|
|
if (!succeed) {
|
2014-10-10 04:29:46 +08:00
|
|
|
fprintf(stderr, "Error during processing.\n");
|
2017-05-13 14:42:43 +08:00
|
|
|
return EXIT_FAILURE;
|
2014-10-10 04:29:46 +08:00
|
|
|
}
|
2014-09-20 05:33:47 +08:00
|
|
|
}
|
2016-01-29 09:37:34 +08:00
|
|
|
|
2013-12-10 18:52:54 +08:00
|
|
|
PERF_COUNT_END
|
2017-05-13 14:42:43 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2009-07-11 10:03:09 +08:00
|
|
|
}
|