solver

package
v0.0.0-...-45acbec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package solver 是本库的【纯本地推理层】——不触网,离线可测。

CaptchaSolver 把图像字节求解为点选坐标:图像解码校验 → wazero 进程内加载的 wasm 推理 (检测 + 特征)→ NMS/TopK 解析 → 匈牙利匹配 → 坐标。推理后端与 Solve 管线解耦(Solve 只 依赖一个 infer 函数)。内嵌 wasm 与可选的 AOT 编译缓存均经 go:embed 压缩携带、启动时解压/播种。

网络编排(拉图/提交/validate)属另一层 pkg/client,不在本包。

Package solver 提供验证码求解的核心 API。

Index

Constants

View Source
const DefaultConfThreshold float32 = 0.1

DefaultConfThreshold 是未经 WithConfThreshold 指定时的检测置信度阈值。

检测分数近乎二值:真答案格多在 0.8 以上,误检要到 0.005 以下才出现。取值落在这段空档里, 以召回笔画稀疏或贴边被截断的格;超量由 Rust 侧的答案格数量上限截断兜底。

Variables

View Source
var (
	// ErrImageDecode 表示输入图像无法解码(非 JPEG/PNG 或已损坏)。不可重试。
	ErrImageDecode = errors.New("gtlv/solver: image decode failed")
	// ErrNoPromptBox 表示未检出提示词框。
	ErrNoPromptBox = errors.New("gtlv/solver: no prompt box detected")
	// ErrNoAnswerBoxes 表示未检出任何答案框。
	ErrNoAnswerBoxes = errors.New("gtlv/solver: no answer boxes detected")
	// ErrAnswerCountOutOfRange 表示答案框数量不在 2-4 的合理区间(检测异常)。
	ErrAnswerCountOutOfRange = errors.New("gtlv/solver: answer box count out of range (want 2-4)")
	// ErrFeatureCountMismatch 表示特征数与检测框数不对齐(Rust 侧特征提取有静默跳过)。
	ErrFeatureCountMismatch = errors.New("gtlv/solver: feature/detection count mismatch")
	// ErrNoMatches 表示匹配阶段未产出任何匹配。
	ErrNoMatches = errors.New("gtlv/solver: no matches produced")
)

求解阶段的哨兵错误。均可用 errors.Is 判定;多数为「本张图内容/检测不理想」的语义, 调用方(如 pkg/client 的重试逻辑)据此判断换图重试是否可能有帮助。

Functions

This section is empty.

Types

type BoundingBox

type BoundingBox struct {
	XMin float64 `json:"x_min"`
	YMin float64 `json:"y_min"`
	XMax float64 `json:"x_max"`
	YMax float64 `json:"y_max"`
}

BoundingBox 表示一个边界框。

type CaptchaSolver

type CaptchaSolver struct {
	// contains filtered or unexported fields
}

CaptchaSolver 是验证码求解器。推理经 WASM(wazero 进程内加载 Rust wasm 模块,无 CGO)完成。

并发安全:Solve 可被多个 goroutine 同时调用——底层 wasm 推理经内部互斥串行化 (验证码低频,串行足矣)。Close 后不得再调用 Solve。

func NewCaptchaSolver

func NewCaptchaSolver(opts ...Option) (*CaptchaSolver, error)

NewCaptchaSolver 创建求解器实例:实例化 wasm 模块、一次性加载模型(热态常驻)。 模型已随 wasm 模块内嵌(由 gtlv-core include_bytes! 携带),无需任何外部文件。 换模型=换 wasm:改 gtlv-core/modeldata 后重编,或用 WithWasmPath 指定外部 .wasm。

func (*CaptchaSolver) Close

func (s *CaptchaSolver) Close() error

Close 释放 FFI 后端资源。

func (*CaptchaSolver) Solve

func (s *CaptchaSolver) Solve(ctx context.Context, imageData []byte) (*Result, error)

Solve 解决验证码。image: JPEG/PNG 图像字节。

type Detection

type Detection struct {
	Box        BoundingBox `json:"box"`
	Confidence float64     `json:"confidence"`
	ClassID    int         `json:"class_id"` // 0=答案目标, 1=提示词
}

Detection 表示一个检测框。

type Match

type Match struct {
	PromptIndex int     `json:"prompt_index"`
	AnswerIndex int     `json:"answer_index"`
	Confidence  float64 `json:"confidence"`
	X           float64 `json:"x"` // 像素坐标
	Y           float64 `json:"y"` // 像素坐标
}

Match 表示一个匹配结果。

type Option

type Option func(*options)

Option 是 CaptchaSolver 的配置选项。

func WithCacheDir

func WithCacheDir(dir string) Option

WithCacheDir 指定 wazero 编译缓存目录(可选;缺省用系统缓存目录)。 持久化缓存让二次启动跳过 wasm→机器码的编译(首启数秒,之后近乎免除)。

func WithConfThreshold

func WithConfThreshold(threshold float32) Option

WithConfThreshold 设置置信度阈值。

func WithPerf

func WithPerf(enable bool) Option

WithPerf 启用性能计时。

func WithVerbose

func WithVerbose(enable bool) Option

WithVerbose 启用详细输出(包含特征向量等调试信息)。

func WithWasmPath

func WithWasmPath(path string) Option

WithWasmPath 指定外部 .wasm 模块路径(可选;缺省用内嵌 go:embed 的模块)。

type PerfReport

type PerfReport struct {
	// 启动耗时(dlopen 库 + gt_init 加载模型)
	StartupMs int64 `json:"startup_ms"`
	// Go 侧耗时
	ImageDecodeMs int64 `json:"image_decode_ms"`
	MatchMs       int64 `json:"match_ms"`
	CryptoMs      int64 `json:"crypto_ms"`
	// Rust 侧耗时(随 FFI wire 缓冲传回)
	RustPreprocessMs int64 `json:"rust_preprocess_ms"`
	YoloInferMs      int64 `json:"yolo_infer_ms"`
	CropResizeMs     int64 `json:"crop_resize_ms"`
	SiameseInferMs   int64 `json:"siamese_infer_ms"`
	RustTotalMs      int64 `json:"rust_total_ms"`
	// 总耗时
	TotalMs int64 `json:"total_ms"`
}

PerfReport 表示性能耗时报告。

type Result

type Result struct {
	PromptBox      *BoundingBox `json:"prompt_box,omitempty"`
	Detections     []Detection  `json:"detections"`
	AnswerFeatures [][]float32  `json:"answer_features,omitempty"` // 答案框特征(verbose 模式)
	PromptFeatures [][]float32  `json:"prompt_features,omitempty"` // 提示词段特征(verbose 模式)
	Matches        []Match      `json:"matches"`
	Perf           *PerfReport  `json:"perf,omitempty"`
}

Result 表示验证码求解的结果。

Directories

Path Synopsis
Package classic 是滑块/轨迹类验证码的【本地】辅助:背景图还原、缺口识别、滑动轨迹生成/加密。
Package classic 是滑块/轨迹类验证码的【本地】辅助:背景图还原、缺口识别、滑动轨迹生成/加密。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL