在iphone中获取tesseract Ocr中的文本位置

时间:2012-09-03 04:52:12

标签: iphone tesseract

使用tesseract我已经提取了文本iPhone.Now想要提取文本以及xml中的文本位置。我使用GetHocrText来检索HTML中的文本。

例如: -

<span class='ocr_word' id='word_3_28' title="bbox 55 226 123 243">
<span class='ocrx_word' id='xword_3_28' title="x_wconf -5">Beverage</span>
</span>

是否还有其他方法可以在tesseract OCR中以XML格式提取文本?

先谢谢

斯里韦德亚

2 个答案:

答案 0 :(得分:3)

更好的方法是使用ResultIterator;你可以使用tesseract :: RIL_BLOCK,tesseract :: RIL_PARA,tesseract :: RIL_TEXTLINE,tesseract :: RIL_WORD或tesseract :: RIL_SYMBOL

来自https://code.google.com/p/tesseract-ocr/wiki/APIExample

tesseract::TessBaseAPI api;
// tesseract.Init here
api.SetVariable("save_blob_choices", "T");
// tesseract.SetImage/tesseract.SetRectangle here
api.Recognize(NULL);

tesseract::ResultIterator* ri = api.GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
if (ri) {
  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));
}

答案 1 :(得分:1)

这不是xml,但这是获取每个角色位置的一种方法:

tesseract::TessBaseAPI tesseract;
// tesseract.Init here
tesseract.SetVariable("save_blob_choices", "T"); // for character-level confidence
// tesseract.SetImage/tesseract.SetRectangle here
char *results_as_text = tesseract.GetBoxText(0);  // characters without spaces/newlines artificially embedded
std::istringstream results_as_stream(results_as_text);
std::string result;
char letter;
int x1, y1, x2, y2;
while (std::getline(results_as_stream,result)) {
    std::istringstream result_stream(result);
    result_stream >> letter;
    result_stream >> x1;
    result_stream >> y1;
    result_stream >> x2;
    result_stream >> y2;
    std::cout << letter << " ((" << x1 << "," << y1 << "),(" << x2 << "," << y2 << "))" << std::endl;
}