Fast and simple OCR library written in Swift

Overview

⛔️ This Project is deprecated and no longer gets maintained!

Please use Apple's Vision framework instead of SwiftOCR. It is very fast, accurate and much less finicky.


Deprecated Carthage compatible CocoaPods Compatible Platform

SwiftOCR

SwiftOCR is a fast and simple OCR library written in Swift. It uses a neural network for image recognition. As of now, SwiftOCR is optimized for recognizing short, one line long alphanumeric codes (e.g. DI4C9CM). We currently support iOS and OS X.

Features

  • Easy to use training class
  • High accuracy
  • Great default image preprocessing
  • Fast and accurate character segmentation algorithm
  • Add support for lowercase characters
  • Add support for connected character segmentation

Why should I choose SwiftOCR instead of Tesseract?

This is a really good question.

If you want to recognize normal text like a poem or a news article, go with Tesseract, but if you want to recognize short, alphanumeric codes (e.g. gift cards), I would advise you to choose SwiftOCR because that's where it exceeds.

Tesseract is written in C++ and over 30 years old. To use it you first have to write a Objective-C++ wrapper for it. The main issue that's slowing down Tesseract is the way memory is managed. Too many memory allocations and releases slow it down.

I did some testing on over 50 difficult images containing alphanumeric codes. The results where astonishing. SwiftOCR beat Tesseract in every category.

SwiftOCR Tesseract
Speed 0.08 sec. 0.63 sec.
Accuracy 97.7% 45.2%
CPU ~30% ~90%
Memory 45 MB 73 MB

How does it work?

  1. Input image is thresholded (binarized).
  2. Characters are extracted from the image, using a technique called Connected-component labeling.
  3. Separated characters are converted into numbers, which are then fed into the neural network.

How to use it?

SwiftOCR is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftOCR'

If you ever used Tesseract you know how exhausting it can be to implement OCR into your project. SwiftOCR is the exact opposite of Tesseract. It can be implemented using just 6 lines of code.

import SwiftOCR

let swiftOCRInstance = SwiftOCR()
    
swiftOCRInstance.recognize(myImage) { recognizedString in
    print(recognizedString)
}

To improve your experience with SwiftOCR you should set your Build Configuration to Release.

Training

Training SwiftOCR is pretty easy. There are only a few steps you have to do, before it can recognize a new font.

The easiest way to train SwiftOCR is using the training app that can be found under /example/OS X/SwiftOCR Training. First select the fonts you want to train from the list. After that, you can change the characters you want to train in the text field. Finally, you have to press the Start Testing button. The only thing that's left now, is waiting. Depending on your settings, this can take between a half and two minutes. After about two minutes you may manually stop the training. Pressing the Save button will save trained network to your desktop. The Test button is used for evaluating the accuracy of the trained neural network.

Examples

Here is an example image. SwiftOCR has no problem recognizing it. If you try to recognize the same image using Tesseract the output is 'LABMENSW' ?!?!?.

Image 1

This image is difficult to recognize because of two reasons:

  • The lighting is uneven. This problem is solved by the innovative preprocessing algorithm of SwiftOCR.
  • The text in this image is distorted. Since SwiftOCR uses a neural network for the recognition, this isn't a real problem. A NN is flexible like a human brain and can recognize even the most distorted image (most of the time).

TODO

Dependencies

License

The code in this repository is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

NOTE: This software depends on other packages that may be licensed under different open source licenses.

