I have used Tesseract OCR iOS to scan text, and I have gotten it to work with a photo included in the project.
But when passing it a UIImage from the UIImagePickerController, it does not work. I set up this simple test:
- Take the original image from the picker, and feed it to tesseract: Does not work.
- Save the UIImage as JPEG, copy it from the app container, include it in the project and feed it to tesseract: Does not work.
- Open the saved UIImage in photoshop, and save it again (no changes with default JPEG quality 12 settings). Include it in the project at feed it to tesseract: Works?!?
Tesseract does recognize the correct amount of lines in the original, but as garbage (i tested several example tests). Once saved in Photoshop the image has a good recognition rate.
I simply cannot figure out what is wrong with the original UIImage that Photoshop somehow fixes. Please help!
Here are the images:
The code for feeding images to tesseract:
- (void)recognizeWithImage:(UIImage *)image {
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLanguage:@"dan"];
operation.tesseract.image = image;
self.imageView.image = image;
operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) {
NSLog(@"Result:\n%@", [recognizedTesseract recognizedText]);
};
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
Here is the code for getting the image from camera:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
NSData *dataForJPEGFile = UIImageJPEGRepresentation(originalImage, 1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [paths[0] stringByAppendingPathComponent:@"temp_ocr_image_orig.jpg"];
[dataForJPEGFile writeToFile:filePath atomically:YES];
[self recognizeWithImage:originalImage];
}
And the testing of the two image files:
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_orig.jpg"]];
[self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_photoshopped.jpg"]];