srividya
srividya

Reputation: 419

Get the text position in tesseract Ocr in iphone

Using tesseract I have extract the text iPhone.Now want extract text along with the text position in xml. I uset GetHocrText which retrieves the text in HTML.

For eg:-

<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>

Is there is any other way to extract text in XML format in tesseract OCR?

Thanks in adv

Srividya

Upvotes: 1

Views: 2170

Answers (2)

Kaolin Fire
Kaolin Fire

Reputation: 2521

The better way to do it is to use ResultIterator; you can use tesseract::RIL_BLOCK, tesseract::RIL_PARA, tesseract::RIL_TEXTLINE, tesseract::RIL_WORD, or tesseract::RIL_SYMBOL

From 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));
}

Upvotes: 3

Kaolin Fire
Kaolin Fire

Reputation: 2521

It's not xml, but this is one way to get the positions for each character:

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;
}

Upvotes: 1

Related Questions