Comments
  • Low reco count even using example on iOS

    Low reco count even using example on iOS

    I'm testing the sample app that comes with the package and when I run it the only way I can get it to accurately recognise alpha numerics is to type them in GIMP and test off my screen.

    I have tried various texts, all single line from number plates, to single words to just numbers and I only get 100% hit when using typed text off my computer screen?

    opened by piginhat 13
  • Errors

    Errors

    I get these errors when trying run the build.

    Cannot invoke 'draw' with an argument list of type '(in: CGRect, withAttributes: [NSAttributedStringKey : Any])’

    NSString(string: code).draw(in: CGRect(origin: CGPoint(x: 0 + randomFloat(5), y: -15.5 + randomFloat(5)), size: randomImg.size), withAttributes: randomFontAttributes())

    Type 'NSString' has no member 'font' return [.font: randomFont(), .kern: CGFloat(8) as NSObject, .foregroundColor: OCRColor(red: 27/255 + randomFloat(0.2), green: 16/255 + randomFloat(0.2), blue: 16/255 + randomFloat(0.2), alpha: 80/100 + randomFloat(0.2)), .paragraphStyle: paragraphStyle] }

    opened by rwalt04 5
  • training ?

    training ?

    Hello

    I am trying to understand what do to when the app is trained ? how do I get back the training neural network to the camera app ?

    Also in the SwiftOCR Camera app there is also SwiftOCRTraining, which is never used what is its purpose?

    opened by raphaels17 4
  • Recognising numbers

    Recognising numbers

    Sorry to open another issue so soon (is an email better?) Despite adding numbers to the training list and adding them to the whitelist I cannot get numbers to be recognised. img_1951

    BTW we're trying to get this working to capture a Mac address with - seperators, any tips you might have would be appreciated.

    opened by LeoSnek 4
  • how to start an example?

    how to start an example?

    In attempt to start an example the mistake appears

    dyld: Library not loaded: @rpath/GPUImage.framework/GPUImage
      Referenced from: /private/var/containers/Bundle/Application/97507C02-4AF1-4CE4-9D20-D6ABA8E0B050/SwiftOCR Camera.app/Frameworks/SwiftOCR.framework/SwiftOCR
      Reason: image not found
    (lldb) 
    

    Help me please to begin to work with this framework.

    El Capitan 10.11.5 (15F34), XCode Version 7.3.1 (7D1014)

    opened by rez1dent3 4
  • Issue with simple image

    Issue with simple image

    Trying sample app for OS X to recognize attached image. It results in 'F7' as recognized string. Any ideas why this may happen ? what are the requirements for fine recognition? img_2457 2

    opened by asidden 4
  • Recognizing special characters and periods

    Recognizing special characters and periods

    @garnele007 Thanks for your swiftOCR and it's working incredibly well! I am converting an image like “// TEXT.TEXT”. It does recognise the characters and provide output TEXTTEXT

    But, I want to include . and (or) / in the list of recognition characters! So that it produce the output like “TEXT.TEXT” as exactly shown on the image.

    Could you let me know if this is possible please ?

    opened by scubess 3
  • Multiple Errors.

    Multiple Errors.

    So i tried to fix those two errors for a long time now. I found nothing useful on the internet. First one: Type 'NSString' has no member 'font'

    return [.font: randomFont(),
                        .kern: CGFloat(8) as NSObject,
                        .foregroundColor: OCRColor(red: 27/255 + randomFloat(0.2), green: 16/255 + randomFloat(0.2), blue: 16/255 + randomFloat(0.2), alpha: 80/100 + randomFloat(0.2)),
                        .paragraphStyle: paragraphStyle]
    

    Second one:

    Cannot invoke 'draw' with an argument list of type '(in: CGRect, withAttributes: [NSAttributedStringKey : Any])'

    NSString(string: code).draw(in: CGRect(origin: CGPoint(x: 0 + randomFloat(5), y: -15.5 + randomFloat(5)), size: randomImg.size), withAttributes: randomFontAttributes())
    

    Thanks in advance for your answers.

    opened by LonoS 3
  • Image Training and Error

    Image Training and Error

    Hello, this seems awesome, I can't make it work, I build for example iOs version and It gives me the following error "/Users/nicolaspalacios/Library/Developer/Xcode/DerivedData/SwiftOCR_Camera-gxrakixxmfxwzmfrklrdfujhvzsp/Build/Intermediates/GPUImage.build/Debug-iphonesimulator/Documentation.build/Script-BC552B3A1558C6FC001F3FFA.sh: line 5: /usr/local/bin/appledoc: No such file or directory" Also, how can I do it so I could gave it images and categorize images based on training for something different than letters? For example Car Colores, shapes, etc?

    opened by magonicolas 3
  • recognize().indexToCharacter() - fatal error: Index out of range

    recognize().indexToCharacter() - fatal error: Index out of range

    The crash happens in its inner function indexToCharacter()

        func indexToCharacter(_ index: Int) -> Character {
            return Array(recognizableCharacters.characters)[index]
        }
    

    The index is 36.

    The recognizableCharacters is ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

    I use the latest version of SwiftOCR. commit hash: 204e6850149dfd50e507e66edf265fc58cf38178

    I have trained my own OCR-Network that I'm using. This is trained with these characters ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-

    It doesn't crash if I use the default OCR-Network file that is in the SwiftOCR repository.

    I like SwiftOCR and find it better than Tesseract.

    opened by neoneye 3
  • Read only numbers!!!

    Read only numbers!!!

    I want read only numbers. when try to internal var recognizableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" convert to internal var recognizableCharacters = "0123456789" it getting crash or sometime it read character also. so how to solve?

    thank you

    opened by NisalChathuranga 3
  • Fix compiler error (Swift 5)

    Fix compiler error (Swift 5)

    'compiler is unable to type-check this expression in reasonable time...' for the randomFloat variable. I fixed this by dividing the return value into three sub sections x, y, and z.

    opened by davidrazmadze 0
  • Carthage not working

    Carthage not working

    What I did:

    1.) Put github "garnele007/SwiftOCR" "HEAD" into Cartfile 2.) carthage update

    What happens:

    *** Building scheme "SwiftOCR Mac" in SwiftOCR.xcodeproj
    *** Building scheme "SwiftOCR" in SwiftOCR.xcodeproj
    Build Failed
    	Task failed with exit code 65:
    ..........
    
    opened by unixb0y 0
  • Statement about accuracy

    Statement about accuracy

    Hello,

    The main readme of this project makes this statement :

    Here is an example image. SwiftOCR has no problem recognizing it. If you try to recognize the same image using Tesseract the output is 'LABMENSW' ?!?!?.

    That statement is not correct. With Tesseract version 4 and higher, the output is perfect and the image does not require any preprocessing. Speaking from experience, Tesseract can successfully handle images that have far worse contrast too.

    opened by CanadianHusky 0
  • No Output String

    No Output String

    I'm not getting any output string. My code is below

        @IBAction func takePhoto(_ sender: Any) {
            
            imagePicker =  UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            
            present(imagePicker, animated: true, completion: nil)
            
            
        }
        
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            imagePicker.dismiss(animated: true, completion: nil)
            imageView.image = info[.originalImage] as? UIImage
            myImage = (info[.originalImage] as? UIImage)!
            
      
        }
        
        
        @IBAction func btnTapped(_ sender: Any) {
            print("Btn tapped")
            
            swiftOCRInstance.recognize(myImage) { recognizedString in
               // print(self.myImage)
                print(recognizedString)
             //   self.outputLbl.text = recognizedString
            }
        
        }
        
    
    opened by junaidxabd 4
