Created
November 19, 2013 17:42
-
-
Save asimihsan/7549319 to your computer and use it in GitHub Desktop.
Noddy little Tesseract wrapper.
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
// ---------------------------------------------------------------------------- | |
// Noddy little Tesseract wrapper. | |
// | |
// Usage: | |
// | |
// ./tesseractcli <path to screenshot image> [left] [top] [width] [height] | |
// | |
// - Screenshot path is mandatory, can be relative to current working | |
// directory or absolute. | |
// - Left, top, width, and height are optional integers that set a | |
// bounding rectable on the screenshot within which to detect text. | |
// | |
// Returns: | |
// | |
// - Exit code 0 with detected text to stdout if OK. | |
// - Else exit code 1 with error message to stderr. | |
// | |
// How to compile: | |
// | |
// yum groupinstall 'Development Tools' | |
// yum install tesseract tesseract-devel leptonica leptonica-devel | |
// g++ -pthread $(pkg-config --cflags --libs tesseract lept) \ | |
// tesseractcli.cpp -o tesseractcli | |
// | |
// Notes: | |
// - The default tesseract CLI tool only writes output to a filepath, | |
// which is unnecessary, so this CLI tool outputs the text to stdout. | |
// - The default tesseract CLI tool doesn't let you set a rectangle | |
// within which to detect text. This one does. | |
// ---------------------------------------------------------------------------- | |
#include <iostream> | |
#include <string> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <leptonica/allheaders.h> | |
#include <tesseract/baseapi.h> | |
bool does_file_exist(const char *path) { | |
struct stat buffer; | |
return stat(path, &buffer) == 0; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc == 1) { | |
std::cerr << "Need path to screenshot image." << std::endl; | |
exit(1); | |
} | |
if (!(does_file_exist(argv[1]))) { | |
std::cerr << "Path '" << argv[1] << "' does not exist" << std::endl; | |
exit(1); | |
} | |
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); | |
// Initialize tesseract-ocr with English, without specifying tessdata path | |
if (api->Init(NULL, "eng")) { | |
std::cerr << "Could not initialize tesseract." << std::endl; | |
exit(1); | |
} | |
// Open input image with leptonica library | |
Pix *image = pixRead(argv[1]); | |
api->SetImage(image); | |
if (argc > 2) { | |
int left = atoi(argv[2]), | |
top = atoi(argv[3]), | |
width = atoi(argv[4]), | |
height = atoi(argv[5]); | |
api->SetRectangle(left, top, width, height); | |
} | |
// Get OCR result | |
char *outText = api->GetUTF8Text(); | |
std::cout << outText << std::endl; | |
// Destroy used object and release memory | |
api->End(); | |
delete [] outText; | |
pixDestroy(&image); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment