Skip to content

Commit

Permalink
Added extra Init that takes a memory buffer or a filereader function …
Browse files Browse the repository at this point in the history
…pointer to enable read of traineddata from memory or foreign file systems. Updated existing readers to use TFile API instead of FILE. This does not yet add big-endian capability to LSTM, but it is very easy from here.
  • Loading branch information
theraysmith committed Apr 27, 2017
1 parent 10e04ff commit 1cc5111
Show file tree
Hide file tree
Showing 48 changed files with 825 additions and 1,191 deletions.
104 changes: 65 additions & 39 deletions api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,30 @@ const int kMinCredibleResolution = 70;
const int kMaxCredibleResolution = 2400;

TessBaseAPI::TessBaseAPI()
: tesseract_(NULL),
osd_tesseract_(NULL),
equ_detect_(NULL),
// Thresholder is initialized to NULL here, but will be set before use by:
// A constructor of a derived API, SetThresholder(), or
// created implicitly when used in InternalSetImage.
thresholder_(NULL),
paragraph_models_(NULL),
block_list_(NULL),
page_res_(NULL),
input_file_(NULL),
output_file_(NULL),
datapath_(NULL),
language_(NULL),
last_oem_requested_(OEM_DEFAULT),
recognition_done_(false),
truth_cb_(NULL),
rect_left_(0), rect_top_(0), rect_width_(0), rect_height_(0),
image_width_(0), image_height_(0) {
}
: tesseract_(nullptr),
osd_tesseract_(nullptr),
equ_detect_(nullptr),
reader_(nullptr),
// Thresholder is initialized to NULL here, but will be set before use by:
// A constructor of a derived API, SetThresholder(), or
// created implicitly when used in InternalSetImage.
thresholder_(nullptr),
paragraph_models_(nullptr),
block_list_(nullptr),
page_res_(nullptr),
input_file_(nullptr),
output_file_(nullptr),
datapath_(nullptr),
language_(nullptr),
last_oem_requested_(OEM_DEFAULT),
recognition_done_(false),
truth_cb_(NULL),
rect_left_(0),
rect_top_(0),
rect_width_(0),
rect_height_(0),
image_width_(0),
image_height_(0) {}

