forked from yalue/onnxruntime_go
/
main.go
310 lines (251 loc) · 9.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"bytes"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"math"
"os"
"sort"
"github.com/8ff/prettyTimer"
"github.com/nfnt/resize"
ort "github.com/yalue/onnxruntime_go"
)
// Embed the model file into the binary
var modelPath = "./yolov8n.onnx"
// Embed the libonnxruntime shared library into the binary
var libPath = "../../test_data/onnxruntime_arm64.dylib"
var imagePath = "./car.png"
var useCoreML = false
var blank []float32
type ModelSession struct {
Session *ort.AdvancedSession
Input *ort.Tensor[float32]
Output *ort.Tensor[float32]
}
func main() {
ts := prettyTimer.NewTimingStats() // Create a new timing stats object
if os.Getenv("USE_COREML") == "true" {
useCoreML = true
}
// Open the image file
file, err := os.Open(imagePath)
if err != nil {
panic(err)
}
defer file.Close()
// Read the entire file into memory
imageData, err := io.ReadAll(file)
if err != nil {
panic(err)
}
// Create a bytes.Buffer from the imageData
imageBuffer := bytes.NewBuffer(imageData)
// Prep blank tensor
reader := bytes.NewReader(imageBuffer.Bytes())
blank, _, _ = prepare_input(reader)
modelSes, err := initSession()
if err != nil {
panic(err)
}
// Run the detection 5 times
for i := 0; i < 5; i++ {
// Create a new reader from the buffer for each iteration
reader := bytes.NewReader(imageBuffer.Bytes())
input, img_width, img_height := prepare_input(reader)
ts.Start()
output, err := runInference(modelSes, input)
if err != nil {
panic(err)
}
ts.Finish()
// Print execution time
boxes := process_output(output, img_width, img_height)
// Print the results
for _, box := range boxes {
objectName := box[4].(string) // Accessing the object name
confidence := box[5].(float32) // Accessing the confidence
x1 := box[0].(float64) // Accessing the x1 coordinate
y1 := box[1].(float64) // Accessing the y1 coordinate
x2 := box[2].(float64) // Accessing the x2 coordinate
y2 := box[3].(float64) // Accessing the y2 coordinate
fmt.Printf("Object: %s Confidence: %.2f Coordinates: (%f, %f), (%f, %f)", objectName, confidence, x1, y1, x2, y2)
}
println()
}
ts.PrintStats()
}
func prepare_input(buffer io.Reader) ([]float32, int64, int64) {
// Decode the image from the buffer
imageObj, _, _ := image.Decode(buffer)
// Get the image size
imageSize := imageObj.Bounds().Size()
imageWidth, imageHeight := int64(imageSize.X), int64(imageSize.Y)
// Resize the image to 640x640 using Lanczos3 algorithm
imageObj = resize.Resize(640, 640, imageObj, resize.Lanczos3)
// Initialize slices to store red, green, blue channels
redChannel := []float32{}
greenChannel := []float32{}
blueChannel := []float32{}
// Iterate through pixels and populate the channel slices
for y := 0; y < 640; y++ {
for x := 0; x < 640; x++ {
r, g, b, _ := imageObj.At(x, y).RGBA()
redChannel = append(redChannel, float32(r/257)/255.0)
greenChannel = append(greenChannel, float32(g/257)/255.0)
blueChannel = append(blueChannel, float32(b/257)/255.0)
}
}
// Concatenate the channel slices to create the final input
inputArray := append(redChannel, greenChannel...)
inputArray = append(inputArray, blueChannel...)
return inputArray, imageWidth, imageHeight
}
func runInference(modelSes ModelSession, input []float32) ([]float32, error) {
inTensor := modelSes.Input.GetData()
copy(inTensor, input)
err := modelSes.Session.Run()
if err != nil {
return nil, err
}
return modelSes.Output.GetData(), nil
}
func initSession() (ModelSession, error) {
ort.SetSharedLibraryPath(libPath)
err := ort.InitializeEnvironment()
if err != nil {
return ModelSession{}, err
}
inputShape := ort.NewShape(1, 3, 640, 640)
inputTensor, err := ort.NewTensor(inputShape, blank)
if err != nil {
return ModelSession{}, err
}
outputShape := ort.NewShape(1, 84, 8400)
outputTensor, err := ort.NewEmptyTensor[float32](outputShape)
if err != nil {
return ModelSession{}, err
}
options, e := ort.NewSessionOptions()
if e != nil {
return ModelSession{}, err
}
if useCoreML { // If CoreML is enabled, append the CoreML execution provider
e = options.AppendExecutionProviderCoreML(0)
if e != nil {
options.Destroy()
return ModelSession{}, err
}
defer options.Destroy()
}
session, err := ort.NewAdvancedSession(modelPath,
[]string{"images"}, []string{"output0"},
[]ort.ArbitraryTensor{inputTensor}, []ort.ArbitraryTensor{outputTensor}, options)
modelSes := ModelSession{
Session: session,
Input: inputTensor,
Output: outputTensor,
}
return modelSes, err
}
func process_output(output []float32, imgWidth, imgHeight int64) [][]interface{} {
// Define a slice to hold the bounding boxes
boundingBoxes := [][]interface{}{}
// Iterate through the output array, considering 8400 indices
for idx := 0; idx < 8400; idx++ {
classID, probability := 0, float32(0.0)
// Iterate through 80 classes and find the class with the highest probability
for col := 0; col < 80; col++ {
currentProb := output[8400*(col+4)+idx]
if currentProb > probability {
probability = currentProb
classID = col
}
}
// If the probability is less than 0.5, continue to the next index
if probability < 0.5 {
continue
}
// Retrieve the label associated with the class ID
label := yolo_classes[classID]
// Extract the coordinates and dimensions of the bounding box
xc, yc, w, h := output[idx], output[8400+idx], output[2*8400+idx], output[3*8400+idx]
x1 := (xc - w/2) / 640 * float32(imgWidth)
y1 := (yc - h/2) / 640 * float32(imgHeight)
x2 := (xc + w/2) / 640 * float32(imgWidth)
y2 := (yc + h/2) / 640 * float32(imgHeight)
// Append the bounding box to the result
boundingBoxes = append(boundingBoxes, []interface{}{float64(x1), float64(y1), float64(x2), float64(y2), label, probability})
}
// Sort the bounding boxes by probability
sort.Slice(boundingBoxes, func(i, j int) bool {
return boundingBoxes[i][5].(float32) < boundingBoxes[j][5].(float32)
})
// Define a slice to hold the final result
result := [][]interface{}{}
// Iterate through sorted bounding boxes, removing overlaps
for len(boundingBoxes) > 0 {
result = append(result, boundingBoxes[0])
tmp := [][]interface{}{}
for _, box := range boundingBoxes {
if iou(boundingBoxes[0], box) < 0.7 {
tmp = append(tmp, box)
}
}
boundingBoxes = tmp
}
return result
}
func iou(box1, box2 []interface{}) float64 {
// Calculate the area of intersection between the two bounding boxes using the intersection function
intersectArea := intersection(box1, box2)
// Calculate the union of the two bounding boxes using the union function
unionArea := union(box1, box2)
// The Intersection over Union (IoU) is the ratio of the intersection area to the union area
return intersectArea / unionArea
}
func union(box1, box2 []interface{}) float64 {
// Extract coordinates of the first rectangle
rect1Left, rect1Bottom, rect1Right, rect1Top := box1[0].(float64), box1[1].(float64), box1[2].(float64), box1[3].(float64)
// Extract coordinates of the second rectangle
rect2Left, rect2Bottom, rect2Right, rect2Top := box2[0].(float64), box2[1].(float64), box2[2].(float64), box2[3].(float64)
// Calculate area of the first rectangle
rect1Area := (rect1Right - rect1Left) * (rect1Top - rect1Bottom)
// Calculate area of the second rectangle
rect2Area := (rect2Right - rect2Left) * (rect2Top - rect2Bottom)
// Use the intersection function to calculate the area of overlap between the two rectangles
intersectArea := intersection(box1, box2)
// The union of two rectangles is the sum of their areas minus the area of their overlap
return rect1Area + rect2Area - intersectArea
}
func intersection(box1, box2 []interface{}) float64 {
// Extracting the coordinates of the first box
firstBoxX1, firstBoxY1, firstBoxX2, firstBoxY2 := box1[0].(float64), box1[1].(float64), box1[2].(float64), box1[3].(float64)
// Extracting the coordinates of the second box
secondBoxX1, secondBoxY1, secondBoxX2, secondBoxY2 := box2[0].(float64), box2[1].(float64), box2[2].(float64), box2[3].(float64)
// Calculating the x coordinate of the left side of the intersection
intersectX1 := math.Max(firstBoxX1, secondBoxX1)
// Calculating the y coordinate of the bottom side of the intersection
intersectY1 := math.Max(firstBoxY1, secondBoxY1)
// Calculating the x coordinate of the right side of the intersection
intersectX2 := math.Min(firstBoxX2, secondBoxX2)
// Calculating the y coordinate of the top side of the intersection
intersectY2 := math.Min(firstBoxY2, secondBoxY2)
// Calculating and returning the area of the intersection
return (intersectX2 - intersectX1) * (intersectY2 - intersectY1)
}
// Array of YOLOv8 class labels
var yolo_classes = []string{
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
"sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie",
"suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon",
"bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut",
"cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse",
"remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book",
"clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush",
}