This is an open-source macOS-based Objective-C wrapper for the OCR library Tesseract.
You can also use this in Swift, instructions below.
Star this repo if you find it helpful; fork if you want to experiment with it. :)
The wrapper consists of just the following files
tessdata/(Language files for Tesseract)
include/(Headers for the dependencies)
For those of you who wish to first test out the OCR capabilities, the included
Screenshot-OCR is a demo application to showcase this.
First build the Xcode project included in this repository. This will generate an application through wish you can take a screenshot, as shown in the following gif.
In the Xcode log you will find the corresponding text Tesseract detected for this screenshot.
Getting this to work in your own project
Clone this project
Copy over the
tessdatafolders to your project.
Add these folders to your project in Xcode. Make sure
libare added as groups and
tessdatais added as a folder reference.
The location of this setting is shown in the following image:
Copy over the files
SLTesseract.hto your code directory.
Verify that the file
SLTesseract.mmis added to
Targets > Build Phases > Compile Sources. Additionally, verify that all the static libraries are also added to
Targets > Build Phases > Link Binary With Libraries. (This process should be done automatically)
You are now ready to use Tesseract in your macOS project. (See Example Usage for code syntax)
At the top of the file include the header file
SLTesseract *ocr = [[SLTesseract alloc] init];
will initiallize the class SLTesseract.
ocr.language = @"eng";
ocr.charWhitelist = @"abcdefghijklmnopqrstuvwxyz"
ocr.charBlacklist = @"1234567890"
Finally, assuming you already have the image that you wish to perform OCR on in NSImage form, you can call
NSString *text = [ocr recognize:image];
to recognize the image in question and get the corresponding text.
Usage in Swift
This library can be easily imported in a Swift project.
Just replicate all the steps above.
When adding .h and .mm files you will be prompted by Xcode to add a Bridging Header (if don't have one already).
Xcode will generate a file named
Add this line to the Bridging Header:
Initialize like this:
let ocr = SLTesseract()
ocr.language = "eng"
ocr.charWhitelist = "abcdefghijklmnopqrstuvwxyz"
ocr.charBlacklist = "1234567890"
Finally perform OCR by doing this:
let text = ocr.recognize(image)
The libraries below are all included in the
- Tesseract (v4.1.0)
- Leptonica (v1.75.3)
- LibPNG (v1.6.34)
- LibTIFF (v4.0.9)
- LibJPEG (v9c)
- LibZ (v1.2.11)
My project Tesseract macOS itself is distributed under the MIT license (see LICENSE);
Keep in mind that the main dependency Tesseract is distributed under the Apache 2.0 license.
Open an issue if you want something fixed.
You may reach me at
Tesseract-macOS@scott-liu.com to inquire about this project.