Releases(1.2.1)
Owner
Nicolas Camenisch
CS Student @ MIT '25 | iOS & Android Developer
Nicolas Camenisch
Simple command-line utility for performing OCR using Apple's Vision framework

ocrit Runs Vision's OCR on input images and outputs corresponding txt files for each image, or writes the recognized results to standard output. USAGE

Guilherme Rambo 61 Nov 23, 2022
TRex 🦖 TRex makes OCR easy and accessible on a Mac

TRex ?? TRex makes OCR easy and accessible on a Mac. But what is OCR anyway? Imagine you have a PDF file or a Web page where you can't select the text

Ameba Labs 700 Dec 23, 2022
A lightweight and fast image loader for iOS written in Swift.

ImageLoader ImageLoader is an instrument for asynchronous image loading written in Swift. It is a lightweight and fast image loader for iOS. Features

Hirohisa Kawasaki 293 Nov 24, 2022
🍁🥓 Lightweight and fast Swift library for image downloading, caching and transformations

MapleBacon Introduction MapleBacon is a lightweight and fast Swift library for downloading and caching images. Example The folder Example contains a s

Jan Gorman 335 Nov 1, 2022
A Fast and Complete Swift Drawing Library

FastDraw A Fast and Complete Swift Drawing Library Description FastDraw is a high performance and highly extensible Drawing Library that supports Appl

