- 环境准备
1.1 安装 Xcode 和 Swift
Swift 需要 Xcode 支持,可在 Mac App Store 下载 Xcode。安装后,检查 Swift 版本:
swift --version
1.2 安装 Tesseract OCR
使用 Homebrew 安装 Tesseract:
brew install tesseract
验证安装:
bash
tesseract --version
1.3 创建 Swift 项目
在终端运行:
bash
swift package init --type executable
cd swift_ocr
然后,在 Package.swift 添加 SwiftyTesseract 依赖:
swift
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "SwiftOCR",
dependencies: [
.package(url: "https://github.com/SwiftyTesseract/SwiftyTesseract.git", from: "3.0.0")
],
targets: [
.target(name: "SwiftOCR", dependencies: ["SwiftyTesseract"])
]
)
更新项目:
bash
swift package update
2. 代码实现:Swift 识别验证码
在 Sources/SwiftOCR/main.swift 编写以下代码:
swift
import Foundation
import SwiftyTesseract
import AppKit
// 预处理图像(灰度 & 二值化)
func preprocessImage(imagePath: String) -> NSImage? {
guard let image = NSImage(contentsOfFile: imagePath) else {
print("无法加载图像")
return nil
}
let processedImage = applyThreshold(to: image, threshold: 128)
return processedImage
}
// 二值化处理
func applyThreshold(to image: NSImage, threshold: Int) -> NSImage {
let bitmapRep = NSBitmapImageRep(data: image.tiffRepresentation!)!
let width = bitmapRep.pixelsWide
let height = bitmapRep.pixelsHigh
for x in 0..<width {for y in 0..<height {let color = bitmapRep.colorAt(x: x, y: y)let brightness = color?.brightnessComponent ?? 0let newColor = brightness > CGFloat(threshold) / 255.0 ? NSColor.white : NSColor.blackbitmapRep.setColor(newColor, atX: x, y: y)}
}let newImage = NSImage(size: NSSize(width: width, height: height))
newImage.addRepresentation(bitmapRep)
return newImage
}
// OCR 识别验证码
func recognizeCaptcha(imagePath: String) {
let tesseract = SwiftyTesseract(language: .english)
if let processedImage = preprocessImage(imagePath: imagePath),let imageData = processedImage.tiffRepresentation {tesseract.performOCR(on: imageData) { recognizedText inswitch recognizedText {case .success(let text):print("识别出的验证码: \(text)")case .failure(let error):print("OCR 识别失败: \(error)")}}
}
}
// 运行 OCR
let imagePath = "captcha.png"
recognizeCaptcha(imagePath: imagePath)
3. 代码解析
3.1 图像预处理
为了提高 OCR 识别率,我们对图像进行了以下优化:
灰度化:
swift
let brightness = color?.brightnessComponent ?? 0
二值化:
swift
let newColor = brightness > CGFloat(threshold) / 255.0 ? NSColor.white : NSColor.black
bitmapRep.setColor(newColor, atX: x, y: y)
3.2 OCR 解析
使用 SwiftyTesseract 进行 OCR 识别:
let tesseract = SwiftyTesseract(language: .english)
tesseract.performOCR(on: imageData) { recognizedText in
case .success(let text): print("识别出的验证码: (text)")
4. 运行程序
确保 captcha.png 存在于项目目录下,然后运行:
bash
swift run
示例输出:
makefile
识别出的验证码: A8KZT
5. 提高 OCR 识别准确率
5.1 设置 Tesseract 参数
Tesseract 提供了不同的页面分割模式(PSM)。对于验证码,推荐使用 PSM 6:
swift
tesseract.engineMode = .lstmOnly
tesseract.pageSegmentationMode = .singleLine
5.2 设定字符白名单
如果验证码仅包含数字和大写字母:
swift
tesseract.customWords = ["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
5.3 进一步图像优化
可以尝试:
去噪点(滤波)
字符分割(去除粘连字符)
6. 其他 OCR 方案
如果 Tesseract 不够精准,可以使用 Core ML OCR:
swift
import Vision
func recognizeWithVision(imagePath: String) {
guard let image = NSImage(contentsOfFile: imagePath),
let imageData = image.tiffRepresentation,
let ciImage = CIImage(data: imageData) else { return }
let request = VNRecognizeTextRequest { request, _ inif let results = request.results as? [VNRecognizedTextObservation] {for observation in results {print("识别出的文本: \(observation.topCandidates(1).first?.string ?? "")")}}
}let handler = VNImageRequestHandler(ciImage: ciImage)
try? handler.perform([request])
}
调用:
swift
recognizeWithVision(imagePath: "captcha.png")