Skip to content

Commit

Permalink
Removed dependency on cube from the code
Browse files Browse the repository at this point in the history
  • Loading branch information
theraysmith committed Dec 14, 2016
1 parent 5129c43 commit 9f5ba91
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 240 deletions.
82 changes: 14 additions & 68 deletions api/baseapi.cpp
Expand Up @@ -743,53 +743,6 @@ void TessBaseAPI::DumpPGM(const char* filename) {
fclose(fp);
}

#ifndef NO_CUBE_BUILD
/**
* Placeholder for call to Cube and test that the input data is correct.
* reskew is the direction of baselines in the skewed image in
* normalized (cos theta, sin theta) form, so (0.866, 0.5) would represent
* a 30 degree anticlockwise skew.
*/
int CubeAPITest(Boxa* boxa_blocks, Pixa* pixa_blocks,
Boxa* boxa_words, Pixa* pixa_words,
const FCOORD& reskew, Pix* page_pix,
PAGE_RES* page_res) {
int block_count = boxaGetCount(boxa_blocks);
ASSERT_HOST(block_count == pixaGetCount(pixa_blocks));
// Write each block to the current directory as junk_write_display.nnn.png.
for (int i = 0; i < block_count; ++i) {
Pix* pix = pixaGetPix(pixa_blocks, i, L_CLONE);
pixDisplayWrite(pix, 1);
}
int word_count = boxaGetCount(boxa_words);
ASSERT_HOST(word_count == pixaGetCount(pixa_words));
int pr_word = 0;
PAGE_RES_IT page_res_it(page_res);
for (page_res_it.restart_page(); page_res_it.word () != NULL;
page_res_it.forward(), ++pr_word) {
WERD_RES *word = page_res_it.word();
WERD_CHOICE* choice = word->best_choice;
// Write the first 100 words to files names wordims/<wordstring>.tif.
if (pr_word < 100) {
STRING filename("wordims/");
if (choice != NULL) {
filename += choice->unichar_string();
} else {
char numbuf[32];
filename += "unclassified";
snprintf(numbuf, 32, "%03d", pr_word);
filename += numbuf;
}
filename += ".tif";
Pix* pix = pixaGetPix(pixa_words, pr_word, L_CLONE);
pixWrite(filename.string(), pix, IFF_TIFF_G4);
}
}
ASSERT_HOST(pr_word == word_count);
return 0;
}
#endif // NO_CUBE_BUILD

