• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Swift Optical Character Recognition Tutorial

Home ios Swift Optical Character Recognition Tutorial

Swift Optical Character Recognition Tutorial

May 12, 2018 | Posted by Andrew | ios, swift, tutorial, xcode |

By the end of this tutorial you will be able to use the Tesseract OCR library to recognize text in images. I have tried the following methods with the following success rates on the images in this tutorial

  • Core ML – 50%
  • Swift OCR – 70-80%
  • Tesseract OCR – 100%

Setting up Tesseract OCR

We will be using CocoaPods to install the Tesseract OCR library for Swift. If you are unfamilar with CocoaPods check out the tutorial here.  So to get going create a new single view application in Swift.  We named ours ‘SeemuOCR’. Once you have created the project navigate to the project folder in terminal and run pod init to setup the Podfile.

Once you have your podfile setup add the following line as follows to our podfile:

pod ‘TesseractOCRiOS’, ‘4.0.0’

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'SeemuOCR' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SeemuOCR
  pod 'TesseractOCRiOS', '4.0.0'
  
  target 'SeemuOCRTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'SeemuOCRUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

Save this file and back in terminal run pod install to install TesseractOCR.

pod install

Before we go any further go and download the contents needed for the tutorial here. Once you have the contents go to the project folder and create a folder called ‘tessdata’ in the following location in your project (The same level as AppDelegate and ViewController). Once you have created this add the english training data.

A quick note – this training data is a specific version (3), the latest tesseract data files do not currently work with this library.

Next up open up the .xcworkspace project folder. With finder open drag the ‘tessdata’ folder you created into XCode under the project as follows. Once it’s in the project the folder should be blue (This means it matches the file system, rather then a link) otherwise it won’t work.

Recognizing text in images with Swift

Add the images from the contents to the images.xcassets folder:

Then add the following code to the View Controller

import UIKit
import TesseractOCR

class ViewController: UIViewController, G8TesseractDelegate {

    let tesseract:G8Tesseract = G8Tesseract(language: "eng")
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        print("Running OCR")
        
        tesseract.delegate = self
        tesseract.charWhitelist = "0123456789"
        
        var imageToCheck = UIImage(named: "1")
        tesseract.image = imageToCheck
        tesseract.recognize()
        print("The 1 text is \(tesseract.recognizedText!)")
        
        imageToCheck = UIImage(named: "3")
        tesseract.image = imageToCheck
        tesseract.recognize()
        print("The 3 text is \(tesseract.recognizedText!)")
        
        imageToCheck = UIImage(named: "6")
        tesseract.image = imageToCheck
        tesseract.recognize()
        print("The 6 text is \(tesseract.recognizedText!)")
        
        imageToCheck = UIImage(named: "9")
        tesseract.image = imageToCheck
        tesseract.recognize()
        print("The 9 text is \(tesseract.recognizedText!)")
        
    }
    
    func shouldCancelImageRecognition(for tesseract: G8Tesseract!) -> Bool {
        return false
    }

}

The code is pretty self explanatory – run it and you will see the results printed to the console, it picks up what the text is in the image every time!

This library also has options you can optionally setup. For example you will have noticed we used ‘charWhiteList’ – this will set a list of characters that the library will only detect, essentially we are saying our images only contain numbers so only look for this.

You can also do the opposite and set a Black List of characters you don’t want the image to detect.

Download source code

Tags: aicharactermlocr
4
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Replace occurrences of a character in a String

May 6, 2016

Replacing a certain occurrence of a character in a string,[...]

Get nth character of a String & get a Substring from a String

Apr 18, 2016

To get n character of a String in Swift you[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next