Collin Zhang 18 Nov 14, 2022
1D, 2D, and 3D variations of Fast Fourier Transforms for a Metal S4TF backend

MetalFFT MetalFFT is an experiment in adding GPU acceleration for 1D, 2D, and 3D variations of Fast Fourier Transforms. This framework's original purp

Philip Turner 21 Oct 11, 2022
1D, 2D, and 3D variations of Fast Fourier Transforms for a Metal S4TF backend

MetalFFT MetalFFT is an experiment in adding GPU acceleration for 1D, 2D, and 3D variations of Fast Fourier Transforms. This framework's original purp

Philip Turner 21 Oct 11, 2022
A VisionCamera Frame Processor plugin for fast buffer resizing

vision-camera-resize-plugin A VisionCamera Frame Processor Plugin for fast buffer resizing. By resizing buffers to a smaller resolution, you can achie

Marc Rousavy 16 Aug 10, 2022
A simple macOS app to read code from images, written purely in Swift using Vision Framework.

CodeReader A simple macOS app to read code from images, written purely in Swift using Vision Framework. Usage Drag an image Click the convert button R

Md Ibrahim Hassan 44 Nov 20, 2022
Simple PhotoBrowser/Viewer inspired by facebook, twitter photo browsers written by swift

SKPhotoBrowser [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) Simple PhotoBrowser

keishi suzuki 2.4k Jan 6, 2023
A simple UIImageView extension for using initials as a profile image, written in swift

InitialsImageView An easy, helpful UIImageView extension that generates letter initials as a placeholder for user profile images, with a randomized ba

Tom Bachant 215 Dec 17, 2022
GPU-based media processing library using Metal written in Swift

GPU-based media processing library using Metal written in Swift. Overview MetalAcc is a GPU-Based media processing library that lets you apply GPU-acc

Jiawei Wang 259 Dec 17, 2022
Simple image crop library for iOS

PhotoCropper This is a simple image crop library for iOS I made for fun on Chris

Aaron Lee 5 Jun 21, 2022
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

Evgenii Neumerzhitckii 330 Sep 9, 2022
A beautiful and flexible text field control implementation of "Float Label Pattern". Written in Swift.

SkyFloatingLabelTextField SkyFloatingLabelTextField is a beautiful, flexible and customizable implementation of the space saving "Float Label Pattern"

Skyscanner 4k Jan 3, 2023
Rounded async imageview downloader lightly cached and written in Swift

PASImageView ============ **Rounded async imageview downloader lightly cached and written in Swift 3 ** Objective-C version here Snapshot Usage //XIB

Pierre Abi-aad 173 Nov 6, 2022
Instant camera hybrid with multiple effects and filters written in Swift.

Kontax Cam Download on the app store! No longer on the app store Kontax Cam is an instant camera built 100% using Swift for iOS. You can take your pho

Kevin Laminto 108 Dec 27, 2022
Visualiser written in Swift, SwiftUI and Metal API

ModularMTL About Visualisation of modular multiplication on a circle. Written in Swift using Metal API and SwiftUI. Images Features Keyboard controls

Gracien 12 Dec 20, 2022
A Shortcuts-like and highly customizable SFSymbol picker written in Swift.

SFTintedIconPicker SFTintedIconPicker is a Shortcuts-like and highly customizable SFSymbol picker written in Swift. Features Native Appearance Search

StephenFang 2 Aug 16, 2022