自动通过腾讯点选验证码
自动通过腾讯点选验证码。golang + gocv + yolov5
go-yolov5-onnx.go
package main
import (
"fmt"
"image"
"image/color"
"gocv.io/x/gocv"
)
// 定义一些常量
const (
confidenceThreshold float32 = 0.5 // 置信度阈值
nmsThreshold float32 = 0.3 // NMS 阈值
// 模型训练的类别数
numClasses = 1
)
func main() {
// 1. 加载 ONNX 模型
onnxModelPath := "./best.onnx"
net := gocv.ReadNetFromONNX(onnxModelPath)
if net.Empty() {
fmt.Printf("Error reading network model from: %v\n", onnxModelPath)
return
}
defer net.Close()
// 2. 读取并预处理输入图片
imagePath := "./target_bg_image.png"
img := gocv.IMRead(imagePath, gocv.IMReadColor)
if img.Empty() {
fmt.Printf("Error reading image from: %s\n", imagePath)
return
}
defer img.Close()
blob := gocv.BlobFromImage(
img,
1.0/255.0,
image.Pt(672, 480),
gocv.NewScalar(0, 0, 0, 0),
true, // 交换RB通道
false,
)
defer blob.Close()
// 3. 将处理好的图片输入网络并执行推理
net.SetInput(blob, "")
layerNames := net.GetLayerNames()
outLayerIndices := net.GetUnconnectedOutLayers()
outputLayerName := layerNames[outLayerIndices[0]-1]
outputs := net.Forward(outputLayerName)
if outputs.Empty() {
fmt.Println("Error: Forward pass returned no outputs.")
return
}
defer outputs.Close()
// 4. 解析和后处理输出结果
fmt.Printf("Raw output matrix info: Rows=%d, Cols=%d, Total=%d\n", outputs.Rows(), outputs.Cols(), outputs.Total())
// ======================== 关键修正点 ========================
// 手动重塑矩阵,因为 gocv.Forward() 未能正确解析输出维度
numCols := 5 + numClasses
numRows := outputs.Total() / numCols
// 将一维的Mat重塑为 [numRows, numCols] 的二维Mat
reshaped := outputs.Reshape(1, numRows)
defer reshaped.Close() // reshaped是一个新的Mat头,需要独立关闭
fmt.Printf("Reshaped matrix: Rows=%d, Cols=%d\n", reshaped.Rows(), reshaped.Cols())
// ==========================================================
// 使用重塑后的矩阵进行后处理
boxes, confidences, classIDs := postProcess(reshaped, img.Cols(), img.Rows())
if len(boxes) == 0 {
fmt.Println("No objects detected after post-processing.")
return
}
indices := gocv.NMSBoxes(boxes, confidences, confidenceThreshold, nmsThreshold)
if len(indices) == 0 {
fmt.Println("No objects detected after NMS.")
return
}
fmt.Printf("Detected %d objects:\n", len(indices))
for _, i := range indices {
box := boxes[i]
classID := classIDs[i]
confidence := confidences[i]
fmt.Printf(" - Class ID: %d, Confidence: %.2f, BBox: %v\n", classID, confidence, box)
green := color.RGBA{0, 255, 0, 255}
gocv.Rectangle(&img, box, green, 2)
gocv.PutText(&img, fmt.Sprintf("Class %d", classID), image.Pt(box.Min.X, box.Min.Y-5), gocv.FontHersheySimplex, 0.5, green, 2)
}
gocv.IMWrite("result.jpg", img)
fmt.Println("Result image saved to result.jpg")
}
// postProcess 函数现在接收重塑后的矩阵,其内部逻辑无需修改
func postProcess(output gocv.Mat, originalImgWidth, originalImgHeight int) ([]image.Rectangle, []float32, []int) {
var boxes []image.Rectangle
var confidences []float32
var classIDs []int
rows := output.Rows()
cols := output.Cols()
xScale := float32(originalImgWidth) / 672.0
yScale := float32(originalImgHeight) / 480.0
for r := 0; r < rows; r++ {
confidence := output.GetFloatAt(r, 4)
if confidence < confidenceThreshold {
continue
}
var maxScore float32 = 0.0
var classID int = 0
// 从第5列开始是类别得分
for c := 5; c < cols; c++ {
score := output.GetFloatAt(r, c)
if score > maxScore {
maxScore = score
classID = c - 5
}
}
if maxScore*confidence > confidenceThreshold {
confidences = append(confidences, confidence)
classIDs = append(classIDs, classID)
cx := output.GetFloatAt(r, 0) * xScale
cy := output.GetFloatAt(r, 1) * yScale
w := output.GetFloatAt(r, 2) * xScale
h := output.GetFloatAt(r, 3) * yScale
x1 := int(cx - w/2)
y1 := int(cy - h/2)
x2 := int(cx + w/2)
y2 := int(cy + h/2)
boxes = append(boxes, image.Rect(x1, y1, x2, y2))
}
}
return boxes, confidences, classIDs
}
windows 环境中,配置 gocv 环境是个天坑
安装 scoop 后跟随教程 :
https://www.cnblogs.com/beidaxmf/p/18258953
- 此时你应当已经解决 mingw、cmake 环境问题。
- 接下来解决 gocv 环境问题
若 golang 版本高于 1.13 大概率是无法运行 win_build_opencv.cmd,参考解决办法:
https://www.cnblogs.com/liulog/p/18822802
若是 cmd 运行遇到问题
.... Compatibility with CMake < 3.5 has been removed from CMake. Update the VERSION argument value. ....
可看:
https://www.cnblogs.com/liulog/p/18822802
https://github.com/hybridgroup/gocv/issues/1320#issuecomment-2993421780
项目备份
源码留存备份:
训练数据:
路线图:
- 继续跟随 https://blog.2zxz.com/archives/icondetection 完成
Simaese孪生神经网络部分。
运行
yolotool 已开始删除对 yolov5 的支持
在 yolov5 中常用的命令:
激活 venv
.venv/scripts/activate
开始训练
python train.py --workers 0
将权重 *.pt 文件转为通用 onnx 模型
python export.py --weights runs/train/exp/weights/best.pt
自动通过腾讯点选验证码
https://blog.9991565.xyz/archives/zi-dong-tong-guo-teng-xun-dian-xuan-yan-zheng-ma