- 环境准备
1.1 安装 R
如果尚未安装 R,可以从 CRAN 官方网站 下载并安装。
安装完成后,使用以下命令检查是否成功安装:
r
R --version
1.2 安装 Tesseract OCR
macOS(Homebrew)
brew install tesseract
Linux(Ubuntu)
bash
sudo apt update
sudo apt install tesseract-ocr
Windows
从 Tesseract GitHub 下载并安装。
安装完成后,检查 Tesseract 是否可用:
bash
tesseract --version
1.3 安装 R 语言的 Tesseract 库
在 R 环境中运行:
r
install.packages("tesseract")
2. 代码实现
创建 captcha_reader.R 并写入以下代码:
r
library(tesseract)
library(magick)
读取验证码图片
image_path <- "captcha.png" # 替换为你的验证码图片路径
image <- image_read(image_path)
预处理:转换为灰度图,提高对比度
image <- image_convert(image, colorspace = "gray")
image <- image_modulate(image, brightness = 120, saturation = 0)
OCR 识别
ocr_engine <- tesseract("eng") # 设定为英文 OCR
text <- ocr(image, engine = ocr_engine)
输出识别结果
cat("识别出的验证码:", text, "\n")
3. 代码解析
3.1 读取图像
r
image <- image_read(image_path)
使用 magick 包读取验证码图像。
3.2 进行图像预处理
r
image <- image_convert(image, colorspace = "gray")
image <- image_modulate(image, brightness = 120, saturation = 0)
将图像转换为灰度模式,提高字符对比度。
3.3 进行 OCR 识别
r
ocr_engine <- tesseract("eng")
text <- ocr(image, engine = ocr_engine)
使用 tesseract 进行 OCR 解析,并指定 eng 语言。
3.4 输出识别结果
r
cat("识别出的验证码:", text, "\n")
4. 运行程序
确保 captcha.png 图片存在于相同目录下,然后在 R 环境中运行:
r
source("captcha_reader.R")
示例输出:
makefile
识别出的验证码: X7G9H
5. 提高 OCR 识别率
5.1 选择不同的 Tesseract PSM 模式
ocr_engine <- tesseract("eng", options = list(tessedit_pageseg_mode = 6))
PSM 6 适用于单行验证码,提高准确率。
5.2 限制识别字符集
ocr_engine <- tesseract("eng", options = list(tessedit_char_whitelist = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
让 Tesseract 只识别数字和大写字母,提高精确度。