From 0c2225bdbbaa4b9caaa68f03116ed6f6fa83dcf2 Mon Sep 17 00:00:00 2001 From: Admin Date: Fri, 17 Jul 2026 10:24:41 +0000 Subject: [PATCH] fix: handle dynamic dimensions in Math output shape --- internal/captcha/handler.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/captcha/handler.go b/internal/captcha/handler.go index 390c72e..7f49d7d 100644 --- a/internal/captcha/handler.go +++ b/internal/captcha/handler.go @@ -343,10 +343,31 @@ func preprocessMath(img image.Image) ([]float32, error) { func decodeMath(output []float32, outputShape []int64) string { // 输出格式: [num_chars, batch, timesteps] = [51, 1, 19] // 需要转置为 [batch, timesteps, num_chars] = [1, 19, 51] + if len(outputShape) < 3 || len(output) == 0 { + return "" + } + numChars := int(outputShape[0]) // 51 batch := int(outputShape[1]) // 1 timesteps := int(outputShape[2]) // 19 + // 处理动态维度(可能返回 -1) + if numChars <= 0 { + numChars = 51 // 默认字符数 + } + if batch <= 0 { + batch = 1 + } + if timesteps <= 0 { + // 从输出大小推断 + timesteps = len(output) / (numChars * batch) + } + + // 边界检查 + if numChars <= 0 || batch <= 0 || timesteps <= 0 { + return "" + } + // 转置: output[c*batch*t + b*t + t] -> transposed[b*timesteps*numChars + t*numChars + c] transposed := make([]float32, batch*timesteps*numChars) for b := 0; b < batch; b++ {