Tesseract  3.02
tesseract-ocr/ccmain/cubeclassifier.cpp
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 // Author: rays@google.com (Ray Smith)
00004 // File:        cubeclassifier.cpp
00005 // Description: Cube implementation of a ShapeClassifier.
00006 // Author:      Ray Smith
00007 // Created:     Wed Nov 23 10:39:45 PST 2011
00008 //
00009 // (C) Copyright 2011, Google Inc.
00010 // Licensed under the Apache License, Version 2.0 (the "License");
00011 // you may not use this file except in compliance with the License.
00012 // You may obtain a copy of the License at
00013 // http://www.apache.org/licenses/LICENSE-2.0
00014 // Unless required by applicable law or agreed to in writing, software
00015 // distributed under the License is distributed on an "AS IS" BASIS,
00016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017 // See the License for the specific language governing permissions and
00018 // limitations under the License.
00019 //
00021 
00022 #include "cubeclassifier.h"
00023 
00024 #include "char_altlist.h"
00025 #include "char_set.h"
00026 #include "cube_object.h"
00027 #include "cube_reco_context.h"
00028 #include "tessclassifier.h"
00029 #include "tesseractclass.h"
00030 #include "trainingsample.h"
00031 #include "unicharset.h"
00032 
00033 namespace tesseract {
00034 
00035 CubeClassifier::CubeClassifier(tesseract::Tesseract* tesseract)
00036     : cube_cntxt_(tesseract->GetCubeRecoContext()),
00037       shape_table_(*tesseract->shape_table()) {
00038 }
00039 CubeClassifier::~CubeClassifier() {
00040 }
00041 
00042 // Classifies the given [training] sample, writing to results.
00043 // See ShapeClassifier for a full description.
00044 int CubeClassifier::ClassifySample(const TrainingSample& sample,
00045                                    Pix* page_pix, int debug, int keep_this,
00046                                    GenericVector<ShapeRating>* results) {
00047   results->clear();
00048   if (page_pix == NULL) return 0;
00049 
00050   ASSERT_HOST(cube_cntxt_ != NULL);
00051   const TBOX& char_box = sample.bounding_box();
00052   CubeObject* cube_obj = new tesseract::CubeObject(
00053       cube_cntxt_, page_pix, char_box.left(),
00054       pixGetHeight(page_pix) - char_box.top(),
00055       char_box.width(), char_box.height());
00056   CharAltList* alt_list = cube_obj->RecognizeChar();
00057   alt_list->Sort();
00058   CharSet* char_set = cube_cntxt_->CharacterSet();
00059   if (alt_list != NULL) {
00060     for (int i = 0; i < alt_list->AltCount(); ++i) {
00061       // Convert cube representation to a shape_id.
00062       int alt_id = alt_list->Alt(i);
00063       int unichar_id = char_set->UnicharID(char_set->ClassString(alt_id));
00064       int shape_id = shape_table_.FindShape(unichar_id, -1);
00065       if (shape_id >= 0)
00066         results->push_back(ShapeRating(shape_id, alt_list->AltProb(i)));
00067     }
00068     delete alt_list;
00069   }
00070   delete cube_obj;
00071   return results->size();
00072 }
00073 
00074 // Provides access to the ShapeTable that this classifier works with.
00075 const ShapeTable* CubeClassifier::GetShapeTable() const {
00076   return &shape_table_;
00077 }
00078 
00079 CubeTessClassifier::CubeTessClassifier(tesseract::Tesseract* tesseract)
00080     : cube_cntxt_(tesseract->GetCubeRecoContext()),
00081       shape_table_(*tesseract->shape_table()),
00082       pruner_(new TessClassifier(true, tesseract)) {
00083 }
00084 CubeTessClassifier::~CubeTessClassifier() {
00085   delete pruner_;
00086 }
00087 
00088 // Classifies the given [training] sample, writing to results.
00089 // See ShapeClassifier for a full description.
00090 int CubeTessClassifier::ClassifySample(const TrainingSample& sample,
00091                                        Pix* page_pix, int debug, int keep_this,
00092                                        GenericVector<ShapeRating>* results) {
00093   int num_results = pruner_->ClassifySample(sample, page_pix, debug, keep_this,
00094                                             results);
00095   if (page_pix == NULL) return num_results;
00096 
00097   ASSERT_HOST(cube_cntxt_ != NULL);
00098   const TBOX& char_box = sample.bounding_box();
00099   CubeObject* cube_obj = new tesseract::CubeObject(
00100       cube_cntxt_, page_pix, char_box.left(),
00101       pixGetHeight(page_pix) - char_box.top(),
00102       char_box.width(), char_box.height());
00103   CharAltList* alt_list = cube_obj->RecognizeChar();
00104   CharSet* char_set = cube_cntxt_->CharacterSet();
00105   if (alt_list != NULL) {
00106     for (int r = 0; r < num_results; ++r) {
00107       const Shape& shape = shape_table_.GetShape((*results)[r].shape_id);
00108       // Get the best cube probability of all unichars in the shape.
00109       double best_prob = 0.0;
00110       for (int i = 0; i < alt_list->AltCount(); ++i) {
00111         int alt_id = alt_list->Alt(i);
00112         int unichar_id = char_set->UnicharID(char_set->ClassString(alt_id));
00113         if (shape.ContainsUnichar(unichar_id) &&
00114             alt_list->AltProb(i) > best_prob) {
00115           best_prob = alt_list->AltProb(i);
00116         }
00117       }
00118       (*results)[r].rating = best_prob;
00119     }
00120     delete alt_list;
00121     // Re-sort by rating.
00122     results->sort(&ShapeRating::SortDescendingRating);
00123   }
00124   delete cube_obj;
00125   return results->size();
00126 }
00127 
00128 // Provides access to the ShapeTable that this classifier works with.
00129 const ShapeTable* CubeTessClassifier::GetShapeTable() const {
00130   return &shape_table_;
00131 }
00132 
00133 }  // namespace tesseract
00134 
00135 
00136