feat(api): 新增 /api/capabilities 类型展示接口(11 类能力 + 6 个模型的参数量/结构/I-O)
Build and Push / build (push) Successful in 5m26s
Build and Push / build (push) Successful in 5m26s
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
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)"},
|
||||
}
|
||||
|
||||
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: "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)",
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -28,6 +28,7 @@ func (h *CaptchaHandler) RegisterRoutes(r *gin.RouterGroup, authRequired bool) {
|
||||
captcha.POST("/register", h.Register)
|
||||
captcha.POST("/login", h.Login)
|
||||
captcha.GET("/tokens/verification", middleware.JWTAuth(), h.VerifyToken)
|
||||
captcha.GET("/capabilities", h.Capabilities) // 类型展示,公开
|
||||
|
||||
// 需要认证的接口
|
||||
auth := captcha.Group("")
|
||||
|
||||
Reference in New Issue
Block a user