I got a few questions about how input images are handled in Tesseract (with leptonica). What I'm trying to do here is to have a method that can process any image file (no specific format requiered) and feed it later to the tesseract API, but this doesn't seems to be the right way of doing things with leptonica...
Here is an exemple of what I'm doing:
string tmpFile ="path/to/my/file";
// Trying to load a PIXA struct, since it can handle multipage images
PIXA* sourceImg =pixaRead(tmpFile.c_str());
if (sourceImg == NULL) {
// this happen when pixaRead method fails to load the image
// So we suppose it's a single page image-file.
sourceImg =new PIXA;
sourceImg->n =1;
sourceImg->pix =(Pix**)malloc(sizeof(Pix*));
assert(sourceImg->pix != NULL);
sourceImg->pix[0] =pixRead(tmpFile.c_str());
sourceImg->refcount =1;
}
api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
// Now we can process each pages
for(int i=0; i<sourceImg->n; i++) {
// results is an object I use to save text from each documents,
// with page count
if (i > 0)
results.addPage();
Pix* image =sourceImg->pix[i];
api->SetImage(image);
// Get OCR result
outText = api->GetUTF8Text();
// Here I process stuff, not really important
int dummyPos=0;
results.addLine(outText, dummyPos, dummyPos, dummyPos, dummyPos);
delete [] outText;
}
pixaDestroy(&sourceImg);
api->End();
So this is working, but not in the way I want, because even if I use a multipage tiff I got the following message when loading the image:
Error in pixaReadStream: not a pixa file
Error in pixaRead: pixa not read
It's still able to process the document thank's to the "pixRead" method I use in case the "pixaRead" fails...
Can someone explain to me what is wrong with my use of the "pixaRead" function? And is it possible to handle single and multipage images with something like that?
PS: I'm using Tesseract V4.0 and Leptonica V1.74.4
Thanks in advance!
Use pixaReadMultipageTiff
to read TIFF image (single or multiple pages) and pixRead
for other image formats.