void testtess() { cv::Mat img = imread("phototest.png"); if(img.empty()) { cerr << "noup." << endl; return; } imshow("blah",img); waitKey(1); cout << "photo " << img.size() << endl; int rc = api.Init(tessdata_dir.c_str(), "ara", tesseract::OEM_DEFAULT); if (rc) { cerr << "Could not initialize tesseract.\n"; exit(1); } api.SetPageSegMode(tesseract::PSM_AUTO); printf("Tesseract Open Source OCR Engine v%s with Leptonica\n", tesseract::TessBaseAPI::Version()); printf("Init languages %s\n",api.GetInitLanguagesAsString()); Mat tmp; cvtColor(img, tmp, CV_BGR2GRAY); adaptiveThreshold(tmp, tmp, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 51, 35); imshow("eqhist", tmp); // cvtColor(tmp, out(Rect(0,0,r.width,r.height)), CV_GRAY2BGR); char* cstr = api.TesseractRect(tmp.data, tmp.channels(), tmp.cols*tmp.channels(), 0, 0, tmp.cols, tmp.rows); cout << cstr << endl; delete[] cstr; waitKey(0); }
//Default constructor OCRTesseractImpl(const char* datapath, const char* language, const char* char_whitelist, int oemode, int psmode) { #ifdef HAVE_TESSERACT const char *lang = "eng"; if (language != NULL) lang = language; if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode)) { cout << "OCRTesseract: Could not initialize tesseract." << endl; throw 1; } //cout << "OCRTesseract: tesseract version " << tess.Version() << endl; tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode; tess.SetPageSegMode(pagesegmode); if(char_whitelist != NULL) tess.SetVariable("tessedit_char_whitelist", char_whitelist); else tess.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); tess.SetVariable("save_best_choices", "T"); #else cout << "OCRTesseract("<<oemode<<psmode<<"): Tesseract not found." << endl; if (datapath != NULL) cout << " " << datapath << endl; if (language != NULL) cout << " " << language << endl; if (char_whitelist != NULL) cout << " " << char_whitelist << endl; #endif }
int main(int argc, char *argv[]) { if(argc < 2) { cout << "Usage: main <image-file-name>" << endl; exit(0); } // load the image Mat img = imread(argv[1]); //references = getReferences();//load the reference images tess.Init(NULL, NULL, tesseract::OEM_DEFAULT); tess.SetVariable("tessedit_char_whitelist", "0123456789"); //for speed testing /*double t0 = (double) getTickCount();/ for(int i = 0; i < 100; i++) { signDetection(img); } double t1 = (double) getTickCount(); cout << (t1-t0) / getTickFrequency() << endl;*/ signDetection(img); waitKey(0); return 0; }
std::string identifyText(cv::Mat input, std::string language) { ocr.Init(NULL, language.c_str(), tesseract::OEM_TESSERACT_ONLY); //std::string whitelist = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.:"; //ocr.SetVariable("tessedit_char_whitelist", whitelist.c_str()); ocr.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK); ocr.SetImage(input.data, input.cols, input.rows, 1, input.step); std::string text = ocr.GetUTF8Text(); return text; }
int tess_capi_get_ocr(PIX *pix,char *outstr,int maxlen,FILE *out) { STRING text_out; if (!api.ProcessPage(pix,0,NULL,NULL,0,&text_out)) { /* pixDestroy(&pix); */ if (out!=NULL) fprintf(out,"tesscapi: Error during bitmap processing.\n"); api.Clear(); return(-1); } strncpy(outstr,text_out.string(),maxlen-1); outstr[maxlen-1]='\0'; api.Clear(); return(0); }
int SubsExtractor::ocr(char *outtext) { Mat crop,crop2; Mat(img, ROI).copyTo(crop); cvtColor(crop, crop2, CV_BGR2GRAY); //adaptiveThreshold(crop2,crop,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,3,5); int th1 = 200; int th2 = 255; threshold(crop2,crop,th1,th2,THRESH_BINARY); //tesseract::TessBaseAPI tess; tess.SetImage((uchar *)crop.data,crop.cols,crop.rows,1,crop.cols); imshow("control", crop); char *s; if((s = tess.GetUTF8Text()) != NULL) { //fprintf(stderr,"OCR: %s\n",s); strcpy(outtext,s); return strlen(outtext); } return 0; }
int detectNumber(Mat img){ //Mat circ = copyContour(img, circle); inRange(img, Scalar(0, 0, 51), Scalar(255, 255, 255), img); //filter black tess.SetImage((uchar*)img.data, img.size().width, img.size().height, img.channels(), img.step1()); //tess.SetRectangle(circle[0]-circle[2], circle[1]-circle[2],2*circle[2],2*circle[2]); string out = string (tess.GetUTF8Text()); out.erase( std::remove_if( out.begin(), out.end(), ::isspace ), out.end() ); const char* result = out.c_str(); //printf("%s\n", result); switch(atoi(result)){ case 10: return 5; case 20: return 6; case 30: return 7; case 40: return 8; case 50: return 9; case 60: return 10; case 70: return 11; case 80: return 12; } //rectangle(img, Point(circle[0]-circle[2], circle[1]-circle[2]), Point(circle[0]+circle[2], circle[1]+circle[2]), Scalar (100,255,255),3,8,0); // imwrite("./test.png", img); return -1;}
void TesseractBridge::init() { // int rc = api.Init(tessdata_dir.c_str(), NULL); // if (rc) { // cerr << "Could not initialize tesseract.\n"; // exit(1); // } // api.End(); int rc = api.Init(tessdata_dir.c_str(), "ara", tesseract::OEM_DEFAULT); if (rc) { cerr << "Could not initialize tesseract.\n"; exit(1); } api.SetPageSegMode(tesseract::PSM_AUTO); printf("Tesseract Open Source OCR Engine v%s with Leptonica\n", tesseract::TessBaseAPI::Version()); printf("Init languages %s\n",api.GetInitLanguagesAsString()); // GenericVector<STRING> v; api.GetAvailableLanguagesAsVector(&v); // for (int i = 0; i < v.length(); ++i) { // printf("lang %s\n",v[i].string()); // } }
pair<int,string> TesseractBridge::processEx(const Mat& tmp, Rect& r) { api.Clear(); char* cstr = api.TesseractRect(tmp.data, tmp.channels(), tmp.cols*tmp.channels(), 0, 0, tmp.cols, tmp.rows); // cout << cstr << endl; delete[] cstr; tesseract::ResultIterator* ri = api.GetIterator(); tesseract::PageIteratorLevel level = tesseract::RIL_WORD; if (ri != 0) { const char* word = ri->GetUTF8Text(level); if(!word) return make_pair(0, ""); float conf = ri->Confidence(level); int x1, y1, x2, y2; ri->BoundingBox(level, &x1, &y1, &x2, &y2); // printf("word: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n", // word, conf, x1, y1, x2, y2); string theword(word); delete[] word; bool allnonalphanom = true; for (int i=0; i<theword.size(); i++) { allnonalphanom = allnonalphanom && !(isalnum(theword[i]) || theword[i] == '"' || theword[i] == '.' || theword[i] == ',' || theword[i] == '?' || theword[i] == '!' || theword[i] == '\''); } if (allnonalphanom) return make_pair(0, ""); r.x += x1; r.y += y1; r.width = x2-x1; r.height = y2-y1; return make_pair((int)conf, theword); } else return make_pair(0, ""); }
void TesseractBridge::close() { api.End(); }
void run(Mat& image, string& output, vector<Rect>* component_rects=NULL, vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL, int component_level=0) { CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC3) ); #ifdef HAVE_TESSERACT if (component_texts != 0) component_texts->clear(); if (component_rects != 0) component_rects->clear(); if (component_confidences != 0) component_confidences->clear(); tess.SetImage((uchar*)image.data, image.size().width, image.size().height, image.channels(), image.step1()); tess.Recognize(0); char *outText; outText = tess.GetUTF8Text(); output = string(outText); delete [] outText; if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) ) { tesseract::ResultIterator* ri = tess.GetIterator(); tesseract::PageIteratorLevel level = tesseract::RIL_WORD; if (component_level == OCR_LEVEL_TEXTLINE) level = tesseract::RIL_TEXTLINE; if (ri != 0) { do { const char* word = ri->GetUTF8Text(level); if (word == NULL) continue; float conf = ri->Confidence(level); int x1, y1, x2, y2; ri->BoundingBox(level, &x1, &y1, &x2, &y2); if (component_texts != 0) component_texts->push_back(string(word)); if (component_rects != 0) component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1)); if (component_confidences != 0) component_confidences->push_back(conf); delete[] word; } while (ri->Next(level)); } delete ri; } tess.Clear(); #else cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl; output.clear(); if(component_rects) component_rects->clear(); if(component_texts) component_texts->clear(); if(component_confidences) component_confidences->clear(); #endif }
~OCRTesseractImpl() { #ifdef HAVE_TESSERACT tess.End(); #endif }
/* ** ocr_type=0: OEM_DEFAULT ** ocr_type=1: OEM_TESSERACT_ONLY ** ocr_type=2: OEM_CUBE_ONLY ** ocr_type=3: OEM_TESSERACT_CUBE_COMBINED */ int tess_capi_init(char *datapath,char *language,int ocr_type,FILE *out, char *initstr,int maxlen) { int status; #ifdef USE_NLS setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); #endif // fprintf(stderr, "tesseract %s\n", tesseract::TessBaseAPI::Version()); // Make the order of args a bit more forgiving than it used to be. const char* lang = "eng"; tesseract::PageSegMode pagesegmode = tesseract::PSM_SINGLE_BLOCK; if (language!=NULL && language[0]!='\0') lang = language; /* if (output == NULL) { fprintf(stderr, _("Usage:%s imagename outputbase [-l lang] " "[-psm pagesegmode] [configfile...]\n"), argv[0]); fprintf(stderr, _("pagesegmode values are:\n" "0 = Orientation and script detection (OSD) only.\n" "1 = Automatic page segmentation with OSD.\n" "2 = Automatic page segmentation, but no OSD, or OCR\n" "3 = Fully automatic page segmentation, but no OSD. (Default)\n" "4 = Assume a single column of text of variable sizes.\n" "5 = Assume a single uniform block of vertically aligned text.\n" "6 = Assume a single uniform block of text.\n" "7 = Treat the image as a single text line.\n" "8 = Treat the image as a single word.\n" "9 = Treat the image as a single word in a circle.\n" "10 = Treat the image as a single character.\n")); fprintf(stderr, _("-l lang and/or -psm pagesegmode must occur before any" "configfile.\n")); exit(1); } */ api.SetOutputName(NULL); status=api.Init(datapath,lang, ocr_type==0 ? tesseract::OEM_DEFAULT : (ocr_type==1 ? tesseract::OEM_TESSERACT_ONLY : (ocr_type==2 ? tesseract::OEM_CUBE_ONLY : (tesseract::OEM_TESSERACT_CUBE_COMBINED)))); if (status) return(status); /* api.Init("tesscapi",lang,tesseract::OEM_DEFAULT, &(argv[arg]), argc - arg, NULL, NULL, false); */ // We have 2 possible sources of pagesegmode: a config file and // the command line. For backwards compatability reasons, the // default in tesseract is tesseract::PSM_SINGLE_BLOCK, but the // default for this program is tesseract::PSM_AUTO. We will let // the config file take priority, so the command-line default // can take priority over the tesseract default, so we use the // value from the command line only if the retrieved mode // is still tesseract::PSM_SINGLE_BLOCK, indicating no change // in any config file. Therefore the only way to force // tesseract::PSM_SINGLE_BLOCK is from the command line. // It would be simpler if we could set the value before Init, // but that doesn't work. if (api.GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK) api.SetPageSegMode(pagesegmode); /* ** Initialization message */ { char istr[256]; sprintf(istr,"Tesseract Open Source OCR Engine v%s ",tesseract::TessBaseAPI::Version()); if (ocr_type==0 || ocr_type==3) sprintf(&istr[strlen(istr)],"[CUBE+] (lang="); else if (ocr_type==2) sprintf(&istr[strlen(istr)],"[CUBE] (lang="); strncpy(&istr[strlen(istr)],language,253-strlen(istr)); istr[253]='\0'; strcat(istr,")"); if (out!=NULL) fprintf(out,"%s\n",istr); if (initstr!=NULL) { strncpy(initstr,istr,maxlen-1); initstr[maxlen-1]='\0'; } } /* Turn off CUBE debugging output */ api.SetVariable("cube_debug_level","0"); #if (WILLUSDEBUG & 1) api.SetVariable("cube_debug_level","9"); api.SetVariable("paragraph_debug_level","9"); api.SetVariable("tessdata_manager_debug_level","9"); api.SetVariable("tosp_debug_level","9"); api.SetVariable("wordrec_debug_level","9"); api.SetVariable("segsearch_debug_level","9"); #endif return(0); }
void tess_capi_end(void) { api.End(); }