TessBaseAPI::~TessBaseAPI() {
End();
Expand Down Expand Up @@ -275,20 +279,33 @@ int TessBaseAPI::Init(const char* datapath, const char* language,
const GenericVector<STRING> *vars_vec,
const GenericVector<STRING> *vars_values,
bool set_only_non_debug_params) {
return Init(datapath, 0, language, oem, configs, configs_size, vars_vec,
vars_values, set_only_non_debug_params, nullptr);
}

// In-memory version reads the traineddata file directly from the given
// data[data_size] array. Also implements the version with a datapath in data,
// flagged by data_size = 0.
int TessBaseAPI::Init(const char* data, int data_size, const char* language,
OcrEngineMode oem, char** configs, int configs_size,
const GenericVector<STRING>* vars_vec,
const GenericVector<STRING>* vars_values,
bool set_only_non_debug_params, FileReader reader) {
PERF_COUNT_START("TessBaseAPI::Init")
// Default language is "eng".
if (language == NULL) language = "eng";
if (language == nullptr) language = "eng";
STRING datapath = data_size == 0 ? data : language;
// If the datapath, OcrEngineMode or the language have changed - start again.
// Note that the language_ field stores the last requested language that was
// initialized successfully, while tesseract_->lang stores the language
// actually used. They differ only if the requested language was NULL, in
// which case tesseract_->lang is set to the Tesseract default ("eng").
if (tesseract_ != NULL &&
(datapath_ == NULL || language_ == NULL ||
*datapath_ != datapath || last_oem_requested_ != oem ||
if (tesseract_ != nullptr &&
(datapath_ == nullptr || language_ == nullptr || *datapath_ != datapath ||
last_oem_requested_ != oem ||
(*language_ != language && tesseract_->lang != language))) {
delete tesseract_;
tesseract_ = NULL;
tesseract_ = nullptr;
}
// PERF_COUNT_SUB("delete tesseract_")
#ifdef USE_OPENCL
Expand All @@ -297,27 +314,33 @@ int TessBaseAPI::Init(const char* datapath, const char* language,
#endif
PERF_COUNT_SUB("OD::InitEnv()")
bool reset_classifier = true;
if (tesseract_ == NULL) {
if (tesseract_ == nullptr) {
reset_classifier = false;
tesseract_ = new Tesseract;
if (reader != nullptr) reader_ = reader;
TessdataManager mgr(reader_);
if (data_size != 0) {
mgr.LoadMemBuffer(language, data, data_size);
}
if (tesseract_->init_tesseract(
datapath, output_file_ != NULL ? output_file_->string() : NULL,
language, oem, configs, configs_size, vars_vec, vars_values,
set_only_non_debug_params) != 0) {
datapath.string(),
output_file_ != nullptr ? output_file_->string() : nullptr,
language, oem, configs, configs_size, vars_vec, vars_values,
set_only_non_debug_params, &mgr) != 0) {
return -1;
}
}
PERF_COUNT_SUB("update tesseract_")
// Update datapath and language requested for the last valid initialization.
if (datapath_ == NULL)
if (datapath_ == nullptr)
datapath_ = new STRING(datapath);
else
*datapath_ = datapath;
if ((strcmp(datapath_->string(), "") == 0) &&
(strcmp(tesseract_->datadir.string(), "") != 0))
*datapath_ = tesseract_->datadir;

if (language_ == NULL)
if (language_ == nullptr)
language_ = new STRING(language);
else
*language_ = language;
Expand Down Expand Up @@ -421,7 +444,8 @@ int TessBaseAPI::InitLangMod(const char* datapath, const char* language) {
tesseract_ = new Tesseract;
else
ParamUtils::ResetToDefaults(tesseract_->params());
return tesseract_->init_tesseract_lm(datapath, NULL, language);
TessdataManager mgr;
return tesseract_->init_tesseract_lm(datapath, NULL, language, &mgr);
}

/**
Expand All @@ -431,7 +455,7 @@ int TessBaseAPI::InitLangMod(const char* datapath, const char* language) {
void TessBaseAPI::InitForAnalysePage() {
if (tesseract_ == NULL) {
tesseract_ = new Tesseract;
tesseract_->InitAdaptiveClassifier(false);
tesseract_->InitAdaptiveClassifier(nullptr);
}
}

Expand Down Expand Up @@ -2239,7 +2263,7 @@ int TessBaseAPI::FindLines() {
}
if (tesseract_ == NULL) {
tesseract_ = new Tesseract;
tesseract_->InitAdaptiveClassifier(false);
tesseract_->InitAdaptiveClassifier(nullptr);
}
if (tesseract_->pix_binary() == NULL)
Threshold(tesseract_->mutable_pix_binary());
Expand All @@ -2261,22 +2285,24 @@ int TessBaseAPI::FindLines() {

Tesseract* osd_tess = osd_tesseract_;
OSResults osr;
if (PSM_OSD_ENABLED(tesseract_->tessedit_pageseg_mode) && osd_tess == NULL) {
if (PSM_OSD_ENABLED(tesseract_->tessedit_pageseg_mode) &&
osd_tess == nullptr) {
if (strcmp(language_->string(), "osd") == 0) {
osd_tess = tesseract_;
} else {
osd_tesseract_ = new Tesseract;
if (osd_tesseract_->init_tesseract(
datapath_->string(), NULL, "osd", OEM_TESSERACT_ONLY,
NULL, 0, NULL, NULL, false) == 0) {
TessdataManager mgr(reader_);
if (osd_tesseract_->init_tesseract(datapath_->string(), nullptr, "osd",
OEM_TESSERACT_ONLY, nullptr, 0,
nullptr, nullptr, false, &mgr) == 0) {
osd_tess = osd_tesseract_;
osd_tesseract_->set_source_resolution(
thresholder_->GetSourceYResolution());
} else {
tprintf("Warning: Auto orientation and script detection requested,"
" but osd language failed to load\n");
delete osd_tesseract_;
osd_tesseract_ = NULL;
osd_tesseract_ = nullptr;
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions api/baseapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
// To avoid collision with other typenames include the ABSOLUTE MINIMUM
// complexity of includes here. Use forward declarations wherever possible
// and hide includes of complex types in baseapi.cpp.
#include "platform.h"
#include "apitypes.h"
#include "thresholder.h"
#include "unichar.h"
#include "tesscallback.h"
#include "publictypes.h"
#include "pageiterator.h"
#include "platform.h"
#include "publictypes.h"
#include "resultiterator.h"
#include "serialis.h"
#include "tesscallback.h"
#include "thresholder.h"
#include "unichar.h"

template <typename T> class GenericVector;
class PAGE_RES;
Expand Down Expand Up @@ -237,6 +238,13 @@ class TESS_API TessBaseAPI {
int Init(const char* datapath, const char* language) {
return Init(datapath, language, OEM_DEFAULT, NULL, 0, NULL, NULL, false);
}
// In-memory version reads the traineddata file directly from the given
// data[data_size] array, and/or reads data via a FileReader.
int Init(const char* data, int data_size, const char* language,
OcrEngineMode mode, char** configs, int configs_size,
const GenericVector<STRING>* vars_vec,
const GenericVector<STRING>* vars_values,
bool set_only_non_debug_params, FileReader reader);

/**
* Returns the languages string used in the last valid initialization.
Expand Down Expand Up @@ -859,6 +867,7 @@ class TESS_API TessBaseAPI {
Tesseract* tesseract_; ///< The underlying data object.
Tesseract* osd_tesseract_; ///< For orientation & script detection.
EquationDetect* equ_detect_; ///<The equation detector.
FileReader reader_; ///< Reads files from any filesystem.
ImageThresholder* thresholder_; ///< Image thresholding module.
GenericVector<ParagraphModel *>* paragraph_models_;
BLOCK_LIST* block_list_; ///< The page layout.
Expand Down
Loading

0 comments on commit 1cc5111

Please sign in to comment.