用 Swift 解析验证码(结合 Tesseract OCR)

  1. 环境准备
    1.1 安装 Swift

macOS 自带 Swift,如需更新,可使用:

xcode-select --install

更多内容访问ttocr.com或联系1436423940
然后检查 Swift 版本:

swift --version

Linux 用户可以从 Swift 官方网站
下载对应版本,并按照说明安装。

1.2 安装 Tesseract OCR
macOS(Homebrew 安装)
brew install tesseract

Linux(Ubuntu 示例)
sudo apt update
sudo apt install tesseract-ocr -y

Windows

Windows 用户可以从 Tesseract 官方 GitHub
下载并安装 Tesseract,并配置环境变量。

检查 Tesseract 是否安装成功:

tesseract --version

1.3 创建 Swift 项目
mkdir SwiftOCR
cd SwiftOCR
swift package init --type executable

然后,在 Package.swift 中添加 Tesseract 相关依赖:

// swift-tools-version:5.5
import PackageDescription

let package = Package(
name: "SwiftOCR",
dependencies: [
.package(url: "https://github.com/gali8/Tesseract-OCR-iOS.git", from: "4.0.0")
],
targets: [
.executableTarget(
name: "SwiftOCR",
dependencies: ["Tesseract-OCR-iOS"]
)
]
)

运行:

swift build

  1. 代码实现

编辑 Sources/SwiftOCR/main.swift,写入以下代码:

import Foundation
import TesseractOCR

// 图像预处理
func preprocessImage(inputPath: String, outputPath: String) {
guard let image = NSImage(contentsOfFile: inputPath) else {
print("无法打开图片")
return
}

// 转换为灰度图像
let grayscaleImage = CIImage(data: image.tiffRepresentation!)?.applyingFilter("CIPhotoEffectMono")

// 二值化处理
let context = CIContext()
if let outputImage = grayscaleImage, let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
    let processedImage = NSImage(cgImage: cgImage, size: image.size)
    
    // 保存处理后的图像
    let data = processedImage.tiffRepresentation
    try? data?.write(to: URL(fileURLWithPath: outputPath))
}

}

// 识别验证码
func recognizeCaptcha(imagePath: String) -> String {
if let tesseract = G8Tesseract(language: "eng") {
tesseract.engineMode = .tesseractOnly
tesseract.pageSegmentationMode = .singleLine
tesseract.image = NSImage(contentsOfFile: imagePath)
tesseract.recognize()
return tesseract.recognizedText ?? "识别失败"
}
return "初始化失败"
}

let inputImage = "captcha.png" // 你的验证码图片路径
let processedImage = "processed_captcha.png"

// 预处理验证码图像
preprocessImage(inputPath: inputImage, outputPath: processedImage)

// OCR 识别
let result = recognizeCaptcha(imagePath: processedImage)
print("识别出的验证码: (result)")

  1. 代码解析
    3.1 图像预处理

为了提高 OCR 识别率,我们对验证码进行了优化:

转换为灰度图像:

let grayscaleImage = CIImage(data: image.tiffRepresentation!)?.applyingFilter("CIPhotoEffectMono")

二值化处理(增强对比度):

if let outputImage = grayscaleImage, let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
let processedImage = NSImage(cgImage: cgImage, size: image.size)
}

保存处理后的图像:

let data = processedImage.tiffRepresentation
try? data?.write(to: URL(fileURLWithPath: outputPath))

3.2 OCR 解析

初始化 Tesseract OCR:

if let tesseract = G8Tesseract(language: "eng") {

设置 OCR 识别模式(PSM 6 适用于验证码):

tesseract.pageSegmentationMode = .singleLine

加载验证码图像并执行 OCR 识别:

tesseract.image = NSImage(contentsOfFile: imagePath)
tesseract.recognize()

获取识别结果:

tesseract.recognizedText ?? "识别失败"

  1. 运行程序

确保 captcha.png 存在于项目目录,然后运行:

swift run

示例输出:

识别出的验证码: X9GH5

  1. 提高 OCR 识别率
    5.1 选择合适的 PSM 模式

Tesseract 提供了不同的页面分割模式:

tesseract.pageSegmentationMode = .singleChar

PSM 6:假设是单行文本(默认推荐)

PSM 7:只检测单个文本行

PSM 10:单个字符模式(适用于单字符验证码)

5.2 只识别特定字符

如果验证码仅包含字母和数字:

tesseract.charWhitelist = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

5.3 进一步优化

降噪处理:可以使用 Core Image 的 CIMinimumComponent 或 CIColorControls 来提高对比度

字符分割:如果验证码字符粘连,可尝试 OpenCV-Swift 进行字符分割

使用深度学习:如果 Tesseract 不能满足需求,可以结合 Core ML 或 TensorFlow Lite 进行训练,提高验证码识别成功率

posted @ 2025-09-01 22:27  ttocr、com  阅读(8)  评论(0)    收藏  举报