实战教程:用 Go 实现验证码识别(基于 TFLite 模型)

项目简介
本项目使用 Go 编写服务端程序,通过 TensorFlow Lite 加载事先训练好的验证码识别模型,实现图像验证码的自动识别功能。

编程语言:Go

推理引擎:TensorFlow Lite

模型格式:.tflite

图片格式:RGB PNG

特点:轻量、跨平台、可部署于微服务/边缘节点

第一步:模型准备(训练 + 转换)
使用 Python 完成训练并转换为 TFLite,代码参考如下:

import tensorflow as tf
from tensorflow.keras import layers

输入图像尺寸:60x160x3

inputs = tf.keras.Input(shape=(60, 160, 3))
x = layers.Conv2D(32, 3, activation="relu")(inputs)
x = layers.MaxPooling2D()(x)
x = layers.Conv2D(64, 3, activation="relu")(x)
x = layers.Flatten()(x)
x = layers.Dense(128, activation="relu")(x)
outputs = [layers.Dense(36, activation="softmax")(x) for _ in range(4)]

model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")

模拟训练过程

import numpy as np
X = np.random.rand(1000, 60, 160, 3).astype(np.float32)
y = np.random.randint(0, 36, (1000, 4))
model.fit(X, [y[:, i] for i in range(4)], epochs=1)

导出 TFLite

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open("captcha_model.tflite", "wb") as f:
f.write(tflite_model)
第二步:Go 程序识别验证码
下面是完整的 Go 代码:

package main

import (
"fmt"
"image"
"image/png"
"os"
"image/draw"

"github.com/mattn/go-tflite"
"github.com/nfnt/resize"

)

func loadAndPreprocess(imgPath string) ([]float32, error) {
file, err := os.Open(imgPath)
if err != nil {
return nil, err
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
return nil, err
}

// 缩放到模型输入尺寸 160x60
resized := resize.Resize(160, 60, img, resize.Lanczos3)
rgba := image.NewRGBA(resized.Bounds())
draw.Draw(rgba, rgba.Bounds(), resized, image.Point{}, draw.Src)

data := make([]float32, 60*160*3)
idx := 0
for y := 0; y < 60; y++ {
	for x := 0; x < 160; x++ {
		r, g, b, _ := rgba.At(x, y).RGBA()
		data[idx] = float32(r) / 65535
		data[idx+1] = float32(g) / 65535
		data[idx+2] = float32(b) / 65535
		idx += 3
	}
}
return data, nil

}

func predict(imgPath string) {
model := tflite.NewModelFromFile("captcha_model.tflite")
options := tflite.NewInterpreterOptions()
interpreter := tflite.NewInterpreter(model, options)
defer interpreter.Delete()
interpreter.AllocateTensors()

input := interpreter.GetInputTensor(0)
data, _ := loadAndPreprocess(imgPath)
input.CopyFromBuffer(data)

interpreter.Invoke()

charset := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result := ""
for i := 0; i < 4; i++ {
	output := interpreter.GetOutputTensor(i)
	out := make([]float32, 36)
	output.CopyToBuffer(&out[0])
	maxIdx := 0
	for j := 1; j < 36; j++ {
		if out[j] > out[maxIdx] {
			maxIdx = j
		}
	}
	result += string(charset[maxIdx])
}
fmt.Println("识别结果:", result)

}

func main() {
predict("test_captcha.png")
}
第三步:运行测试
将一张名为 test_captcha.png 的验证码图像放在同级目录,执行:

go run main.go
输出示例:

识别结果: X9M7

posted @ 2025-05-31 23:15  ttocr、com  阅读(23)  评论(0)    收藏  举报