SwiftyTesseractRTE (SwiftyTesseract Real-Time Engine) is a real-time optical character recognition library built on top of SwiftyTesseract.
Swift Version | SwiftyTesseractRTE Version |
---|---|
4.0 | 1.x.x |
4.1 | 1.x.x |
4.2 | 2.x.x |
import SwiftyTesseract
import SwiftyTesseractRTE
Create an instance of RealTimeEngine and assign it's regionOfInterest property.
var realTimeEngine: RealTimeEngine!
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var regionOfInterest: UIView! // A subview of previewView
override func viewDidLoad() {
let swiftyTesseract = SwiftyTesseract(language: .english)
realTimeEngine = RealTimeEngine(swiftyTesseract: swiftyTesseract, desiredReliability: .verifiable) { recognizedString in
// Do something with the recognized string
}
}
override func viewDidLayoutSubviews() {
// Must occur during viewDidLayoutSubviews() - Autolayout constraints are not set in viewDidLoad()
realTimeEngine.bindPreviewLayer(to: previewView)
realTimeEngine.regionOfInterest = regionOfInterest.frame
// Only neccessary if providing a visual cue where the regionOfInterest is to your end user
previewView.addSublayer(regionOfInterest.layer)
}
SwiftyTesseractRTE allows for showing the camera preview without constantly attempting to recognize text. This allows you to let your end user to line up the text to be scanned before starting the scan. The recognitionIsActive
property is set to true
by default, so to prevent a scan from starting immediately, set recognitionIsActive
to false after instantiating RealTimeEngine
and before calling startPreview()
. Calling stopPreview()
also prevents recognition from being attempted because there are no inbound camera frames to be scanned.
// Starts optical character recognition
realTimeEngine.recognitionIsActive = true
// Stops optical character recognition
realTimeEngine.recognitionIsActive = false
// Starts camera preview
realTimeEngine.startPreview()
// Stops camera preview
realTimeEngine.stopPreview()
For camera permissions, you will need to add the Privacy - Camera Usage Description
permission to your Info.plist
file. If you are using the default SwiftyTesseractRTE AVManager
implementation, permission will be requested at the time the camera preview is being generated. If a custom implementation of AVManager
is provided, then it will be up to the developer to implement the permission request. For most use cases, the default AVManager
implementation is preferred and a custom implementation should only be provided if access to CMSampleBuffer
s are needed from the camera at the same time that SwiftyTesseractRTE is being run for OCR.
Starting in SwiftyTesseractRTE 2.0, the SwiftyTesseractRTEDelegate protocol has been replaced in favor of a closure property called onRecognitionComplete
with a type of ((String) -> ())?
. This can be assigned in a few ways:
RealTimeEngine(swiftyTesseract: swiftyTesseract, desiredReliability: .verifiable) { recognizedString in
// Do something with the recognized string
}
RealTimeEngine(swiftyTesseract: swiftyTesseract, desiredReliability: .verifiable, onRecognitionComplete: aFunction)
let realTimeEngine = RealTimeEngine(swiftyTesseract: swiftyTesseract, desiredReliability: .verifiable)
// Anonymous closure
realTimeEngine.onRecognitionComplete = { recognizedString in
// Do something with the recognized string
}
// Named closure
realTimeEngine.onRecognitionComplete = aFunction
SwiftyTesseractRTE is currently only able to utilized in portrait mode, but that does not mean your entire app also has to be portrait mode only. See the example project's AppDelegate (specifically the addition of a shouldRotate
boolean member variable and the implementation of application(_:supportedInterfaceOrientationsFor:)
) and ViewController files (specifically the viewWillAppear()
and viewWillDisappear()
methods) for an example on how to make a single view controller portrait mode only.
Tested with pod --version
: 1.3.1
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'SwiftyTesseractRTE', '~> 2.0'
end
Replace YOUR_TARGET_NAME
and then, in the Podfile
directory, type:
$ pod install
See SwiftyTesseract's Additional Configuration section on properly setting up SwiftyTesseract to be utilized in your project.
Official documentation for SwiftyTesseractRTE can be found here
Contributions are always welcome! Please refer to Contributing to SwiftyTesseractRTE for the full guidelines on creating issues and opening pull requests to the project.