Files
AntiCaptcha/internal/handler/capabilities.go
T
Cmake 8e1d57df2a
Build and Push / build (push) Successful in 6m38s
feat(anim): 多帧(动图)验证码流水线 + 统一任务入口
- captcha/pipeline.go:decode_frames(GIF/APNG)→ detect_frames(复用 YOLOX)→ track(IoU 贪心)→ mark_visited
- AnimalJump:动物跳点类验证码,输入动图 + 候选点,返回每个点是否被经过 + 该点的首个帧号 + 未经过点列表
- POST /api/task/run(type 分发,taskTypes 注册表作为新类型扩展点)
- POST /api/frames/detect(逐帧检测,给自定义时序流水线复用)
- GET /api/task/types
- 类型展示 +2 条(动图点选 / 逐帧检测)
- 试用页支持动图任务:动图上传 + 候选点文本 + radius/class_filter
2026-12-04 01:38:31 +08:00

268 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"fmt"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
// ModelInfo 描述一个 ONNX 模型的构成,用于「类型展示」页。
// Params 是离线 introspection 出来的实测值(onnx initializer 张量元素数合计),
// SizeMB 在运行期按磁盘文件重新计算,保证不会跟实际部署对不上。
type ModelInfo struct {
Key string `json:"key"`
File string `json:"file"`
Arch string `json:"arch"`
ParamsM float64 `json:"params_m"`
SizeMB float64 `json:"size_mb"`
Input string `json:"input"`
Output string `json:"output"`
Quant string `json:"quant"`
Note string `json:"note"`
LoadedOk bool `json:"loaded_ok"`
}
// CapabilityDef 描述一个验证码接口:能干什么、怎么调、用哪个模型。
type CapabilityDef struct {
Key string `json:"key"`
Name string `json:"name"`
Group string `json:"group"`
Endpoint string `json:"endpoint"`
Desc string `json:"desc"`
Inputs []string `json:"inputs"`
Output string `json:"output"`
ModelKey string `json:"model_key"` // 空表示纯 OpenCV 算法,无模型
Cost int `json:"cost"`
Tips []string `json:"tips"`
Sample string `json:"sample"`
}
var modelCatalog = []ModelInfo{
{Key: "ocr", File: "OCR.onnx", Arch: "CRNN(CNN + BiLSTM + CTC)", ParamsM: 13.52,
Input: "input1[1,1,64,W] 灰度", Output: "387[1,seqlen,8210]", Quant: "fp32",
Note: "字符集 8210 类(7913 汉字 + 字母数字),中文验证码可认"},
{Key: "math", File: "CRNN_Math.onnx", Arch: "CRNN(同族轻量版)", ParamsM: 8.33,
Input: "input[B,3,70,200]", Output: "output[B,T,19]", Quant: "fp32",
Note: "19 类:0-9 与 + - × ÷ = ?"},
{Key: "rotate", File: "Rotation-RotNetR.onnx", Arch: "ResNet 系分类器(RotNet)", ParamsM: 24.22,
Input: "input[1,3,224,224]", Output: "output[1,360]", Quant: "fp32",
Note: "把角度当 360 分类问题;顺时针转返回角即回正,180° 附近会失效"},
{Key: "siamese", File: "Siamese-ResNet18.onnx", Arch: "Siamese 双塔(ResNet18 主干)", ParamsM: 11.37,
Input: "img1[B,3,H,W] + img2[B,3,H,W]", Output: "emb1[B,256] + emb2[B,256]", Quant: "fp32",
Note: "两个 256 维 embedding 算 L2 距离,相似度 = 1/(1+dist)"},
{Key: "detection_icon", File: "Detection_Icon.onnx", Arch: "YOLO11n 目标检测", ParamsM: 2.62,
Input: "images[1,3,640,640]", Output: "output0[1,5,8400]", Quant: "fp32",
Note: "Ultralytics YOLO11n 自训(data.yaml,单类 icon)"},
{Key: "detection_text", File: "Detection_Text.onnx", Arch: "YOLO11n 目标检测", ParamsM: 2.62,
Input: "images[1,3,640,640]", Output: "output0[1,5,8400]", Quant: "fp32",
Note: "Ultralytics YOLO11n 自训(单类 text)"},
{Key: "detection_object", File: "Detection_Object.onnx", Arch: "YOLOX-s 目标检测(Apache-2.0)", ParamsM: 8.96,
Input: "images[1,3,640,640]", Output: "output0[1,8400,85]", Quant: "fp32",
Note: "COCO 80 类;网格偏移解码 + exp(w/h),类别由 obj×cls 得出"},
{Key: "classify_object", File: "Classification_Object.onnx", Arch: "MobileNetV3-Large 分类(BSD-3)", ParamsM: 5.47,
Input: "images[1,3,224,224]", Output: "output0[1,1000]", Quant: "fp32",
Note: "ImageNet 1000 类;预处理为短边 232 → 中心裁 224 → ImageNet 归一化"},
}
var capabilityCatalog = []CapabilityDef{
{
Key: "ocr", Name: "文字识别", Group: "识别", Endpoint: "/api/ocr",
Desc: "识别图片中的文字,支持中文、英文、数字混排;返回纯文本字符串",
Inputs: []string{"img_base64:图片 base64(jpg/png)"},
Output: "{ \"result\": \"jepv\" }", ModelKey: "ocr", Cost: 1,
Tips: []string{"灰度单通道输入,宽度可变,长条图片直接喂", "大小写不做后处理,模型认什么就是什么"},
Sample: "4~6 位字母数字验证码、干扰线验证码",
},
{
Key: "math", Name: "算术式识别", Group: "识别", Endpoint: "/api/math",
Desc: "识别算术表达式并计算结果,支持加减乘除与比较符",
Inputs: []string{"img_base64:图片 base64"},
Output: "{ \"result\": \"46\" }", ModelKey: "math", Cost: 1,
Tips: []string{"输入固定 70x200 归一,超宽图会横向压扁", "表达式本身由 OCR 给出,结果由服务端计算"},
Sample: "12+34=? 这类算式验证码",
},
{
Key: "slider_match", Name: "缺口滑块", Group: "滑块", Endpoint: "/api/slider/match",
Desc: "定位滑块拼图的目标缺口位置,返回 x 坐标",
Inputs: []string{"target_base64:滑块小图", "background_base64:带缺口的背景图"},
Output: "{ \"result\": 214 }", ModelKey: "", Cost: 1,
Tips: []string{"纯 OpenCV 模板匹配 + 梯度,不依赖模型", "返回的是缺口在图中的 x 坐标(像素)"},
Sample: "极验/腾讯式拼图滑块",
},
{
Key: "slider_comparison", Name: "阴影差异滑块", Group: "滑块", Endpoint: "/api/slider/comparison",
Desc: "两图逐像素求差,找最左差异列,返回缺口左上角坐标",
Inputs: []string{"target_base64:缺口图", "background_base64:完整背景图(尺寸需一致)"},
Output: "{ \"result\": [144, 76] }", ModelKey: "", Cost: 1,
Tips: []string{"尺寸不一致直接 400,并给出中文原因", "阈值 80,逐列扫描 ≥5 差异像素判为缺口"},
Sample: "天爱/无感式阴影滑块",
},
{
Key: "similarity", Name: "图片相似度", Group: "比对", Endpoint: "/api/compare/similarity",
Desc: "判断两张图是否为同一目标,返回 0~1 相似度",
Inputs: []string{"image1_base64:图一", "image2_base64:图二"},
Output: "{ \"result\": 0.569 }", ModelKey: "siamese", Cost: 2,
Tips: []string{"自比 = 1.0;同图加模糊仍在 0.53 以上", "与纯白图比对落在 0.50 附近,可作为「无关」基线"},
Sample: "图标点选里判断两张小图是不是同一个图标",
},
{
Key: "single_rotate", Name: "单图旋转", Group: "旋转", Endpoint: "/api/rotate/single/rotate",
Desc: "判断图片被旋转了多少度,返回 0~359 的分类结果",
Inputs: []string{"img_base64:待测图片"},
Output: "{ \"result\": 229 }", ModelKey: "rotate", Cost: 1,
Tips: []string{"把图顺时针旋转返回值,就是回正姿势", "接近 180° 时模型会锁到 0 类,业务侧要兜底", "内部固定 224x224 + ImageNet 归一,缩放必须 BILINEAR"},
Sample: "圆形鱼眼/背景图旋转验证码",
},
{
Key: "double_rotate", Name: "双图环形旋转", Group: "旋转", Endpoint: "/api/rotate/double/rotate",
Desc: "内外两圈同心环,算出内圈需要逆时针转多少度与外圈吻合",
Inputs: []string{"inside_base64:内圈小图", "outside_base64:外圈环带图", "可选 check_pixel / speed_ratio / grayscale / anticlockwise"},
Output: "{ \"result\": {\"angle\":26,\"inner_angle\":25.5,\"raw_angle\":51,\"similarity\":0.8927} }",
ModelKey: "", Cost: 2,
Tips: []string{"raw_angle 是视觉对齐角(内圈逆时针转这么多度)", "inner_angle = raw ÷ (speed_ratio+1),只用于拖拽距离折算", "similarity < 0.6 基本可判图不对"},
Sample: "带内外双环的 3D 旋转验证码",
},
{
Key: "detect_icon", Name: "图标检测", Group: "点选", Endpoint: "/api/detection/icon",
Desc: "检测图中的图标并给出每个图标的位置与类别",
Inputs: []string{"img_base64:大图"},
Output: "{ \"detections\": [{bbox, confidence, class}] }", ModelKey: "detection_icon", Cost: 2,
Tips: []string{"YOLO11n,640x640 输入,单类 icon", "只认训练过的图标;纯几何图形(圆/方块)会返回空"},
Sample: "图标点选框选验证码",
},
{
Key: "detect_text", Name: "文字检测", Group: "点选", Endpoint: "/api/detection/text",
Desc: "检测图中的文字块位置,用于文字点选",
Inputs: []string{"img_base64:大图"},
Output: "{ \"detections\": [{bbox, confidence, class}] }", ModelKey: "detection_text", Cost: 2,
Tips: []string{"YOLO11n,单类 text", "和 OCR 配合:检测定位 + OCR 认字"},
Sample: "「请依次点击『风』『花』『雪』『月』」",
},
{
Key: "detect_object", Name: "目标类型检测", Group: "点选", Endpoint: "/api/detection/object",
Desc: "识别图中的日常物体,给出每个物体的类别、位置与置信度(COCO 80 类)",
Inputs: []string{"img_base64:图片 base64(jpg/png)"},
Output: "{ \"result\": { \"objects\": [{\"class\":\"person\",\"confidence\":0.91,\"box\":[x1,y1,x2,y2]}], \"count\": 3 } }",
ModelKey: "detection_object", Cost: 2,
Tips: []string{
"COCO 80 类,冷门物体识别不到属正常",
"小图(<400px)自动把置信度阈值从 0.30 放宽到 0.18",
"按类别分别做 NMS,不同类别的框不会互相压制",
"点选验证码里问「点击所有自行车/杯子」这类题,可以先过它再按类别筛框",
},
Sample: "「请点击图中所有的自行车」、商品图物体盘点",
},
{
Key: "detect_object_detail", Name: "检测 + 细分类", Group: "点选", Endpoint: "/api/detection/object/detail",
Desc: "先框出物体,再把每个框裁出来单独分类,给出比 COCO 大类更细的品类(两段式识别)",
Inputs: []string{"img_base64:图片 base64(jpg/png)"},
Output: "{ \"result\": { \"objects\": [{\"class\":\"bottle\",\"confidence\":0.83,\"fine_class\":\"water_bottle\",\"fine_score\":0.41,\"box\":[...]}] } }",
ModelKey: "detection_object", Cost: 3,
Tips: []string{
"两段式:YOLOX 负责定位(80 大类),MobileNetV3 负责细类",
"单次最多细分类 12 个框,避免耗时失控",
"框小于 12px 或贴边的会跳过细分类,只有大类",
"COCO 大类够用时用 /api/detection/object,更省钱也更快",
},
Sample: "「点击所有矿泉水瓶」这类细分点选题",
},
{
Key: "classify_object", Name: "物品分类", Group: "识别", Endpoint: "/api/classify/object",
Desc: "判断整张图(或裁剪出的单个图标)属于什么,返回 ImageNet 前 5 类及概率",
Inputs: []string{"img_base64:图片 base64,中心裁正方形后识别"},
Output: "{ \"result\": { \"top\": [{\"class\":\"banana\",\"score\":0.87}, ...] } }",
ModelKey: "classify_object", Cost: 1,
Tips: []string{
"主体不居中时先裁剪再送进来,准确率会明显提升",
"1000 类偏 ImageNet 语义(动物/乐器/工具/车辆),手绘图标可能落到近义类",
"只要位置不要类别时用目标检测,只要类别时用这个",
},
Sample: "图标点选的候选图标品类判断、单物体识别",
},
{
Key: "animal_jump", Name: "动图点选(未经过的点)", Group: "点选", Endpoint: "/api/task/run",
Desc: "拆帧 → 每帧检测 → 串轨迹 → 筛候选点,返回「没被经过的点」(type=animal_jump)",
Inputs: []string{"gif_base64:动图(GIF/APNG)或单图", "candidates:[{x,y}] 候选点坐标", "可选 radius(默认40) / class_filter(只统计某个类别的轨迹) / step(抽帧间隔)"},
Output: "{ \"result\": { \"candidates\": [{\"x\":80,\"y\":80,\"visited\":true,\"hits\":3}], \"click\": [{\"x\":80,\"y\":250}], \"unvisited_count\": 1, \"tracks\": [...] } }",
ModelKey: "detection_object", Cost: 3,
Tips: []string{
"轨迹是 IoU 贪心匹配串起来的,物体跳动间隔太大就配不上(可调 radius)",
"class_filter 填 COCO 类名(bird / sports ball / cat)可过滤掉背景误检",
"step 抽帧能大幅降耗时:30 帧动图用 step=2 只跑 15 次检测",
"候选点可以先自己用 /api/detection/object 找圆点,再喂进来",
},
Sample: "「点它没跳过的那些点」类动图验证码",
},
{
Key: "frames_detect", Name: "逐帧检测", Group: "点选", Endpoint: "/api/frames/detect",
Desc: "把动图拆成帧、每帧跑目标检测,返回每帧的框;给自定义时序流水线复用",
Inputs: []string{"gif_base64:动图或单图", "可选 step:抽帧间隔"},
Output: "{ \"result\": { \"frames\": 12, \"used_frames\": 6, \"per_frame\": [{\"frame\":0,\"objects\":[...]}] } }",
ModelKey: "detection_object", Cost: 2,
Tips: []string{
"视频(mp4/webm)本接口不处理,需要 ffmpeg 先抽帧再按多图送",
"想自己写后处理(比如算位移矢量、判是否越线)就用它取数据",
},
Sample: "滑块轨迹分析、动图行为识别",
},
{
Key: "click_icon_order", Name: "图标点选顺序", Group: "点选", Endpoint: "/api/detection/icon/order",
Desc: "按提示图标的顺序,给出大图中对应的点击坐标序列",
Inputs: []string{"order_img_base64:提示小图", "target_img_base64:大图"},
Output: "{ \"result\": [{x, y, score}] }", ModelKey: "siamese+detection_icon", Cost: 3,
Tips: []string{"先用 YOLO 框出候选,再用 Siamese 逐个比对排序", "顺序错一位就整题错,建议保留每步 score 便于排查"},
Sample: "依次点击下列图标",
},
{
Key: "click_text_order", Name: "文字点选顺序", Group: "点选", Endpoint: "/api/detection/text/order",
Desc: "按提示文字的顺序,给出大图中对应文字块的点击坐标序列",
Inputs: []string{"order_img_base64:提示文字图", "target_img_base64:大图"},
Output: "{ \"result\": [{x, y, text}] }", ModelKey: "siamese+detection_text+ocr", Cost: 3,
Tips: []string{"YOLO 框字 + OCR 认字 + 按提示串顺序匹配", "提示里有重复字时要按出现次序处理"},
Sample: "「依次点击『一』『二』『三』」",
},
}
// Capabilities 返回「类型展示」所需的全部静态信息(无需鉴权,首页可直接用)。
func (h *CaptchaHandler) Capabilities(c *gin.Context) {
models := make([]ModelInfo, len(modelCatalog))
copy(models, modelCatalog)
totalParams := 0.0
totalSize := 0.0
for i := range models {
models[i].SizeMB = 0
if fi, err := os.Stat(filepath.Join("./models", models[i].File)); err == nil {
mb := float64(fi.Size()) / 1e6
models[i].SizeMB = float64(int(mb*100)) / 100
models[i].LoadedOk = true
totalSize += mb
}
totalParams += models[i].ParamsM
}
groups := []string{}
seen := map[string]bool{}
for _, cap := range capabilityCatalog {
if !seen[cap.Group] {
seen[cap.Group] = true
groups = append(groups, cap.Group)
}
}
c.JSON(200, gin.H{
"capabilities": capabilityCatalog,
"models": models,
"summary": gin.H{
"capability_count": len(capabilityCatalog),
"model_count": len(models),
"total_params_m": fmt.Sprintf("%.2f", totalParams),
"total_size_mb": fmt.Sprintf("%.1f", totalSize),
"groups": groups,
"runtime": "CPU(OpenCV 4.6 + ONNXRuntime 1.18,无需 GPU)",
},
})
}