A modern Swift wrapper for Tesseract OCR, providing a clean API for text recognition in iOS and macOS applications.
- ๐ Pure Swift API with async/await support
- ๐ฑ iOS 16+ and macOS 13+ support
- ๐ 100+ language support via Tesseract
- ๐ฆ Distributed as Swift Package Manager
- ๐ฏ Pre-built XCFrameworks for easy integration
- ๐พ Built-in language data downloader
- ๐ผ Direct CGImage support
- macOS: Apple Silicon (ARM64) Macs only
- iOS: Currently not supported (XCFrameworks need iOS slices)
- Architecture: ARM64 only (Intel x86_64 not supported)
Add TesseractSwift to your project:
dependencies: [
.package(url: "https://github.com/amebalabs/TesseractSwift.git", from: "1.0.0")
]
import TesseractSwift
// Initialize engine
let engine = TesseractEngine(dataPath: "/path/to/tessdata")
try engine.initialize(language: "eng")
// Recognize text from CGImage
let text = try engine.recognize(cgImage: image)
print("Recognized text: \(text)")
print("Confidence: \(engine.confidence())%")
let downloader = LanguageDownloader.shared
let language = TesseractLanguage(code: "fra", name: "French", script: "Latin", fileSize: nil)
// Download language data
try await downloader.downloadLanguage(language, to: tessdataURL) { progress in
print("Download progress: \(progress * 100)%")
}
// Get list of common languages
let languages = LanguageDownloader.commonLanguages
// Check downloaded languages
let downloaded = downloader.downloadedLanguages(in: tessdataURL)
// Set recognition mode based on your content
engine.setPageSegmentationMode(.auto) // Automatic detection
engine.setPageSegmentationMode(.singleLine) // Single line of text
engine.setPageSegmentationMode(.singleBlock) // Uniform block of text
TesseractSwift supports 100+ languages. Common languages include:
- English (eng)
- Spanish (spa)
- French (fra)
- German (deu)
- Italian (ita)
- Portuguese (por)
- Russian (rus)
- Chinese Simplified (chi_sim)
- Chinese Traditional (chi_tra)
- Japanese (jpn)
- Korean (kor)
- Arabic (ara)
- Hindi (hin)
This package was created to provide Tesseract OCR support for TRex, extending its language support beyond the built-in Vision framework.
import TesseractSwift
class TesseractOCREngine: OCREngine {
private let engine: TesseractEngine
init(dataPath: String) {
self.engine = TesseractEngine(dataPath: dataPath)
}
func recognizeText(in image: CGImage) async throws -> String {
if !engine.isInitialized {
try engine.initialize(language: "eng")
}
return try engine.recognize(cgImage: image)
}
}
- Clone the repository
- Ensure you have the pre-built XCFrameworks in
Binaries/
- Open in Xcode or build via command line:
swift build
swift test
- iOS 16.0+ / macOS 13.0+
- Swift 5.9+
- Xcode 15.0+
This package includes Tesseract OCR (Apache 2.0) and Leptonica (BSD-style license).