Created
October 23, 2018 20:54
-
-
Save JburkeRSAC/7f1fa09cb5e0eab10c82be19ab2cd3ca to your computer and use it in GitHub Desktop.
Compile: g++ -O3 -std=c++11 ocr_simple.cpp `pkg-config --cflags --libs tesseract opencv`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <tesseract/baseapi.h> | |
#include <leptonica/allheaders.h> | |
#include <opencv2/opencv.hpp> | |
using namespace std; | |
using namespace cv; | |
int main(int argc, char* argv[]){ | |
string outText; | |
string imPath = argv[1]; | |
// Create Tesseract object | |
tesseract::TessBaseAPI *ocr = new tesseract::TessBaseAPI(); | |
// Initialize tesseract to use English (eng) and the LSTM OCR engine. | |
ocr->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY); | |
// Set Page segmentation mode to PSM_AUTO (3) | |
ocr->SetPageSegMode(tesseract::PSM_AUTO); | |
// Open input image using OpenCV | |
Mat im = cv::imread(imPath, IMREAD_COLOR); | |
// Set image data | |
ocr->SetImage(im.data, im.cols, im.rows, 3, im.step); | |
// Run Tesseract OCR on image | |
outText = string(ocr->GetUTF8Text()); | |
// print recognized text | |
cout << outText << endl; // Destroy used object and release memory ocr->End(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment