3

how to convert image to text using iOS swift ?

Step 01: Take a photo using iOS camera . (Done by using UIImagePickerController in iOS swift)

Step 02: I got image .

Step 03 : I have to convert these image (UIImage) to text format .

using iOS swift .

I have referred many links as already we have VNDetectTextRectanglesRequest for identifying character box .

but my purpose is how we convert from image to text . Not as an rectangular boxes using iOS swift

suripappu Rengan
  • 61
  • 1
  • 1
  • 5

4 Answers4

2

Using CoreML's VNDetectTextRectanglesRequest, you only able to find regions of visible text in an image. And, thats not enough to get text out from an image with swift.

First step is to crop the images, You will need to crop the images for each image in VNTextObservation. Like

    for textObservation in textObservations {
        guard let rects = textObservation.characterBoxes else {
            continue
        }
        var xMin = CGFloat.greatestFiniteMagnitude
        var xMax: CGFloat = 0
        var yMin = CGFloat.greatestFiniteMagnitude
        var yMax: CGFloat = 0
        for rect in rects {

            xMin = min(xMin, rect.bottomLeft.x)
            xMax = max(xMax, rect.bottomRight.x)
            yMin = min(yMin, rect.bottomRight.y)
            yMax = max(yMax, rect.topRight.y)
        }
       let imageRect = CGRect(x: xMin * size.width, y: yMin * size.height, width: (xMax - xMin) * size.width, height: (yMax - yMin) * size.height)

Second step is to send images to image processing tools like Opencv etc,.there are some online tutorials about how to integrate with iOS and you can use objective-c header if you want to use it with swift. https://medium.com/pharos-production/using-opencv-in-a-swift-project-679868e1b798

Once you got processed image, Third step is As mentioned by Nick,
you then use tesseract or ABBYY SDK's.

Tesseract is free to use and you can find iOS framework for tesseract 3.03-rc1 here. The most important thing you need to aware about OCR tools is language. What language you try to convert ? what language the detected image has ? Mostly you got trained data for multiple languages in the tesseract repository. In Summary, the work flow will be ,

Image Capture -> Image Process -> OCR process

Selva
  • 60
  • 8
0

If you need to convert the image to text for OCR then you can use the following links:-

There is no in-built libraries for OCR but you can use the following links

1) Open Source OCR - Tesseract http://code.google.com/p/tesseract-ocr/ - completely free, but less accurate.

This link will show how to run in iPhone: https://github.com/nolanbrown/Tesseract-iPhone-Demo

2) Commercial OCR - http://abbyy.com/mobileocr/iphone - highly accurate, customer support, etc. but it costs money.

Swati Gautam
  • 191
  • 9
  • Thans swati .- (NSString ) ocrImage: (UIImage *) uiImage { //code from robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043 . char text = tess->TesseractRect(imageData,(int)bytes_per_pixel,(int)bytes_per_line, 0, 0,(int) imageSize.height,(int) imageSize.width); // Do something useful with the text! NSLog(@"Converted text: %@",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]); return [NSString stringWithCString:text encoding:NSUTF8StringEncoding]; } . HOW WE CAN USE tess->TesseractRect SWIFT IOS . – suripappu Rengan Mar 07 '18 at 11:04
0

I think you talk about extracting text from images A process called cOCR "Optical character recognition"

Read : https://en.wikipedia.org/wiki/Optical_character_recognition IOS has no builtIn OCR SDK/library

i highly advice that you check Tesseract, an open-source OCR engine maintained by Google . https://github.com/tesseract-ocr/tesseract

also you can found a complete swift4 article here

https://www.raywenderlich.com/163445/tesseract-ocr-tutorial-ios

also remember to capture high quality picture before OCR process .

Ayman Ferki
  • 196
  • 7
  • Thanks Ayman .- (NSString ) ocrImage: (UIImage *) uiImage { //code from robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043 . char text = tess->TesseractRect(imageData,(int)bytes_per_pixel,(int)bytes_per_line, 0, 0,(int) imageSize.height,(int) imageSize.width); // Do something useful with the text! NSLog(@"Converted text: %@",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]); return [NSString stringWithCString:text encoding:NSUTF8StringEncoding]; } . HOW WE CAN USE tess->TesseractRect SWIFT IOS . – suripappu Rengan Mar 07 '18 at 11:04
  • Thanks Aymen. Really great to archieve this in ios swift 4 recent version. thanks for help . shall we get text from existing photo . Eg: i have wriiten in a papper as "Hello Ayman" and take a photo . use this existion Hello Ayman photo . shall we find text from existing image? – suripappu Rengan Mar 07 '18 at 12:01
  • yes you can read text from exiting photo . but it's depends on a lot of factor ... why you didn't try it first . then try to fix any problem appear . also it's better if you upvote any useful answers – Ayman Ferki Mar 07 '18 at 16:27
0

If you need to recognise text from image then you can refer this:

1) Tesseract OCR : https://github.com/cconstable/OCR-iOS-Example

2) ABBYY : http://abbyy.com/mobileocr/iphone

3) Google Cloud Vision : https://cloud.google.com/vision/

Tesseract OCR was more accurate depending on the image resolution, fonts, text color etc.

Nick
  • 875
  • 6
  • 20
  • thanks Nick . shall we get text from existing photo . Eg: i have wriiten in a papper as "Hello Nick" and take a photo . use this existion hello Nick photo . shall we find text from existing image? – suripappu Rengan Mar 07 '18 at 11:57
  • Using Tesseract OCR I'm not sure but I think not possible to get text from hand written note. – Nick Mar 08 '18 at 12:38