tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/classify/tessclassifier.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:        tessclassifier.cpp
00005 // Description: Tesseract implementation of a ShapeClassifier.
00006 // Author:      Ray Smith
00007 // Created:     Tue Nov 22 14:16:25 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 "tessclassifier.h"
00023 
00024 #include "classify.h"
00025 #include "trainingsample.h"
00026 
00027 namespace tesseract {
00028 
00029 // Classifies the given [training] sample, writing to results.
00030 // See ShapeClassifier for a full description.
00031 int TessClassifier::UnicharClassifySample(
00032     const TrainingSample& sample, Pix* page_pix, int debug,
00033     UNICHAR_ID keep_this, GenericVector<UnicharRating>* results) {
00034   int old_matcher_level = classify_->matcher_debug_level;
00035   int old_matcher_flags = classify_->matcher_debug_flags;
00036   int old_classify_level = classify_->classify_debug_level;
00037   if (debug) {
00038     // Explicitly set values of various control parameters to generate debug
00039     // output if required, restoring the old values after classifying.
00040     classify_->matcher_debug_level.set_value(2);
00041     classify_->matcher_debug_flags.set_value(25);
00042     classify_->classify_debug_level.set_value(3);
00043   }
00044   classify_->CharNormTrainingSample(pruner_only_, keep_this, sample, results);
00045   if (debug) {
00046     classify_->matcher_debug_level.set_value(old_matcher_level);
00047     classify_->matcher_debug_flags.set_value(old_matcher_flags);
00048     classify_->classify_debug_level.set_value(old_classify_level);
00049   }
00050   return results->size();
00051 }
00052 
00053 // Provides access to the ShapeTable that this classifier works with.
00054 const ShapeTable* TessClassifier::GetShapeTable() const {
00055   return classify_->shape_table();
00056 }
00057 // Provides access to the UNICHARSET that this classifier works with.
00058 // Only needs to be overridden if GetShapeTable() can return NULL.
00059 const UNICHARSET& TessClassifier::GetUnicharset() const {
00060   return classify_->unicharset;
00061 }
00062 
00063 // Displays classification as the given shape_id. Creates as many windows
00064 // as it feels fit, using index as a guide for placement. Adds any created
00065 // windows to the windows output and returns a new index that may be used
00066 // by any subsequent classifiers. Caller waits for the user to view and
00067 // then destroys the windows by clearing the vector.
00068 int TessClassifier::DisplayClassifyAs(
00069     const TrainingSample& sample, Pix* page_pix, int unichar_id, int index,
00070     PointerVector<ScrollView>* windows) {
00071   int shape_id = unichar_id;
00072   if (GetShapeTable() != NULL)
00073     shape_id = BestShapeForUnichar(sample, page_pix, unichar_id, NULL);
00074   if (shape_id < 0) return index;
00075   if (UnusedClassIdIn(classify_->PreTrainedTemplates, shape_id)) {
00076     tprintf("No built-in templates for class/shape %d\n", shape_id);
00077     return index;
00078   }
00079   classify_->ShowBestMatchFor(shape_id, sample.features(),
00080                               sample.num_features());
00081   return index;
00082 }
00083 
00084 }  // namespace tesseract
00085 
00086 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines