This library is no longer maintained and will see no further updates.
I created this project out of a need to perform OCR on labels and machine readable formats in a warehouse environment. It served that purpose well enough,
however I have not worked on that project in over 3 years and have not personally used this project since.
If you need OCR support in your application, I suggest you use the first party option by using the Text Recognition
capabilities of Apple's Vision framework. If your language is not supported by Apple, I suggest you fork this project and maintain it yourself. If you need assistance migrating to another solution
or in maintaining your own fork, you or your company can reach out to me to arrange a contract agreement.
When submitting to App Store Connect, libtesseract.framework will need to be removed from your app bundle before submission. This can be achieved through a post-build action in your application target's scheme by running the following:
Develop should be considered unstable and API breaking changes could happen at any time. If you need to utilize some changes contained in develop, adding the specific commit is highly recommended:
.package(
url: "https://github.com/SwiftyTesseract/SwiftyTesseract.git",
// This is just an example of a commit hash, do not just copy and paste this into your Package.swift
.revision("0e0c6aca147add5d5750ecb7810837ef4fd10fc2")
)
SwiftyTesseract 3.x.x Support
4.0.0 contains a lot of major breaking changes and there have been issues when migrating from Xcode 11 to 12 with versions 3.x.x. The support/3.x.x branch has been created to be able to address any issues for those who are unable or unwilling to migrate to the latest version. This branch is only to support blocking issues and will not see any new features.
Support for Cocoapods and Carthage Dropped
As the Swift Package Manager improves year over year, I have been decided to take advantage of binary Swift Packages that were announced during WWDC 2020 to eliminate having the dependency files being built ad-hoc and served out of the main source repo. This also has the benefit for being able to support other platforms via Swift Package Manager like Linux because the project itself is no longer dependent on Tesseract being vendored out of the source repository. While I understand this may cause some churn with existing projects that rely on SwiftyTesseract as a dependency, Apple platforms themselves have their own first-party OCR support through the Vision APIs.
SwiftyTesseract class renamed to Tesseract
The SwiftyTesseract class name felt a bit verbose and is more descriptive of the project than the class itself. To disambiguate between Google's Tesseract project and SwiftyTesseract's Tesseract class, all mentions of the class will be displayed as a code snippet: Tesseract.
Using SwiftyTesseract in Your Project
Import the module
importSwiftyTesseract
There are two ways to quickly instantiate SwiftyTesseract without altering the default values. With one language:
let tesseract =Tesseract(language: .english)
Or with multiple languages:
let tesseract =Tesseract(languages: [.english, .french, .italian])
Performing OCR
Platform Agnostic
Pass an instance of Data derived from an image to performOCR(on:)
let imageData =tryData(contentsOf: urlOfYourImage)
let result: Result<String, Tesseract.Error> = tesseract.performOCR(on: imageData)
Combine
Pass an instance of Data derived from an image to performOCRPublisher(on:)
let imageData =tryData(contentsOf: urlOfYourImage)
let result: AnyPublisher<String, Tesseract.Error> = tesseract.performOCRPublisher(on: imageData)
UIKit
Pass a UIImage to the performOCR(on:)orperformOCRPublisher(on:) methods:
let image =UIImage(named: "someImageWithText.jpg")!let result: Result<String, Error> = tesseract.performOCR(on: image)
let publisher: AnyPublisher<String, Error> = tesseract.performOCRPublisher(on: image)
AppKit
Pass a NSImage to the performOCR(on:)orperformOCRPublisher(on:) methods:
let image =NSImage(named: "someImageWithText.jpg")!let result: Result<String, Error> = tesseract.performOCR(on: image)
let publisher: AnyPublisher<String, Error> = tesseract.performOCRPublisher(on: image)
Conclusion
For people who want a synchronous call, the performOCR(on:) method provides a Result<String, Error> return value and blocks on the thread it is called on.
The performOCRPublisher(on:) publisher is available for ease of performing OCR in a background thread and receiving results on the main thread (only available on iOS 13.0+ and macOS 10.15+):
let cancellable = tesseract.performOCRPublisher(on: image)
.subscribe(on: backgroundQueue)
.receive(on: DispatchQueue.main)
.sink(
receiveCompletion: { completion in// do something with completion
},
receiveValue: { string in// do something with string
}
)
The publisher provided by performOCRPublisher(on:) is a cold publisher, meaning it does not perform any work until it is subscribed to.
Extensibility
The major downside to the pre-4.0.0 API was it's lack of extensibility. If a user needed to set a variable or perform an operation that existed in the Google Tesseract API but didn't exist on the SwiftyTesseract API, the only options were to fork the project or create a PR. This has been remedied by creating an extensible API for Tesseract variables and Tesseract functions.
Tesseract Variable Configuration
Starting in 4.0.0, all public instance variables of Tesseract have been removed in favor of a more extensible and declarative API:
let swiftyTesseract =SwiftyTesseract(language: .english)
swiftyTesseract.blackList="@#$%^&*"
swiftyTesseract.minimumCharacterHeight=35
swiftyTesseract.preserveInterwordSpaces=true
Tesseract.Variable
Tesseract.Variable is a new struct introduced in 4.0.0. It's definition is quite simple:
extensionTesseract {
publicstructVariable: RawRepresentable{
publicinit(rawValue: String) {
self.init(rawValue)
}
publicinit(_rawValue: String) {
self.rawValue= rawValue
}
publiclet rawValue: String
}
}
extensionTesseract.Variable: ExpressibleByStringLiteral{
publictypealiasStringLiteralType=Stringpublicinit(stringLiteralvalue: String) {
self.init(value)
}
}
// Extensions containing the previous API variables available as members of SwiftyTesseractpublicextensionTesseract.Variable {
staticlet allowlist: Tesseract.Variable ="tessedit_char_whitelist"staticlet disallowlist: Tesseract.Variable ="tessedit_char_blacklist"staticlet preserveInterwordSpaces: Tesseract.Variable ="preserve_interword_spaces"staticlet minimumCharacterHeight: Tesseract.Variable ="textord_min_xheight"staticlet oldCharacterHeight: Tesseract.Variable ="textord_old_xheight"
}
The problem here is that the library doesn't cover all the cases. What if you wanted to set Tesseract to only recognize numbers? You may be able to set the allowlist to only recognize numerals, but the Google Tesseract API already has a variable that does that: "classify_bln_numeric_mode".
Extending the library to make use of that variable could look something like this:
tesseract.configure {
set("classify_bln_numeric_mode", .true)
}
// Or extend Tesseract.Variable to get a clean trailing dot syntax:// Using ExpressibleByStringLiteral conformanceextensionTesseract.Variable {
staticlet numericMode: Tesseract.Variable ="classify_bln_numeric_mode"
}
// Using initializerextensionTesseract.Variable {
staticlet numericMode = Tesseract.Variable("classify_bln_numeric_mode")
}
tesseract.configure {
set(.numericMode, .true)
}
perform(action:)
Another issue that I've seen come up several times is "Can you implement X Tesseract feature" as a feature request. This has the same implications as the old property-based accessors for setting Tesseract variables. The perform(action:) method allows users full access to the Tesseract API in a thread-safe manner.
This comes with one major caveat: You will be completely responsible for managing memory when dealing with the Tessearct API directly. Using the Tesseract C API means that ARC will not help you. If you use this API directly, make sure your instrument your code and check for leaks. Swift's defer functionality pairs really well with managing memory when dealing directly with C APIs; check out Sources/SwiftyTesseract/Tesseract+OCR.swift for examples of using defer to release memory.
All of the library methods provided on Tesseract other than Tesseract.perform(action:) and Tesseract.configure(_:) are implemented as extensions using only Tesseract.perform(action:) to access the pointer created during initialization. To see this in action see the implementation of performOCR(on:) in Sources/SwiftyTesseract/Tesseract+OCR.swift
As an example, let's implement issue #66 using perform(action:):
If you don't care about all of the boilerplate needed to make your call site feel "Swifty", you could implement it simply like this:
importSwiftyTesseractimportlibtesseractextensionTesseract {
var pageSegMode: TessPageSegMode {
get {
perform { tessPointer inTessBaseAPIGetPageSegMode(tessPointer)
}
}
set {
perform { tessPointer inTessBaseAPISetPageSegMode(tessPointer, newValue)
}
}
}
}
// usage
tesseract.pageSegMode= PSM_SINGLE_COLUMN
ConfigurationBuilder
The declarative configuration syntax is achieved by accepting a function builder with functions that have a return value of (TessBaseAPI) -> Void. Using the previous example of extending the library to set the page segmentation mode of Tesseract, you could also create a function with a return signature of (TessBaseAPI) -> Void to utilize the declarative configuration block either during initialization or through Tesseract.configure(:_):
importSwiftyTesseractimportlibtesseractfuncsetPageSegMode(_pageSegMode: TessPageSegMode) -> (TessBaseAPI) ->Void {
return { tessPointer inTessBaseAPISetPageSegMode(tessPointer, pageSetMode)
}
}
let tesseract =Tesseract(language: .english) {
setPageSegMode(PSM_SINGLE_COLUMN)
}
// or post initialization
tesseract.configure {
setPageSegMode(PSM_SINGLE_COLUMN)
}
(The information for what to implement for this example was found in the Tesseract documentation)
Conclusion
The major feature of 4.0.0 is it's lack of features. The core of Tesseract is less than 130 lines of code, with the remainder of the code base implemented as extensions. I have attempted to be as un-opinionated as possible while providing an API that feels right at home in Swift. Users of the library are not limited to what I have time for or what other contributors to the project are able to contribute.
Now that this API is available, additions to the API surface of the library will be very selective. There should no longer be any restrictions to users of the library given the extensibility.
A Note on Initializer Defaults
The full signature of the primary Tesseract initializer is
The bundle parameter is required to locate the tessdata folder. This will need to be changed if Tesseract is not being implemented in your application bundle or if you are developing a Swift Package project (in this case you would need to specify Bundle.module, see Tests/SwiftyTesseractTests/SwiftyTesseractTests.swift for an example). The engine mode dictates the type of .traineddata files to put into your tessdata folder. .lstmOnly was chosen as a default due to the higher speed and reliability found during testing, but could potentially vary depending on the language being recognized as well as the image itself. See Which Language Training Data Should You Use? for more information on the different types of .traineddata files that can be used with SwiftyTesseract
libtesseract
Tesseract and it's dependencies are now built and distributed as an xcframework under the SwiftyTesseract/libtesseract repository for Apple platforms. Any issues regarding the build configurations for those should be raised under that repository.
Installation
Swift Package Manager is now the only supported dependency manager for bringing SwiftyTesseract into your project.
Apple Platforms
// Package.swift// swift-tools-version:5.3// The swift-tools-version declares the minimum version of Swift required to build this package.importPackageDescriptionlet package =Package(
name: "AwesomePackage",
platforms: [
// These are the minimum versions libtesseract supports
.macOS(.v10_13),
.iOS(.v11),
],
products: [
.library(
name: "AwesomePackage",
targets: ["AwesomePackage"]
),
],
dependencies: [
.package(url: "https://github.com/SwiftyTesseract/SwiftyTesseract.git", .upToNextMajor(from: "4.0.0"))
],
targets: [
.target(
name: "AwesomePackage",
dependencies: ["SwiftyTesseract"]
),
]
)
请发表评论