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:
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"]];
The image
orientation
is different for both the images.When you load the images into the engine:In your case both the images are produced as images with different orientations to the engine:
Here is how they look infront of the engine:
Original Image:
Photoshop image:
If you look closely, they both are presented differently.I believe UIImageJPEGRepresentation
is doing something crazy or when you are writing the image
to the container
, the image gets into different orientation.
You need way to modify the orientation of image that you get from the picker or from your container .
I did some combinations to get the correct orientation as the photoshop image:
//image is the original image
UIImage *imageToDisplay =[UIImage imageWithCGImage:[image CGImage]
scale:1.0
orientation: UIImageOrientationRight];
UIImage *newImage= [UIImage imageWithCGImage:[imageToDisplay CGImage]
scale:1.0
orientation: UIImageOrientationDown];
UIImage *newImage2= [UIImage imageWithCGImage:[newImage CGImage]
scale:1.0
orientation: UIImageOrientationLeft];
//Now I get the correct orientation
// Set the image on which Tesseract should perform recognition
operation.tesseract.image = newImage2 ;
And now you can get the text from OCR as expected.
You should try to get the correct orientation in one line of code. I have used 3 rotations here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With