Teja Nandamuri
Teja Nandamuri

Reputation: 11201

Tesseract OCR not recognizing the image taken from device

I'm using the https://github.com/gali8/Tesseract-OCR-iOS/ to make an app that detects text on business cards.

I'm stuck at making the Tesseract detect the text in image.

If I pass the image through code, Tesseract is able to detect it. If I provide the image taken from the camera, tesseract is not able to recognize it.

-(void)startTess:(UIImage *)img{

 G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
 tesseract.delegate = self;
 tesseract.engineMode=G8OCREngineModeTesseractCubeCombined;

 // Optional: Limit the character set Tesseract should try to recognize from
 tesseract.charWhitelist = @"@.,()-,abcdefghijklmnopqrstuvwxyz0123456789";

 // Specify the image Tesseract should recognize on
 tesseract.image = [img g8_blackAndWhite];

 // Optional: Limit the area of the image Tesseract should recognize on to a rectangle
 CGRect tessRect = CGRectMake(0, 0, img.size.width, img.size.height);
 tesseract.rect = tessRect;

 // Optional: Limit recognition time with a few seconds
 tesseract.maximumRecognitionTime = 4.0;

 // Start the recognition
 [tesseract recognize];

 // Retrieve the recognized text
 NSLog(@"text %@", [tesseract recognizedText]);

 // You could retrieve more information about recognized text with that methods:
 NSArray *characterBoxes = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelSymbol];
 NSArray *paragraphs = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelParagraph];
 NSArray *characterChoices = tesseract.characterChoices;
 UIImage *imageWithBlocks = [tesseract imageWithBlocks:characterBoxes drawText:YES thresholded:NO];

 self.imgView.image = imageWithBlocks;

 NSString * result = [[characterBoxes valueForKey:@"description"] componentsJoinedByString:@"\n"];

 _txtView.text=result;


}

Result when image provided from .xcassets:

enter image description here

Result when image taken directly from the camera:

enter image description here

In both the cases, Tesseract is recognizing the empty space with some random characters. I marked that area in both the images (top-left portion of image).

I made sure that image taken from device camera has the orientation up, as some reported Tesseract doesn't recognize the image taken from camera as it has 180 degree shift.

UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];

// Redraw the image (if necessary) so it has the corrent orientation:
if (chosenImage.imageOrientation != UIImageOrientationUp) {
    UIGraphicsBeginImageContextWithOptions(chosenImage.size, NO, chosenImage.scale);
    [chosenImage drawInRect:(CGRect){0, 0, chosenImage.size}];
    chosenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

What is the best way of debugging this and going forward ?

I submitted an issue at git: https://github.com/gali8/Tesseract-OCR-iOS/issues/358

Edit:

I have changed the iterator level to G8PageIteratorLevelTextline, and now the image taken by device camera gives the following output:

enter image description here

Still it is not accurate. If someone can point out on how to improve this, it would be nice.

Upvotes: 2

Views: 1554

Answers (1)

Mousam Singh
Mousam Singh

Reputation: 755

On the official github source of tesseract there are various preprocessing methods mentioned and along with those measures I would suggest using .tiff images instead of .jpg or .png because using any other kind of image other than tiff compresses the image and reduces it binarizing quality.

Upvotes: 0

Related Questions