/**
* Runs page layout analysis in the mode set by SetPageSegMode.
* May optionally be called prior to Recognize to get access to just
Expand Down Expand Up @@ -1893,13 +1846,16 @@ char* TessBaseAPI::GetUNLVText() {

/**
* Detect the orientation of the input image and apparent script (alphabet).
* orient_deg is the detected clockwise rotation of the input image in degrees (0, 90, 180, 270)
* orient_deg is the detected clockwise rotation of the input image in degrees
* (0, 90, 180, 270)
* orient_conf is the confidence (15.0 is reasonably confident)
* script_name is an ASCII string, the name of the script, e.g. "Latin"
* script_conf is confidence level in the script
* Returns true on success and writes values to each parameter as an output
*/
bool TessBaseAPI::DetectOrientationScript(int* orient_deg, float* orient_conf, const char** script_name, float* script_conf) {
bool TessBaseAPI::DetectOrientationScript(int* orient_deg, float* orient_conf,
const char** script_name,
float* script_conf) {
OSResults osr;

bool osd = DetectOS(&osr);
Expand All @@ -1909,21 +1865,17 @@ bool TessBaseAPI::DetectOrientationScript(int* orient_deg, float* orient_conf, c

int orient_id = osr.best_result.orientation_id;
int script_id = osr.get_best_script(orient_id);
if (orient_conf)
*orient_conf = osr.best_result.oconfidence;
if (orient_deg)
*orient_deg = orient_id * 90; // convert quadrant to degrees
if (orient_conf) *orient_conf = osr.best_result.oconfidence;
if (orient_deg) *orient_deg = orient_id * 90; // convert quadrant to degrees

if (script_name) {
const char* script =
osr.unicharset->get_script_from_script_id(script_id);
const char* script = osr.unicharset->get_script_from_script_id(script_id);

*script_name = script;
}

if (script_conf)
*script_conf = osr.best_result.sconfidence;

if (script_conf) *script_conf = osr.best_result.sconfidence;

return true;
}

Expand All @@ -1938,7 +1890,8 @@ char* TessBaseAPI::GetOsdText(int page_number) {
const char* script_name;
float script_conf;

if (!DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf))
if (!DetectOrientationScript(&orient_deg, &orient_conf, &script_name,
&script_conf))
return NULL;

// clockwise rotation needed to make the page upright
Expand Down Expand Up @@ -2250,8 +2203,8 @@ void TessBaseAPI::Threshold(Pix** pix) {
if (y_res < kMinCredibleResolution || y_res > kMaxCredibleResolution) {
// Use the minimum default resolution, as it is safer to under-estimate
// than over-estimate resolution.
tprintf("Warning. Invalid resolution %d dpi. Using %d instead.\n",
y_res, kMinCredibleResolution);
tprintf("Warning. Invalid resolution %d dpi. Using %d instead.\n", y_res,
kMinCredibleResolution);
thresholder_->SetSourceYResolution(kMinCredibleResolution);
}
PageSegMode pageseg_mode =
Expand Down Expand Up @@ -2857,13 +2810,6 @@ int TessBaseAPI::NumDawgs() const {
return tesseract_ == NULL ? 0 : tesseract_->getDict().NumDawgs();
}

#ifndef NO_CUBE_BUILD
/** Return a pointer to underlying CubeRecoContext object if present. */
CubeRecoContext *TessBaseAPI::GetCubeRecoContext() const {
return (tesseract_ == NULL) ? NULL : tesseract_->GetCubeRecoContext();
}
#endif // NO_CUBE_BUILD

/** Escape a char string - remove <>&"' with HTML codes. */
STRING HOcrEscape(const char* text) {
STRING ret;
Expand Down
14 changes: 4 additions & 10 deletions api/baseapi.h
Expand Up @@ -65,9 +65,6 @@ struct TBLOB;

namespace tesseract {

#ifndef NO_CUBE_BUILD
class CubeRecoContext;
#endif // NO_CUBE_BUILD
class Dawg;
class Dict;
class EquationDetect;
Expand Down Expand Up @@ -620,13 +617,15 @@ class TESS_API TessBaseAPI {

/**
* Detect the orientation of the input image and apparent script (alphabet).
* orient_deg is the detected clockwise rotation of the input image in degrees (0, 90, 180, 270)
* orient_deg is the detected clockwise rotation of the input image in degrees
* (0, 90, 180, 270)
* orient_conf is the confidence (15.0 is reasonably confident)
* script_name is an ASCII string, the name of the script, e.g. "Latin"
* script_conf is confidence level in the script
* Returns true on success and writes values to each parameter as an output
*/
bool DetectOrientationScript(int* orient_deg, float* orient_conf, const char** script_name, float* script_conf);
bool DetectOrientationScript(int* orient_deg, float* orient_conf,
const char** script_name, float* script_conf);

/**
* The recognized text is returned as a char* which is coded
Expand Down Expand Up @@ -762,11 +761,6 @@ class TESS_API TessBaseAPI {

void InitTruthCallback(TruthCallback *cb) { truth_cb_ = cb; }

#ifndef NO_CUBE_BUILD
/** Return a pointer to underlying CubeRecoContext object if present. */
CubeRecoContext *GetCubeRecoContext() const;
#endif // NO_CUBE_BUILD

void set_min_orientation_margin(double margin);

/**
Expand Down
23 changes: 3 additions & 20 deletions ccmain/control.cpp
Expand Up @@ -405,15 +405,6 @@ bool Tesseract::recog_all_words(PAGE_RES* page_res,
// ****************** Pass 5,6 *******************
rejection_passes(page_res, monitor, target_word_box, word_config);

#ifndef NO_CUBE_BUILD
// ****************** Pass 7 *******************
// Cube combiner.
// If cube is loaded and its combiner is present, run it.
if (tessedit_ocr_engine_mode == OEM_TESSERACT_CUBE_COMBINED) {
run_cube_combiner(page_res);
}
#endif

// ****************** Pass 8 *******************
font_recognition_pass(page_res);

Expand Down Expand Up @@ -887,7 +878,7 @@ int Tesseract::RetryWithLanguage(const WordData& word_data,
WordRecognizer recognizer,
WERD_RES** in_word,
PointerVector<WERD_RES>* best_words) {
bool debug = classify_debug_level || cube_debug_level;
bool debug = classify_debug_level;
if (debug) {
tprintf("Trying word using lang %s, oem %d\n",
lang.string(), static_cast<int>(tessedit_ocr_engine_mode));
Expand All @@ -906,8 +897,7 @@ int Tesseract::RetryWithLanguage(const WordData& word_data,
new_words[i]->DebugTopChoice("Lang result");
}
// Initial version is a bit of a hack based on better certainty and rating
// (to reduce false positives from cube) or a dictionary vs non-dictionary
// word.
// or a dictionary vs non-dictionary word.
return SelectBestWords(classify_max_rating_ratio,
classify_max_certainty_margin,
debug, &new_words, best_words);
Expand Down Expand Up @@ -1291,7 +1281,7 @@ void Tesseract::classify_word_and_language(int pass_n, PAGE_RES_IT* pr_it,
// Points to the best result. May be word or in lang_words.
WERD_RES* word = word_data->word;
clock_t start_t = clock();
if (classify_debug_level || cube_debug_level) {
if (classify_debug_level) {
tprintf("%s word with lang %s at:",
word->done ? "Already done" : "Processing",
most_recently_used_->lang.string());
Expand Down Expand Up @@ -1365,13 +1355,6 @@ void Tesseract::classify_word_pass1(const WordData& word_data,
BLOCK* block = word_data.block;
prev_word_best_choice_ = word_data.prev_word != NULL
? word_data.prev_word->word->best_choice : NULL;
#ifndef NO_CUBE_BUILD
// If we only intend to run cube - run it and return.
if (tessedit_ocr_engine_mode == OEM_CUBE_ONLY) {
cube_word_pass1(block, row, *in_word);
return;
}
#endif
#ifndef ANDROID_BUILD
if (tessedit_ocr_engine_mode == OEM_LSTM_ONLY ||
tessedit_ocr_engine_mode == OEM_TESSERACT_LSTM_COMBINED) {
Expand Down
6 changes: 3 additions & 3 deletions ccmain/par_control.cpp
Expand Up @@ -20,7 +20,7 @@
#include "tesseractclass.h"
#ifdef _OPENMP
#include <omp.h>
#endif // _OPENMP
#endif // _OPENMP

namespace tesseract {

Expand Down Expand Up @@ -54,8 +54,8 @@ void Tesseract::PrerecAllWordsPar(const GenericVector<WordData>& words) {
// Pre-classify all the blobs.
if (tessedit_parallelize > 1) {
#ifdef _OPENMP
#pragma omp parallel for num_threads(10)
#endif // _OPENMP
#pragma omp parallel for num_threads(10)
#endif // _OPENMP
for (int b = 0; b < blobs.size(); ++b) {
*blobs[b].choices =
blobs[b].tesseract->classify_blob(blobs[b].blob, "par", White, NULL);
Expand Down
3 changes: 0 additions & 3 deletions ccmain/paramsd.h
Expand Up @@ -24,9 +24,6 @@
#define VARABLED_H

#include "elst.h"
#ifndef NO_CUBE_BUILD
#include "scrollview.h"
#endif
#include "params.h"
#include "tesseractclass.h"

Expand Down
21 changes: 2 additions & 19 deletions ccmain/tessedit.cpp
Expand Up @@ -243,17 +243,6 @@ bool Tesseract::init_tesseract_lang_data(
ambigs_debug_level, use_ambigs_for_adaption, &unicharset);
if (tessdata_manager_debug_level) tprintf("Loaded ambigs\n");
}
#ifndef NO_CUBE_BUILD
if (tessedit_ocr_engine_mode == OEM_CUBE_ONLY) {
ASSERT_HOST(init_cube_objects(false, &tessdata_manager));
if (tessdata_manager_debug_level)
tprintf("Loaded Cube w/out combiner\n");
} else if (tessedit_ocr_engine_mode == OEM_TESSERACT_CUBE_COMBINED) {
ASSERT_HOST(init_cube_objects(true, &tessdata_manager));
if (tessdata_manager_debug_level)
tprintf("Loaded Cube with combiner\n");
}
#endif
// Init ParamsModel.
// Load pass1 and pass2 weights (for now these two sets are the same, but in
// the future separate sets of weights can be generated).
Expand Down Expand Up @@ -446,14 +435,8 @@ int Tesseract::init_tesseract_internal(
}
// If only LSTM will be used, skip loading Tesseract classifier's
// pre-trained templates and dictionary.
bool init_tesseract = tessedit_ocr_engine_mode != OEM_LSTM_ONLY &&
tessedit_ocr_engine_mode != OEM_CUBE_ONLY;
bool init_dict = init_tesseract;
if (tessedit_ocr_engine_mode == OEM_CUBE_ONLY &&
!tessdata_manager.SeekToStart(TESSDATA_CUBE_UNICHARSET)) {
init_dict = true;
}
program_editup(textbase, init_tesseract, init_dict);
bool init_tesseract = tessedit_ocr_engine_mode != OEM_LSTM_ONLY;
program_editup(textbase, init_tesseract, init_tesseract);
tessdata_manager.End();
return 0; //Normal exit
}
Expand Down
29 changes: 2 additions & 27 deletions ccmain/tesseractclass.cpp
Expand Up @@ -42,15 +42,11 @@
#include "tesseractclass.h"

#include "allheaders.h"
#ifndef NO_CUBE_BUILD
#include "cube_reco_context.h"
#endif
#include "edgblob.h"
#include "equationdetect.h"
#include "globals.h"
#ifndef NO_CUBE_BUILD
#ifndef ANDROID_BUILD
#include "lstmrecognizer.h"
#include "tesseract_cube_combiner.h"
#endif

namespace tesseract {
Expand Down Expand Up @@ -224,7 +220,6 @@ Tesseract::Tesseract()
"Run paragraph detection on the post-text-recognition "
"(more accurate)",
this->params()),
INT_MEMBER(cube_debug_level, 0, "Print cube debug info.", this->params()),
BOOL_MEMBER(lstm_use_matrix, 1,
"Use ratings matrix/beam search with lstm", this->params()),
STRING_MEMBER(outlines_odd, "%| ", "Non standard number of outlines",
Expand Down Expand Up @@ -608,7 +603,6 @@ Tesseract::Tesseract()

backup_config_file_(NULL),
pix_binary_(NULL),
cube_binary_(NULL),
pix_grey_(NULL),
pix_original_(NULL),
pix_thresholds_(NULL),
Expand All @@ -621,10 +615,6 @@ Tesseract::Tesseract()
reskew_(1.0f, 0.0f),
most_recently_used_(this),
font_table_size_(0),
#ifndef NO_CUBE_BUILD
cube_cntxt_(NULL),
tess_cube_combiner_(NULL),
#endif
equ_detect_(NULL),
#ifndef ANDROID_BUILD
lstm_recognizer_(NULL),
Expand All @@ -637,24 +627,14 @@ Tesseract::~Tesseract() {
pixDestroy(&pix_original_);
end_tesseract();
sub_langs_.delete_data_pointers();
#ifndef NO_CUBE_BUILD
// Delete cube objects.
if (cube_cntxt_ != NULL) {
delete cube_cntxt_;
cube_cntxt_ = NULL;
}
if (tess_cube_combiner_ != NULL) {
delete tess_cube_combiner_;
tess_cube_combiner_ = NULL;
}
#ifndef ANDROID_BUILD
delete lstm_recognizer_;
lstm_recognizer_ = NULL;
#endif
}

void Tesseract::Clear() {
pixDestroy(&pix_binary_);
pixDestroy(&cube_binary_);
pixDestroy(&pix_grey_);
pixDestroy(&pix_thresholds_);
pixDestroy(&scaled_color_);
Expand Down Expand Up @@ -704,8 +684,6 @@ void Tesseract::SetBlackAndWhitelist() {
// page segmentation.
void Tesseract::PrepareForPageseg() {
textord_.set_use_cjk_fp_model(textord_use_cjk_fp_model);
pixDestroy(&cube_binary_);
cube_binary_ = pixClone(pix_binary());
// Find the max splitter strategy over all langs.
ShiroRekhaSplitter::SplitStrategy max_pageseg_strategy =
static_cast<ShiroRekhaSplitter::SplitStrategy>(
Expand All @@ -716,9 +694,6 @@ void Tesseract::PrepareForPageseg() {
static_cast<inT32>(sub_langs_[i]->pageseg_devanagari_split_strategy));
if (pageseg_strategy > max_pageseg_strategy)
max_pageseg_strategy = pageseg_strategy;
// Clone the cube image to all the sub langs too.
pixDestroy(&sub_langs_[i]->cube_binary_);
sub_langs_[i]->cube_binary_ = pixClone(pix_binary());
pixDestroy(&sub_langs_[i]->pix_binary_);
sub_langs_[i]->pix_binary_ = pixClone(pix_binary());
}
Expand Down

0 comments on commit 9f5ba91

Please sign in to comment.