diff --git a/internal/captcha/handler.go b/internal/captcha/handler.go index 7fb3b89..ca02b93 100644 --- a/internal/captcha/handler.go +++ b/internal/captcha/handler.go @@ -743,23 +743,31 @@ func preprocessRotation(img image.Image) ([]float32, error) { bounds := img.Bounds() w, h := bounds.Dx(), bounds.Dy() + // 原版要求正方形图片,这里我们也先裁剪成正方形 + // 取较短边作为正方形边长 size := w if h < w { size = h } + // 中心裁剪成正方形 cropX := (w - size) / 2 cropY := (h - size) / 2 cropped := imaging.Crop(img, image.Rect(cropX, cropY, cropX+size, cropY+size)) + // 原版: new_size = int(h / sqrt(2)),去掉边角 sqrt2 := math.Sqrt(2.0) newSize := int(float64(size) / sqrt2) offset := (size - newSize) / 2 + + // 再次中心裁剪(去掉边角区域) centerCropped := imaging.Crop(cropped, image.Rect(offset, offset, offset+newSize, offset+newSize)) + // resize 到 224x224 resized := imaging.Resize(centerCropped, 224, 224, imaging.Lanczos) rgb := imaging.Clone(resized) + // ImageNet 标准化 mean := [3]float32{0.485, 0.456, 0.406} std := [3]float32{0.229, 0.224, 0.225}