使用 Ruby 和 Tesseract 实现验证码识别工具
一、项目背景
验证码识别在自动化测试、爬虫、表单处理等场景中十分常见。虽然 Ruby 不是主流图像处理语言,但通过其丰富的 gem 生态,我们可以方便地调用 Tesseract 实现 OCR 功能。
二、环境准备
- 安装 Ruby
如果尚未安装 Ruby,可使用以下方式之一:
更多内容访问ttocr.com或联系1436423940
macOS: 自带或使用 brew install ruby
Ubuntu: sudo apt install ruby
Windows: 推荐使用 RubyInstaller:https://rubyinstaller.org/
验证版本:
ruby -v
gem -v
2. 安装 Tesseract OCR
macOS: brew install tesseract
Ubuntu: sudo apt install tesseract-ocr
Windows: 安装并配置环境变量(添加到 PATH)
- 安装 gem:rtesseract
gem install rtesseract
三、项目结构
目录结构如下:
captcha_ocr_ruby/
├── captcha.png # 验证码图片
└── main.rb # 主程序文件
四、编写识别脚本
创建 main.rb 文件,内容如下:
require 'rtesseract'
读取图像
image_path = 'captcha.png'
begin
image = RTesseract.new(image_path, lang: 'eng')
text = image.to_s.strip
puts "识别结果: #{text}"
rescue => e
puts "识别失败: #{e.message}"
end
运行:
ruby main.rb
示例输出:
识别结果: G4K9
五、添加图像预处理(可选)
图像预处理可以使用 mini_magick gem 调用 ImageMagick 进行处理:
gem install mini_magick
更新 main.rb:
require 'mini_magick'
require 'rtesseract'
def preprocess_image(input_path, output_path)
image = MiniMagick::Image.open(input_path)
image.colorspace 'Gray'
image.threshold '50%'
image.write output_path
end
input = 'captcha.png'
processed = 'processed.png'
preprocess_image(input, processed)
begin
image = RTesseract.new(processed, lang: 'eng')
puts "识别结果: #{image.to_s.strip}"
rescue => e
puts "识别失败: #{e.message}"
end
六、打包成 CLI 工具
使用 Ruby 的标准库 optparse 创建命令行参数支持:
require 'optparse'
require 'rtesseract'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby ocr.rb -i IMAGE_PATH"
opts.on("-iIMAGE", "--image=IMAGE", "图片路径") do |img|
options[:image] = img
end
end.parse!
if options[:image].nil?
puts "请使用 -i 参数指定验证码图片路径"
exit
end
begin
image = RTesseract.new(options[:image], lang: 'eng')
puts "识别结果: #{image.to_s.strip}"
rescue => e
puts "识别失败: #{e.message}"
end
运行示例:
ruby main.rb -i captcha.png
浙公网安备 33010602011771号