網頁

2018年10月26日 星期五

Tesseract ResultIterator.cpp

// 參照 Tesseract API for VS2017
// 依據字符辨識,列出所有候選字
#include "pch.h"
#include <iostream>

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
Pix *image = pixRead("D:\\TensorFlow\\OCR\\aaa.png");
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "eng")) {
std::cerr << "Could not initialize tesseract.\n";
}
api->SetPageSegMode(tesseract::PSM_AUTO_OSD);
api->SetImage(image);
api->Recognize(0);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
//tesseract::PageIteratorLevel level = tesseract::RIL_TEXTLINE;
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);
if (level = tesseract::RIL_SYMBOL) {
// 列出所有可能的候選字
tesseract::ChoiceIterator ci(*ri);
do {
const char* choice = ci.GetUTF8Text();
printf("\t\t%s conf: %f\n", choice, ci.Confidence());
} while (ci.Next());
printf("---------------------------------------------\n");
}
delete[] word;
} while (ri->Next(level));
}

api->End();
pixDestroy(&image);
}

沒有留言:

張貼留言