使用 Swift 结合 Tesseract OCR 解析验证码

  1. 环境准备
    1.1 安装 Xcode 和 Swift

Swift 需要 Xcode 支持,可在 Mac App Store 下载 Xcode
。安装后,检查 Swift 版本:

swift --version
更多内容访问ttocr.com或联系1436423940
1.2 安装 Tesseract OCR

使用 Homebrew 安装 Tesseract:

brew install tesseract

验证安装:

tesseract --version

1.3 创建 Swift 项目

在终端运行:

swift package init --type executable
cd swift_ocr

然后,在 Package.swift 添加 SwiftyTesseract 依赖:

// 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"])
]
)

更新项目:

swift package update

  1. 代码实现:Swift 识别验证码

在 Sources/SwiftOCR/main.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 ?? 0
        let newColor = brightness > CGFloat(threshold) / 255.0 ? NSColor.white : NSColor.black
        bitmapRep.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 in
        switch recognizedText {
        case .success(let text):
            print("识别出的验证码: \(text)")
        case .failure(let error):
            print("OCR 识别失败: \(error)")
        }
    }
}

}

// 运行 OCR
let imagePath = "captcha.png"
recognizeCaptcha(imagePath: imagePath)

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

为了提高 OCR 识别率,我们对图像进行了以下优化:

灰度化:

let brightness = color?.brightnessComponent ?? 0

二值化:

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)")

  1. 运行程序

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

swift run

示例输出:

识别出的验证码: A8KZT

  1. 提高 OCR 识别准确率
    5.1 设置 Tesseract 参数

Tesseract 提供了不同的页面分割模式(PSM)。对于验证码,推荐使用 PSM 6:

tesseract.engineMode = .lstmOnly
tesseract.pageSegmentationMode = .singleLine

5.2 设定字符白名单

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

tesseract.customWords = ["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"]

5.3 进一步图像优化

可以尝试:

去噪点(滤波)

字符分割(去除粘连字符)

  1. 其他 OCR 方案

如果 Tesseract 不够精准,可以使用 Core ML OCR:

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, _ in
    if 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])

}

调用:

recognizeWithVision(imagePath: "captcha.png")

posted @ 2025-09-03 22:54  ttocr、com  阅读(6)  评论(0)    收藏  举报