00001 00002 // File: osdetect.h 00003 // Description: Orientation and script detection. 00004 // Author: Samuel Charron 00005 // 00006 // (C) Copyright 2008, Google Inc. 00007 // Licensed under the Apache License, Version 2.0 (the "License"); 00008 // you may not use this file except in compliance with the License. 00009 // You may obtain a copy of the License at 00010 // http://www.apache.org/licenses/LICENSE-2.0 00011 // Unless required by applicable law or agreed to in writing, software 00012 // distributed under the License is distributed on an "AS IS" BASIS, 00013 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 // See the License for the specific language governing permissions and 00015 // limitations under the License. 00016 // 00018 00019 #ifndef TESSERACT_CCMAIN_OSDETECT_H__ 00020 #define TESSERACT_CCMAIN_OSDETECT_H__ 00021 00022 #include "strngs.h" 00023 #include "unicharset.h" 00024 00025 class TO_BLOCK_LIST; 00026 class BLOBNBOX; 00027 class BLOB_CHOICE_LIST; 00028 00029 namespace tesseract { 00030 class Tesseract; 00031 } 00032 00033 // Max number of scripts in ICU + "NULL" + Japanese and Korean + Fraktur 00034 const int kMaxNumberOfScripts = 116 + 1 + 2 + 1; 00035 00036 struct OSBestResult { 00037 int orientation; 00038 const char* script; 00039 float sconfidence; 00040 float oconfidence; 00041 }; 00042 00043 struct OSResults { 00044 OSResults() { 00045 for (int i = 0; i < 4; ++i) { 00046 for (int j = 0; j < kMaxNumberOfScripts; ++j) 00047 scripts_na[i][j] = 0; 00048 orientations[i] = 0; 00049 } 00050 } 00051 float orientations[4]; 00052 float scripts_na[4][kMaxNumberOfScripts]; 00053 00054 UNICHARSET* unicharset; 00055 OSBestResult best_result; 00056 }; 00057 00058 class OrientationDetector { 00059 public: 00060 OrientationDetector(OSResults*); 00061 bool detect_blob(BLOB_CHOICE_LIST* scores); 00062 void update_best_orientation(); 00063 int get_orientation(); 00064 private: 00065 OSResults* osr_; 00066 }; 00067 00068 class ScriptDetector { 00069 public: 00070 ScriptDetector(OSResults*, tesseract::Tesseract* tess); 00071 void detect_blob(BLOB_CHOICE_LIST* scores); 00072 void update_best_script(int); 00073 void get_script() ; 00074 bool must_stop(int orientation); 00075 private: 00076 OSResults* osr_; 00077 static const char* korean_script_; 00078 static const char* japanese_script_; 00079 static const char* fraktur_script_; 00080 int korean_id_; 00081 int japanese_id_; 00082 int katakana_id_; 00083 int hiragana_id_; 00084 int han_id_; 00085 int hangul_id_; 00086 int latin_id_; 00087 int fraktur_id_; 00088 tesseract::Tesseract* tess_; 00089 }; 00090 00091 bool orientation_and_script_detection(STRING& filename, 00092 OSResults*, 00093 tesseract::Tesseract*); 00094 00095 bool os_detect(TO_BLOCK_LIST* port_blocks, 00096 OSResults* osr, 00097 tesseract::Tesseract* tess); 00098 00099 bool os_detect_blob(BLOBNBOX* bbox, OrientationDetector* o, 00100 ScriptDetector* s, OSResults*, 00101 tesseract::Tesseract* tess); 00102 #endif // TESSERACT_CCMAIN_OSDETECT_H__