123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- #ifndef __OPENCV_TEXT_OCR_HPP__
- #define __OPENCV_TEXT_OCR_HPP__
- #include <vector>
- #include <string>
- namespace cv
- {
- namespace text
- {
- enum
- {
- OCR_LEVEL_WORD,
- OCR_LEVEL_TEXTLINE
- };
- enum page_seg_mode
- {
- PSM_OSD_ONLY,
- PSM_AUTO_OSD,
- PSM_AUTO_ONLY,
- PSM_AUTO,
- PSM_SINGLE_COLUMN,
- PSM_SINGLE_BLOCK_VERT_TEXT,
- PSM_SINGLE_BLOCK,
- PSM_SINGLE_LINE,
- PSM_SINGLE_WORD,
- PSM_CIRCLE_WORD,
- PSM_SINGLE_CHAR
- };
- enum ocr_engine_mode
- {
- OEM_TESSERACT_ONLY,
- OEM_CUBE_ONLY,
- OEM_TESSERACT_CUBE_COMBINED,
- OEM_DEFAULT
- };
- class CV_EXPORTS_W BaseOCR
- {
- public:
- virtual ~BaseOCR() {};
- virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0) = 0;
- virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0) = 0;
- };
- class CV_EXPORTS_W OCRTesseract : public BaseOCR
- {
- public:
-
- virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
- virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
-
- CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
- CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
- CV_WRAP virtual void setWhiteList(const String& char_whitelist) = 0;
-
- CV_WRAP static Ptr<OCRTesseract> create(const char* datapath=NULL, const char* language=NULL,
- const char* char_whitelist=NULL, int oem=OEM_DEFAULT, int psmode=PSM_AUTO);
- };
- enum decoder_mode
- {
- OCR_DECODER_VITERBI = 0
- };
- enum classifier_type
- {
- OCR_KNN_CLASSIFIER = 0,
- OCR_CNN_CLASSIFIER = 1
- };
- class CV_EXPORTS_W OCRHMMDecoder : public BaseOCR
- {
- public:
-
- class CV_EXPORTS_W ClassifierCallback
- {
- public:
- virtual ~ClassifierCallback() { }
-
- virtual void eval( InputArray image, std::vector<int>& out_class, std::vector<double>& out_confidence);
- };
- public:
-
- virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
-
- virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
-
- CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
- CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
-
- static Ptr<OCRHMMDecoder> create(const Ptr<OCRHMMDecoder::ClassifierCallback> classifier,
- const std::string& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- decoder_mode mode = OCR_DECODER_VITERBI);
- CV_WRAP static Ptr<OCRHMMDecoder> create(const Ptr<OCRHMMDecoder::ClassifierCallback> classifier,
- const String& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- int mode = OCR_DECODER_VITERBI);
-
- CV_WRAP static Ptr<OCRHMMDecoder> create(const String& filename,
- const String& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- int mode = OCR_DECODER_VITERBI,
- int classifier = OCR_KNN_CLASSIFIER);
- protected:
- Ptr<OCRHMMDecoder::ClassifierCallback> classifier;
- std::string vocabulary;
- Mat transition_p;
- Mat emission_p;
- decoder_mode mode;
- };
- CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierNM(const String& filename);
- CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifierCNN(const String& filename);
- CV_EXPORTS_W Ptr<OCRHMMDecoder::ClassifierCallback> loadOCRHMMClassifier(const String& filename, int classifier);
- CV_EXPORTS void createOCRHMMTransitionsTable(std::string& vocabulary, std::vector<std::string>& lexicon, OutputArray transition_probabilities_table);
- CV_EXPORTS_W Mat createOCRHMMTransitionsTable(const String& vocabulary, std::vector<cv::String>& lexicon);
- class CV_EXPORTS_W OCRBeamSearchDecoder : public BaseOCR
- {
- public:
-
- class CV_EXPORTS_W ClassifierCallback
- {
- public:
- virtual ~ClassifierCallback() { }
-
- virtual void eval( InputArray image, std::vector< std::vector<double> >& recognition_probabilities, std::vector<int>& oversegmentation );
- int getWindowSize() {return 0;}
- int getStepSize() {return 0;}
- };
- public:
-
- virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
- virtual void run(Mat& image, Mat& mask, std::string& output_text, std::vector<Rect>* component_rects=NULL,
- std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
- int component_level=0);
-
- CV_WRAP String run(InputArray image, int min_confidence, int component_level=0);
- CV_WRAP String run(InputArray image, InputArray mask, int min_confidence, int component_level=0);
-
- static Ptr<OCRBeamSearchDecoder> create(const Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier,
- const std::string& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- decoder_mode mode = OCR_DECODER_VITERBI,
- int beam_size = 500);
- CV_WRAP static Ptr<OCRBeamSearchDecoder> create(const Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier,
- const String& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- int mode = OCR_DECODER_VITERBI,
- int beam_size = 500);
-
- CV_WRAP static Ptr<OCRBeamSearchDecoder> create(const String& filename,
- const String& vocabulary,
-
- InputArray transition_probabilities_table,
-
- InputArray emission_probabilities_table,
-
- int mode = OCR_DECODER_VITERBI,
- int beam_size = 500);
- protected:
- Ptr<OCRBeamSearchDecoder::ClassifierCallback> classifier;
- std::string vocabulary;
- Mat transition_p;
- Mat emission_p;
- decoder_mode mode;
- int beam_size;
- };
- CV_EXPORTS_W Ptr<OCRBeamSearchDecoder::ClassifierCallback> loadOCRBeamSearchClassifierCNN(const String& filename);
- class CV_EXPORTS OCRHolisticWordRecognizer : public BaseOCR
- {
- public:
- virtual void run(Mat& image,
- std::string& output_text,
- std::vector<Rect>* component_rects = NULL,
- std::vector<std::string>* component_texts = NULL,
- std::vector<float>* component_confidences = NULL,
- int component_level = OCR_LEVEL_WORD) = 0;
-
- virtual void run(Mat& image,
- Mat& mask,
- std::string& output_text,
- std::vector<Rect>* component_rects = NULL,
- std::vector<std::string>* component_texts = NULL,
- std::vector<float>* component_confidences = NULL,
- int component_level = OCR_LEVEL_WORD) = 0;
-
- static Ptr<OCRHolisticWordRecognizer> create(const std::string &archFilename,
- const std::string &weightsFilename,
- const std::string &wordsFilename);
- };
- }}
- #endif
|