feat(v2): complete Go inference and UI spike

This commit is contained in:
ila
2026-07-22 16:01:28 +08:00
parent 104608751a
commit 22aadf2b26
16 changed files with 411 additions and 13 deletions
+63
View File
@@ -0,0 +1,63 @@
package spike
import (
"fmt"
ort "github.com/yalue/onnxruntime_go"
)
const (
poseInputSize = 640
poseInputCount = 1 * 3 * poseInputSize * poseInputSize
poseOutputCount = 1 * 56 * 8400
)
// RunPose executes the locked YOLO pose ONNX graph once using the CPU runtime.
// The caller supplies absolute model and DLL paths so no camera credential or
// machine-specific path is retained in V2 configuration or source.
func RunPose(modelPath, runtimeDLLPath string, input []float32) ([]float32, error) {
if len(input) != poseInputCount {
return nil, fmt.Errorf("pose input length = %d, want %d", len(input), poseInputCount)
}
ort.SetSharedLibraryPath(runtimeDLLPath)
if err := ort.InitializeEnvironment(); err != nil {
return nil, fmt.Errorf("initialize ONNX Runtime: %w", err)
}
defer func() { _ = ort.DestroyEnvironment() }()
inputTensor, err := ort.NewTensor(ort.NewShape(1, 3, poseInputSize, poseInputSize), input)
if err != nil {
return nil, fmt.Errorf("create pose input tensor: %w", err)
}
defer func() { _ = inputTensor.Destroy() }()
outputTensor, err := ort.NewEmptyTensor[float32](ort.NewShape(1, 56, 8400))
if err != nil {
return nil, fmt.Errorf("create pose output tensor: %w", err)
}
defer func() { _ = outputTensor.Destroy() }()
session, err := ort.NewAdvancedSession(
modelPath,
[]string{"images"},
[]string{"output0"},
[]ort.Value{inputTensor},
[]ort.Value{outputTensor},
nil,
)
if err != nil {
return nil, fmt.Errorf("open pose ONNX session: %w", err)
}
defer func() { _ = session.Destroy() }()
if err := session.Run(); err != nil {
return nil, fmt.Errorf("run pose ONNX session: %w", err)
}
output := outputTensor.GetData()
if len(output) != poseOutputCount {
return nil, fmt.Errorf("pose output length = %d, want %d", len(output), poseOutputCount)
}
return append([]float32(nil), output...), nil
}
+16
View File
@@ -0,0 +1,16 @@
package spike
import (
"strings"
"testing"
)
func TestRunPoseRejectsWrongInputLengthBeforeLoadingRuntime(t *testing.T) {
_, err := RunPose("missing.onnx", "missing.dll", []float32{0})
if err == nil {
t.Fatal("RunPose accepted an input that is not 1x3x640x640")
}
if !strings.Contains(err.Error(), "input length") {
t.Fatalf("RunPose error = %q, want input length error", err)
}
}
+43
View File
@@ -0,0 +1,43 @@
package spike
import "fmt"
// LetterboxBGRToNCHW converts one packed BGR frame into the RGB, CHW,
// float32 tensor expected by the locked 640-pixel YOLO pose ONNX model.
//
// It intentionally uses nearest-neighbour scaling for this Spike. T-303
// must replace or validate this preprocessing against the V1 implementation
// before it becomes the V2 production preprocessing path.
func LetterboxBGRToNCHW(frame []byte, width, height, target int) ([]float32, error) {
if width <= 0 || height <= 0 || target <= 0 {
return nil, fmt.Errorf("frame width, height and target must be positive")
}
if len(frame) != width*height*3 {
return nil, fmt.Errorf("BGR frame length = %d, want %d", len(frame), width*height*3)
}
scaleWidth := target
scaleHeight := height * target / width
if scaleHeight > target {
scaleHeight = target
scaleWidth = width * target / height
}
padX := (target - scaleWidth) / 2
padY := (target - scaleHeight) / 2
plane := target * target
input := make([]float32, 3*plane)
for y := 0; y < scaleHeight; y++ {
sourceY := y * height / scaleHeight
for x := 0; x < scaleWidth; x++ {
sourceX := x * width / scaleWidth
source := (sourceY*width + sourceX) * 3
destination := (padY+y)*target + padX + x
input[destination] = float32(frame[source+2]) / 255.0
input[plane+destination] = float32(frame[source+1]) / 255.0
input[2*plane+destination] = float32(frame[source]) / 255.0
}
}
return input, nil
}
+38
View File
@@ -0,0 +1,38 @@
package spike
import "testing"
func TestLetterboxBGRToNCHWPlacesPixelInScaledImage(t *testing.T) {
// One bright BGR pixel in a 2:1 frame should be resized into the centred
// 640x320 image area. The remaining top/bottom area must stay black.
frame := []byte{0, 0, 0, 255, 0, 0}
input, err := LetterboxBGRToNCHW(frame, 2, 1, 640)
if err != nil {
t.Fatalf("LetterboxBGRToNCHW returned an error: %v", err)
}
const plane = 640 * 640
if got := len(input); got != 3*plane {
t.Fatalf("input length = %d, want %d", got, 3*plane)
}
if got := input[2*plane+159*640+320]; got != 0 {
t.Fatalf("top padding blue channel = %v, want 0", got)
}
if got := input[0*plane+320*640+320]; got != 0 {
t.Fatalf("red channel = %v, want 0", got)
}
if got := input[1*plane+320*640+320]; got != 0 {
t.Fatalf("green channel = %v, want 0", got)
}
if got := input[2*plane+320*640+320]; got != 1 {
t.Fatalf("blue channel = %v, want 1", got)
}
}
func TestLetterboxBGRToNCHWRejectsIncorrectFrameSize(t *testing.T) {
_, err := LetterboxBGRToNCHW([]byte{0}, 2, 1, 640)
if err == nil {
t.Fatal("LetterboxBGRToNCHW accepted a truncated BGR frame")
}
}