Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from jessefulton/hocr

adding HOCR support (outputs metadata describing the OCR match)
  • Loading branch information...
commit 2da14095b5aa386fbae1d33f94984644c7c6d3f5 2 parents bd1e5d1 + 4134b55
@kylemcdonald authored
Showing with 53 additions and 6 deletions.
  1. +43 −5 src/ofxTesseract.cpp
  2. +10 −1 src/ofxTesseract.h
View
48 src/ofxTesseract.cpp
@@ -9,9 +9,13 @@ void ofxTesseract::setup(string dataPath, bool absolute, string language) {
// so we override it by setting an environment variable
setenv("TESSDATA_PREFIX", absoluteTessdataPath.c_str(), 1);
tess.Init(absoluteTessdataPath.c_str(), language.c_str());
+
+ // fixes issues with hocr - see http://code.google.com/p/tesseract-ocr/issues/detail?id=463
+ tess.SetInputName("");
setMode(AUTO);
}
+
void ofxTesseract::setWhitelist(string whitelistCharacters) {
tess.SetVariable("tessedit_char_whitelist", whitelistCharacters.c_str());
}
@@ -51,12 +55,46 @@ string ofxTesseract::findText(ofImage& img) {
string ofxTesseract::findText(ofImage& img, ofRectangle& roi) {
ofPixels& pixels = img.getPixelsRef();
+ return findText(pixels, roi);
+}
+
+string ofxTesseract::findText(ofPixels& pixels) {
+ ofRectangle roi(0, 0, pixels.getWidth(), pixels.getHeight());
+ return findText(pixels, roi);
+}
+
+string ofxTesseract::findText(ofPixels& pixels, ofRectangle& roi) {
int bytesPerPixel = pixels.getBytesPerPixel();
return tess.TesseractRect(
- pixels.getPixels(),
- bytesPerPixel,
- pixels.getWidth() * bytesPerPixel,
- roi.x, roi.y,
- roi.width, roi.height
+ pixels.getPixels(),
+ bytesPerPixel,
+ pixels.getWidth() * bytesPerPixel,
+ roi.x, roi.y,
+ roi.width, roi.height
+ );
+}
+
+string ofxTesseract::findTextHocr(ofImage& img) {
+ ofRectangle roi(0, 0, img.getWidth(), img.getHeight());
+ return findTextHocr(img, roi);
+}
+
+string ofxTesseract::findTextHocr(ofImage& img, ofRectangle& roi) {
+ ofPixels& pixels = img.getPixelsRef();
+ int bytesPerPixel = pixels.getBytesPerPixel();
+
+ tess.SetImage(
+ pixels.getPixels(),
+ img.getWidth(),
+ img.getHeight(),
+ bytesPerPixel,
+ pixels.getWidth() * bytesPerPixel
+ );
+
+ tess.SetRectangle(
+ roi.x, roi.y,
+ roi.width, roi.height
);
+
+ return tess.GetHOCRText(0);
}
View
11 src/ofxTesseract.h
@@ -29,7 +29,7 @@ class ofxTesseract {
// dataPath is where to find a folder with tessdata inside
// default arguments assume bin/data/tessdata/eng.traineddata
void setup(string dataPath = "", bool absolute = false, string language = "eng");
-
+
// you must call these after setup, contrary to the tesseract-ocr docs
// whitelist means only those characters are used
void setWhitelist(string whitelistCharacters);
@@ -52,7 +52,16 @@ class ofxTesseract {
// OF_IMAGE_COLOR and OF_IMAGE_GRAYSCALE are both fine
string findText(ofImage& img);
string findText(ofImage& img, ofRectangle& roi);
+
+ //raw pixel data? why not!
+ string findText(ofPixels& pixels);
+ string findText(ofPixels& pixels, ofRectangle& roi);
+
+ //HOCR goodness
+ string findTextHocr(ofImage& img);
+ string findTextHocr(ofImage& img, ofRectangle& roi);
protected:
tesseract::TessBaseAPI tess;
+
};
Please sign in to comment.
Something went wrong with that request. Please try again.