Search code examples
androidopencvocrtess-two

Coordinates of a character/word [OCR app]


So basically, I'm creating an android app (using tesseract and OpenCV) which when given a word after pre-processing and scan steps, draws a rectangle around that word - basically "finds" the word and marks it. However I'm wondering how to get coordinates of a character ? or atleast a word ? I have coordinates of each line, but the coordinates are not relative to the "main-picture", but only coordinates of "text-blocks" that I have. Maybe someone has/knows either explanation/tutorial or some kind of info on how to go about finding coordinates of a word/character. Would highly appreciate.


Solution

  • This sample code, taken from the API Examples Wiki page from tesseract should help: APIExamples

    Focus on those 2 lines: int x1, y1, x2, y2; ri->BoundingBox(level, &x1, &y1, &x2, &y2);

    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);
          int x1, y1, x2, y2;
          ri->BoundingBox(level, &x1, &y1, &x2, &y2);
          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)));
    }