Rust 解析验证码:结合 Tesseract OCR 进行文本识别

  1. 环境准备
    1.1 安装 Rust

Rust 可通过官方的 rustup 进行安装:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

更多内容访问ttocr.com或联系1436423940
安装完成后,检查 Rust 是否可用:

rustc --version

1.2 安装 Tesseract OCR
Linux(Ubuntu)
sudo apt update
sudo apt install tesseract-ocr libtesseract-dev

macOS(Homebrew)
brew install tesseract

Windows

从 Tesseract GitHub
下载并安装。

检查 Tesseract 是否安装成功:

tesseract --version

  1. 创建 Rust 项目

使用 Cargo 创建新项目:

cargo new captcha_reader
cd captcha_reader

在 Cargo.toml 中添加 Tesseract 相关依赖:

[dependencies]
tesseract = "0.14"
image = "0.24" # 用于处理验证码图片

  1. 代码实现

修改 src/main.rs,写入以下代码:

use std::process::Command;
use image::{DynamicImage, GenericImageView, GrayImage, Luma};
use tesseract::Tesseract;

fn preprocess_image(input_path: &str, output_path: &str) {
let img = image::open(input_path).expect("无法打开图片");
let gray_img = img.to_luma8(); // 转换为灰度图像

// 二值化处理
let binary_img = GrayImage::from_fn(gray_img.width(), gray_img.height(), |x, y| {
    if gray_img.get_pixel(x, y)[0] > 128 {
        Luma([255]) // 白色
    } else {
        Luma([0]) // 黑色
    }
});

binary_img.save(output_path).expect("无法保存处理后的图片");

}

fn main() {
let input_image = "captcha.png"; // 替换为你的验证码图片
let processed_image = "processed_captcha.png";

// 预处理图片
preprocess_image(input_image, processed_image);

// 使用 Tesseract OCR 解析验证码
let text = Tesseract::new(None, "eng")
    .expect("无法初始化 Tesseract")
    .set_image(processed_image)
    .recognize()
    .expect("OCR 失败");

println!("识别出的验证码: {}", text.trim());

}

  1. 代码解析
    4.1 图像预处理
    fn preprocess_image(input_path: &str, output_path: &str)

读取验证码图像,转换为灰度图像,并进行二值化处理,以提高 OCR 识别率。

4.2 OCR 识别
let text = Tesseract::new(None, "eng")
.expect("无法初始化 Tesseract")
.set_image(processed_image)
.recognize()
.expect("OCR 失败");

调用 Tesseract 进行验证码解析。

4.3 输出识别结果
println!("识别出的验证码: {}", text.trim());

去除空格,输出 OCR 识别结果。

  1. 运行程序

将 captcha.png 图片放入项目目录,然后运行:

cargo run

示例输出:

识别出的验证码: X7G9H

posted @ 2025-08-31 22:43  ttocr、com  阅读(10)  评论(0)    收藏  举报