Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请求一个yoloV8的使用案例,我已经折腾了很久,但是不知道解决方案,麻烦参考我代码,请求指引 #1192

Open
cplasfwst opened this issue Aug 7, 2024 · 1 comment

Comments

@cplasfwst
Copy link

cplasfwst commented Aug 7, 2024

`package main

import (
"fmt"
"image"
"image/color"
"log"

"gocv.io/x/gocv"

)

var yolo_classes = []string{
"xiaohongshu",
}

func main() {

imgg := gocv.IMRead(`screenshot.png`, gocv.IMReadColor)
if imgg.Empty() {
	fmt.Println(`“无法加载图像文件”`)
	return
}
// 创建一个用于存储转换结果的 Mat 对象
//var converted gocv.Mat
// 调整图像尺寸到 640x640
resizedImg := gocv.NewMat()
gocv.Resize(imgg, &resizedImg, image.Point{X: 640, Y: 640}, 0, 0, gocv.InterpolationDefault)
//gocv.Resize(img, &resized, image.Point{X: 416, Y: 416}, 0, 0, gocv.InterpolationDefault)
//resizedImg.ConvertTo(&resizedImg, gocv.MatTypeCV32F, 1.0/255.0)
// 将图像数据转换为浮点数类型
//resizedImg.ConvertTo(&resizedImg, gocv.MatTypeCV32F)

// 归一化处理
gocv.ConvertScaleAbs(resizedImg, &resizedImg, 1.0/255.0, 0)
defer resizedImg.Close()

fmt.Println("resizedImg dimensions:", resizedImg.Rows(), resizedImg.Cols(), resizedImg.Channels())
// 检查图像信息
fmt.Println("resizedImg dimensions:", resizedImg.Rows(), resizedImg.Cols(), resizedImg.Channels())
// 加载 YOLOv8 模型
net := gocv.ReadNetFromONNX("best.onnx")
if net.Empty() {
	log.Fatal("Error loading YOLOv8 model")
}
defer net.Close()

//打印所有层的名称
//layerNames := net.GetLayerNames()
//for i, name := range layerNames {
//	fmt.Printf("模型的层名:Layer %d: %s\n", i, name)
//}

// 创建 blob 时,确保输入形状、均值、缩放因子等符合模型要求
blob := gocv.BlobFromImage(resizedImg, 1.0/255.0, image.Pt(640, 640), gocv.NewScalar(0, 0, 0, 0), true, false)
net.SetInput(blob, "images")
defer blob.Close()

// 打印 blob 的具体信息
fmt.Println("Blob dimensions:", blob.Size())
fmt.Println("Blob type:", blob.Type())

// 执行前向传播
detections := net.Forward("output0")
if detections.Empty() {
	log.Fatal("Detection output is empty")
}
defer detections.Close()

// 打印 detections 的形状
fmt.Println("Detections shape:", detections.Size())

fmt.Println("size:", detections.Size())
// 打印检测结果的形状以进行调试
fmt.Printf("Detection shape: %d x %d\n", detections.Rows(), detections.Cols())

// 打印 detections 的维度
shape := detections.Size()
fmt.Printf("Detection shape: %d x %d x %d\n", shape[0], shape[1], shape[2])

// 解析检测结果
confidenceThreshold := float32(0.5)
classesThreshold := float32(0.5)
// 按模型的输出格式调整解析
for i := 0; i < shape[2]; i++ { // 遍历每个检测框
	confidence := detections.GetFloatAt(0, 4+i*5) // 可能需要调整索引
	if confidence > confidenceThreshold {
		// 解析类别置信度
		var classID int
		classConfidence := float32(-1.0)
		for c := 0; c < len(yolo_classes); c++ {
			classConf := detections.GetFloatAt(0, 5+c+i*5) // 可能需要调整索引
			if classConf > classConfidence {
				classConfidence = classConf
				classID = c
			}
		}
		if classConfidence > classesThreshold {
			// 获取边界框坐标
			x := detections.GetFloatAt(0, i*5) * float32(resizedImg.Cols())
			y := detections.GetFloatAt(0, i*5+1) * float32(resizedImg.Rows())
			width := detections.GetFloatAt(0, i*5+2) * float32(resizedImg.Cols())
			height := detections.GetFloatAt(0, i*5+3) * float32(resizedImg.Rows())

			// 获取类别名称
			className := "unknown"
			if classID < len(yolo_classes) {
				className = yolo_classes[classID]
			}

			// 打印边界框坐标、置信度和类别
			fmt.Printf("Detected %s at (%.0f, %.0f, %.0f, %.0f) with confidence %.2f\n", className, x, y, width, height, confidence)

			// 在图像上绘制检测结果
			gocv.Rectangle(&imgg, image.Rect(int(x-width/2), int(y-height/2), int(x+width/2), int(y+height/2)), color.RGBA{255, 0, 0, 0}, 2)
			gocv.PutText(&imgg, fmt.Sprintf("%s: %.2f", className, confidence), image.Pt(int(x-width/2), int(y-height/2)-10), gocv.FontHersheyPlain, 1, color.RGBA{255, 0, 0, 0}, 2)
		}
	}
}

// 保存绘制后的图像
if ok := gocv.IMWrite("annotated_screenshot.png", imgg); !ok {
	log.Fatal("Failed to write annotated screenshot")
}

// 显示结果
window := gocv.NewWindow("YOLOv8 Detections")
window.IMShow(imgg)
window.WaitKey(0)
window.Close()

}
`

请求一个完整的yolo案例,有谁能帮帮我

@swdee
Copy link
Contributor

swdee commented Sep 30, 2024

An example/demo has been created here https://github.com/hybridgroup/gocv/blob/release/cmd/yolo-detection/main.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants