Add slide and rotate interactive captcha solvers

New solver subsystem with independent models:
- GapDetectorCNN (1x128x256 grayscale → sigmoid) for slide gap detection
- RotationRegressor (3x128x128 RGB → sin/cos via tanh) for rotation angle prediction
- SlideSolver with 3-tier strategy: template match → edge detect → CNN fallback
- RotateSolver with ONNX sin/cos → atan2 inference
- Generators, training scripts, CLI commands, and slide track utility

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hua
2026-03-11 18:07:06 +08:00
parent 90d6423551
commit 9b5f29083e
20 changed files with 1440 additions and 10 deletions

View File

@@ -33,7 +33,10 @@ captcha-breaker/
│ │ ├── 3d_text/
│ │ ├── 3d_rotate/
│ │ └── 3d_slider/
── classifier/ # 调度分类器训练数据 (混合各类型)
── classifier/ # 调度分类器训练数据 (混合各类型)
│ └── solver/ # Solver 训练数据
│ ├── slide/ # 滑块缺口检测训练数据
│ └── rotate/ # 旋转角度回归训练数据
├── generators/
│ ├── __init__.py
│ ├── base.py # 生成器基类
@@ -41,13 +44,17 @@ captcha-breaker/
│ ├── math_gen.py # 算式验证码生成器 (如 3+8=?)
│ ├── threed_gen.py # 3D立体文字验证码生成器
│ ├── threed_rotate_gen.py # 3D旋转验证码生成器
── threed_slider_gen.py # 3D滑块验证码生成器
── threed_slider_gen.py # 3D滑块验证码生成器
│ ├── slide_gen.py # 滑块缺口训练数据生成器
│ └── rotate_solver_gen.py # 旋转求解器训练数据生成器
├── models/
│ ├── __init__.py
│ ├── lite_crnn.py # 轻量 CRNN (用于普通字符和算式)
│ ├── classifier.py # 调度分类模型
│ ├── threed_cnn.py # 3D文字验证码专用模型 (更深的CNN)
── regression_cnn.py # 回归CNN (3D旋转+滑块, ~1MB)
── regression_cnn.py # 回归CNN (3D旋转+滑块, ~1MB)
│ ├── gap_detector.py # 滑块缺口检测CNN (~1MB)
│ └── rotation_regressor.py # 旋转角度回归 sin/cos (~2MB)
├── training/
│ ├── __init__.py
│ ├── train_classifier.py # 训练调度模型
@@ -56,6 +63,8 @@ captcha-breaker/
│ ├── train_3d_text.py # 训练3D文字识别
│ ├── train_3d_rotate.py # 训练3D旋转回归
│ ├── train_3d_slider.py # 训练3D滑块回归
│ ├── train_slide.py # 训练滑块缺口检测
│ ├── train_rotate_solver.py # 训练旋转角度回归
│ ├── train_utils.py # CTC 训练通用逻辑
│ ├── train_regression_utils.py # 回归训练通用逻辑
│ └── dataset.py # 通用 Dataset 类
@@ -64,20 +73,32 @@ captcha-breaker/
│ ├── pipeline.py # 核心推理流水线 (调度+识别)
│ ├── export_onnx.py # PyTorch → ONNX 导出脚本
│ └── math_eval.py # 算式计算模块
├── solvers/ # 交互式验证码求解器
│ ├── __init__.py
│ ├── base.py # 求解器基类
│ ├── slide_solver.py # 滑块求解 (OpenCV + CNN)
│ └── rotate_solver.py # 旋转求解 (ONNX sin/cos)
├── utils/
│ ├── __init__.py
│ └── slide_utils.py # 滑块轨迹生成工具
├── checkpoints/ # 训练产出的模型文件
│ ├── classifier.pth
│ ├── normal.pth
│ ├── math.pth
│ ├── threed_text.pth
│ ├── threed_rotate.pth
── threed_slider.pth
── threed_slider.pth
│ ├── gap_detector.pth
│ └── rotation_regressor.pth
├── onnx_models/ # 导出的 ONNX 模型
│ ├── classifier.onnx
│ ├── normal.onnx
│ ├── math.onnx
│ ├── threed_text.onnx
│ ├── threed_rotate.onnx
── threed_slider.onnx
── threed_slider.onnx
│ ├── gap_detector.onnx
│ └── rotation_regressor.onnx
├── server.py # FastAPI 推理服务 (可选)
├── cli.py # 命令行入口
└── tests/
@@ -462,3 +483,62 @@ uv run python cli.py serve --port 8080
6. 实现 cli.py 统一入口
7. 可选: server.py HTTP 服务
8. 编写 tests/
## 交互式 Solver 扩展
### 概述
在现有验证码识别架构之上,新增滑块 (slide) 和旋转 (rotate) 两种交互式验证码求解能力。与现有 3d_rotate/3d_slider 的区别:
- **3d_slider** (合成拼图回归) → **slide solver**: 真实滑块验证码OpenCV 优先 + CNN 兜底
- **3d_rotate** (合成圆盘 sigmoid 回归) → **rotate solver**: 真实旋转验证码sin/cos 编码 + 自然图
每个 solver 模型独立训练、独立导出 ONNX、独立替换互不依赖。
### 滑块求解器 (SlideSolver)
- 三种方法按优先级: 模板匹配 → 边缘检测 → CNN 兜底
- 模型: `GapDetectorCNN` (1x128x256 灰度 → sigmoid [0,1])
- OpenCV 延迟导入,未安装时退化到 CNN only
- 输出: `{"gap_x", "gap_x_percent", "confidence", "method"}`
### 旋转求解器 (RotateSolver)
- ONNX 推理 → (sin, cos) → atan2 → 角度
- 模型: `RotationRegressor` (3x128x128 RGB → tanh (sin θ, cos θ))
- 输出: `{"angle", "confidence"}`
### Solver CLI 用法
```bash
# 生成训练数据
uv run python cli.py generate-solver slide --num 30000
uv run python cli.py generate-solver rotate --num 50000
# 训练 (各模型独立)
uv run python cli.py train-solver slide
uv run python cli.py train-solver rotate
# 求解
uv run python cli.py solve slide --bg bg.png [--tpl tpl.png]
uv run python cli.py solve rotate --image img.png
# 导出 (已集成到 export --all)
uv run python cli.py export --model gap_detector
uv run python cli.py export --model rotation_regressor
```
### 滑块轨迹生成
`utils/slide_utils.py` 提供 `generate_slide_track(distance)`:
- 贝塞尔曲线 ease-out 加速减速
- y 轴 ±1~3px 随机抖动
- 时间间隔不均匀
- 末尾微小过冲回退
### Solver 目标指标
| 模型 | 准确率目标 | 推理延迟 | 模型体积 |
|------|-----------|---------|---------|
| 滑块 CNN (±5px) | > 85% | < 30ms | ~1MB |
| 旋转回归 (±5°) | > 85% | < 30ms | ~2MB |