Tesseract OCR iOS Image Format

I used Tesseract OCR iOS to scan the text, and I got it to work with the photograph included in the project.

But when passing UIImage from UIImagePickerController, this will not work. I installed this simple test:

  • Take the original image from the collector and submit it to tesseract: Does not work .
  • Save the UIImage as a JPEG, copy it from the application container, include it in the project and submit it to tesseract: It does not work .
  • Open the saved UIImage in Photoshop and save it again (no change with the default JPEG quality settings of 12). Include it in the project to submit it to tesseract: Works?!?

Tesseract recognizes the correct number of lines in the original, but as garbage (I tested a few test cases). After saving in Photoshop, the image has a good recognition speed.

I just can’t understand what’s wrong with the original UIImage, which Photoshop somehow fixes. Please, help!

Here are the images:

Code for submitting 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 to get the image from the 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 testing two image files:

 [self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_orig.jpg"]]; [self recognizeWithImage:[UIImage imageNamed:@"temp_ocr_image_photoshopped.jpg"]]; 
+5
source share
1 answer

image orientation is different for both images. When uploading images to the engine: in your case, both images are created as images with different orientation to the engine:

Here's how they look in front of the engine:

Original Image:

enter image description here

Image in Photoshop:

enter image description here

If you look carefully, they are both presented differently. I believe UIImageJPEGRepresentation doing something crazy or when you write image to container , the image gets in a different orientation.

You need to change the orientation of the image that you receive from the collector or from your container.

I made several combinations to get the correct orientation as a 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 used 3 spins here.

+3
source

Source: https://habr.com/ru/post/1238635/


All Articles