APIExample - kana112233/tesseract GitHub Wiki
#API示例
这个wiki提供了有关如何在C ++中使用tesseract-ocr API(v3.02.02-4.0.0)的简单示例.
预计tesseract-ocr已正确安装,包括所有依赖.
预计用户熟悉C ++,在他们的平台上编译和链接程序,虽然基础编译示例包含在初学者的Linux中.
有关tesseract-ocr API的更多详细信息,请参见baseapi.h.
#基本示例
码:
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
// Open input image with leptonica library
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->SetImage(image);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText);
// Destroy used object and release memory
api->End();
delete [] outText;
pixDestroy(&image);
return 0;
}
该程序必须链接到tesseract-ocr和leptonica库.
If you want to restrict recognition to a sub-rectangle of the image - call SetRectangle(left, top, width, height) after SetImage. Each SetRectangle clears the recogntion results so multiple rectangles can be recognized with the same image. E.g.
api->SetRectangle(30, 86, 590, 100);
#GetComponentImages示例
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "eng");
api->SetImage(image);
Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL, NULL);
printf("Found %d textline image components.\n", boxes->n);
for (int i = 0; i < boxes->n; i++) {
BOX* box = boxaGetBox(boxes, i, L_CLONE);
api->SetRectangle(box->x, box->y, box->w, box->h);
char* ocrResult = api->GetUTF8Text();
int conf = api->MeanTextConf();
fprintf(stdout, "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s",
i, box->x, box->y, box->w, box->h, conf, ocrResult);
}
#结果迭代器示例
可以从ResultIterator获取每个单词的置信度值和BoundingBox:
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "eng");
api->SetImage(image);
api->Recognize(0);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
if (ri != 0) {
do {
const char* word = ri->GetUTF8Text(level);
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);
delete[] word;
} while (ri->Next(level));
}
也可以使用其他迭代器级别(块,行,字等),参见[PageiteratorLevel](https://github.com/tesseract-ocr/tesseract/blob/master/src/ccstruct/publictypes.h #L219).
#方向和脚本检测(OSD)示例
const char* inputfile = "/usr/src/tesseract/testing/eurotext.tif";
tesseract::Orientation orientation;
tesseract::WritingDirection direction;
tesseract::TextlineOrder order;
float deskew_angle;
PIX *image = pixRead(inputfile);
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init("/usr/src/tesseract/", "eng");
api->SetPageSegMode(tesseract::PSM_AUTO_OSD);
api->SetImage(image);
api->Recognize(0);
tesseract::PageIterator* it = api->AnalyseLayout();
it->Orientation(&orientation, &direction, &order, &deskew_angle);
printf("Orientation: %d;\nWritingDirection: %d\nTextlineOrder: %d\n" \
"Deskew angle: %.4f\n",
orientation, direction, order, deskew_angle);
结果代码的说明在[publictypes.h]中(https://github.com/tesseract-ocr/tesseract/blob/master/src/ccstruct/publictypes.h#L120)
#单个符号的分类器选择上的迭代器示例
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "eng");
api->SetImage(image);
api->SetVariable("save_blob_choices", "T");
api->SetRectangle(37, 228, 548, 31);
api->Recognize(NULL);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
if(ri != 0) {
do {
const char* symbol = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
if(symbol != 0) {
printf("symbol %s, conf: %f", symbol, conf);
bool indent = false;
tesseract::ChoiceIterator ci(*ri);
do {
if (indent) printf("\t\t ");
printf("\t- ");
const char* choice = ci.GetUTF8Text();
printf("%s conf: %f\n", choice, ci.Confidence());
indent = true;
} while(ci.Next());
}
printf("---------------------------------------------\n");
delete[] symbol;
} while((ri->Next(level)));
}
#在Linux上编译C ++ API程序
包含和链接到Tesseract的API是以标准的Linux方式完成的. 要针对API编译基本程序,您可以使用如下命令:
g++ -o myprogram myprogram.cpp -llept -ltesseract
如果Tesseract安装在一个不寻常的地方,你可以使用g ++的-I和-L标志明确指定include和lib目录,如下所示:
g++ -o myprogram myprogram.cpp -I/home/nick/local/include/tesseract -L/home/nick/local/lib -llept -ltesseract
#python中的C-API
版本3.02.02的Tesseract-ocr提供了C-API. 这使得在python中使用tesseract-ocr共享库(和其他可以使用C的语言) 库):
import os
import ctypes
lang = "eng"
filename = "/usr/src/tesseract-ocr/phototest.tif"
libname = "/usr/local/lib64/libtesseract.so.3"
TESSDATA_PREFIX = os.environ.get('TESSDATA_PREFIX')
if not TESSDATA_PREFIX:
TESSDATA_PREFIX = "../"
tesseract = ctypes.cdll.LoadLibrary(libname)
tesseract.TessVersion.restype = ctypes.c_char_p
tesseract_version = tesseract.TessVersion()
api = tesseract.TessBaseAPICreate()
rc = tesseract.TessBaseAPIInit3(api, TESSDATA_PREFIX, lang)
if (rc):
tesseract.TessBaseAPIDelete(api)
print("Could not initialize tesseract.\n")
exit(3)
text_out = tesseract.TessBaseAPIProcessPages(api, filename, None, 0)
result_text = ctypes.string_at(text_out)
print 'Tesseract-ocr version', tesseract_version
print result_text
可以在pastebin找到将python文件对象传递给C-API的示例.
从Tesseract 4.0中提取方向的示例:
# /usr/bin/env python3
# coding: utf-8
PATH_TO_LIBTESS = '/path/to/development/libtesseract.so'
import cffi # requires "pip install cffi"
ffi = cffi.FFI()
ffi.cdef("""
struct Pix;
typedef struct Pix PIX;
PIX * pixRead ( const char *filename );
char * getLeptonicaVersion ( );
typedef struct TessBaseAPI TessBaseAPI;
typedef int BOOL;
const char* TessVersion();
TessBaseAPI* TessBaseAPICreate();
int TessBaseAPIInit3(TessBaseAPI* handle, const char* datapath, const char* language);
void TessBaseAPISetImage2(TessBaseAPI* handle, struct Pix* pix);
BOOL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, char** best_script_name,
int* best_orientation_deg, float* script_confidence,
float* orientation_confidence);
""")
libtess = ffi.dlopen(PATH_TO_LIBTESS)
from ctypes.util import find_library
liblept = ffi.dlopen(find_library('lept'))
ffi.string(libtess.TessVersion())
ffi.string(liblept.getLeptonicaVersion())
api = libtess.TessBaseAPICreate()
libtess.TessBaseAPIInit3(api, ffi.NULL, ffi.NULL)
pix = liblept.pixRead('mono.png'.encode())
libtess.TessBaseAPISetImage2(api, pix)
script_name = ffi.new('char **')
orient_deg = ffi.new('int *')
script_conf = ffi.new('float *')
orient_conf = ffi.new('float *')
libtess.TessBaseAPIDetectOrientationScript(api, script_name, orient_deg, script_conf, orient_conf)
print(ffi.string(script_name).decode('utf-8'))
print(orient_deg[0])
print(script_conf[0])
print(orient_conf[0])
#在C程序中使用C-API的示例
当然,C-API也可以由常规C程序使用,就像在这个非常基本的例子中一样.
#include <stdio.h>
#include <allheaders.h>
#include <capi.h>
void die(const char *errstr) {
fputs(errstr, stderr);
exit(1);
}
int main(int argc, char *argv[]) {
TessBaseAPI *handle;
PIX *img;
char *text;
if((img = pixRead("img.png")) == NULL)
die("Error reading image\n");
handle = TessBaseAPICreate();
if(TessBaseAPIInit3(handle, NULL, "eng") != 0)
die("Error initializing tesseract\n");
TessBaseAPISetImage2(handle, img);
if(TessBaseAPIRecognize(handle, NULL) != 0)
die("Error in Tesseract recognition\n");
if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
die("Error getting text\n");
fputs(text, stdout);
TessDeleteText(text);
TessBaseAPIEnd(handle);
TessBaseAPIDelete(handle);
pixDestroy(&img);
return 0;
}
在Linux上,您可以编译它,就像使用C ++ API构建程序一样.
#include <leptonica/allheaders.h>
#include <tesseract/baseapi.h>
#include <tesseract/renderer.h>
int main()
{
const char* input_image = "/usr/src/tesseract-oc/testing/phototest.tif";
const char* output_base = "my_first_tesseract_pdf";
const char* datapath = "/Projects/OCR/tesseract/tessdata";
int timeout_ms = 5000;
const char* retry_config = nullptr;
bool textonly = false;
int jpg_quality = 92;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(datapath, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
tesseract::TessPDFRenderer *renderer = new tesseract::TessPDFRenderer(
output_base, api->GetDatapath(), textonly, jpg_quality);
bool succeed = api->ProcessPages(input_image, retry_config, timeout_ms, renderer);
if (!succeed) {
fprintf(stderr, "Error during processing.\n");
return EXIT_FAILURE;
}
api->End();
return EXIT_SUCCESS;
}
#include <tesseract/baseapi.h>
#include <tesseract/ocrclass.h>
#include <leptonica/allheaders.h>
#include <thread>
void monitorProgress(ETEXT_DESC *monitor, int page);
void ocrProcess(tesseract::TessBaseAPI *api, ETEXT_DESC *monitor);
void monitorProgress(ETEXT_DESC *monitor, int page) {
while (1) {
printf( "\r%3d%%", monitor[page].progress);
fflush (stdout);
if (monitor[page].progress==100)
break;
}
}
void ocrProcess(tesseract::TessBaseAPI *api, ETEXT_DESC *monitor) {
api->Recognize(monitor);
}
int main() {
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
ETEXT_DESC *monitor = new ETEXT_DESC();
if (api->Init("/tesseract/tessdata_best", "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
return 1;
}
api->SetPageSegMode(tesseract::PSM_AUTO);
Pix *image = pixRead("/tesseract-ocr/test/testing/phototest.tif");
if (!image) {
fprintf(stderr, "Leptonica can't process input file!\n");
return 2;
}
api->SetImage(image);
int page = 0;
std::thread t1(ocrProcess, api, monitor);
std::thread t2(monitorProgress, monitor, page);
t1.join();
t2.join();
pixDestroy(&image);
char *outText = api->GetUTF8Text();
printf("\n%s", outText);
if (outText)
delete [] outText;
api->End();
return 0;
}
更复杂的例子(例如取消OCR过程)可以在TesseractGui,gimagereader的源代码中找到 .org/linux/gimagereader/qt/src/Recognizer.cc)或android textfairy 应用.