/
detectionQuality.go
355 lines (316 loc) · 9.34 KB
/
detectionQuality.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package stats
import (
"fmt"
"github.com/RoboCup-SSL/ssl-go-tools/internal/referee"
"github.com/RoboCup-SSL/ssl-go-tools/internal/vision"
"github.com/RoboCup-SSL/ssl-go-tools/pkg/persistence"
"log"
"os"
"sort"
"time"
)
const maxRobotDt = time.Millisecond * 1000
const maxRobotLoss = time.Millisecond * 200
const minRobotAge = time.Second * 10
const maxBallDt = time.Millisecond * 1000
const maxBallLoss = time.Millisecond * 100
const minBallAge = time.Second * 5
type DetectionQualityProcessor struct {
active bool
cameraData map[uint32]*Camera
robotDataLosses []*DataLoss
ballDataLosses []*DataLoss
robotsFile *os.File
ballFile *os.File
PrintDataLosses bool
FrameProcessor
}
type Camera struct {
id uint32
robots map[TeamColor]map[uint32]*Robot
ball *Ball
}
type TeamColor int
const (
TeamYellow TeamColor = 1
TeamBlue = 2
)
type Robot struct {
id uint32
lastFrameId uint32
firstDetectionTime time.Time
lastDetectionTime time.Time
}
type Ball struct {
lastFrameId uint32
firstDetectionTime time.Time
lastDetectionTime time.Time
}
type DataLoss struct {
Time time.Time
Duration time.Duration
NumFrames uint32
ObjectAge time.Duration
RobotId uint32
TeamColor TeamColor
}
func (p *DetectionQualityProcessor) Init(logFile string) error {
p.cameraData = map[uint32]*Camera{}
f, err := os.Create(logFile + "_robots.csv")
if err != nil {
return err
}
p.robotsFile = f
f, err = os.Create(logFile + "_ball.csv")
if err != nil {
return err
}
p.ballFile = f
return nil
}
func (p *DetectionQualityProcessor) Close() error {
for _, dataLoss := range p.robotDataLosses {
_, err := p.robotsFile.WriteString(fmt.Sprintf("%v,%v,%v,%v,%v,%v\n", dataLoss.Time.UnixNano(), dataLoss.TeamColor, dataLoss.RobotId, dataLoss.ObjectAge.Nanoseconds(), dataLoss.NumFrames, dataLoss.Duration.Nanoseconds()))
if err != nil {
log.Println("Could not write robot timing: ", err)
break
}
}
if p.robotsFile != nil {
if err := p.robotsFile.Close(); err != nil {
return err
}
}
for _, dataLoss := range p.ballDataLosses {
_, err := p.ballFile.WriteString(fmt.Sprintf("%v,%v,%v,%v\n", dataLoss.Time.UnixNano(), dataLoss.ObjectAge.Nanoseconds(), dataLoss.NumFrames, dataLoss.Duration.Nanoseconds()))
if err != nil {
log.Println("Could not write ball timing: ", err)
break
}
}
if p.ballFile != nil {
if err := p.ballFile.Close(); err != nil {
return err
}
}
return nil
}
func (p *DetectionQualityProcessor) ProcessDetection(_ *persistence.Message, frame *vision.SSL_DetectionFrame) {
if !p.active {
return
}
camera := p.cameraData[*frame.CameraId]
if camera == nil {
camera = new(Camera)
camera.id = *frame.CameraId
camera.robots = map[TeamColor]map[uint32]*Robot{}
camera.robots[TeamYellow] = map[uint32]*Robot{}
camera.robots[TeamBlue] = map[uint32]*Robot{}
p.cameraData[*frame.CameraId] = camera
}
if dataLoss := camera.processRobots(frame, frame.RobotsYellow, TeamYellow); dataLoss != nil {
p.robotDataLosses = append(p.robotDataLosses, dataLoss)
}
if dataLoss := camera.processRobots(frame, frame.RobotsBlue, TeamBlue); dataLoss != nil {
p.robotDataLosses = append(p.robotDataLosses, dataLoss)
}
if dataLoss := camera.processBalls(frame); dataLoss != nil {
p.ballDataLosses = append(p.ballDataLosses, dataLoss)
}
}
func (p *Camera) processRobots(frame *vision.SSL_DetectionFrame, robots []*vision.SSL_DetectionRobot, teamColor TeamColor) (dataLoss *DataLoss) {
dataLoss = nil
for _, detectionRobot := range robots {
robot := p.robots[teamColor][*detectionRobot.RobotId]
if robot == nil {
robot = new(Robot)
p.robots[teamColor][*detectionRobot.RobotId] = robot
robot.id = *detectionRobot.RobotId
}
tSent := toTime(*frame.TSent)
dt := tSent.Sub(robot.lastDetectionTime)
frameDiff := *frame.FrameNumber - robot.lastFrameId
if dt > maxRobotDt {
robot.firstDetectionTime = tSent
} else if frameDiff > 1 {
dataLoss = &DataLoss{
Duration: dt,
NumFrames: frameDiff,
Time: tSent,
ObjectAge: tSent.Sub(robot.firstDetectionTime),
RobotId: robot.id,
TeamColor: teamColor,
}
}
robot.lastDetectionTime = tSent
robot.lastFrameId = *frame.FrameNumber
}
return
}
func (p *Camera) processBalls(frame *vision.SSL_DetectionFrame) (dataLoss *DataLoss) {
dataLoss = nil
if len(frame.Balls) == 0 {
return
}
if p.ball == nil {
p.ball = new(Ball)
}
tSent := toTime(*frame.TSent)
dt := tSent.Sub(p.ball.lastDetectionTime)
frameDiff := *frame.FrameNumber - p.ball.lastFrameId
if dt > maxBallDt {
p.ball.firstDetectionTime = tSent
} else if frameDiff > 1 {
dataLoss = &DataLoss{
Duration: dt,
NumFrames: frameDiff,
Time: tSent,
ObjectAge: tSent.Sub(p.ball.firstDetectionTime),
}
}
p.ball.lastDetectionTime = tSent
p.ball.lastFrameId = *frame.FrameNumber
return
}
func toTime(t float64) time.Time {
sentSec := int64(t)
sentNs := int64((t - float64(sentSec)) * 1e9)
return time.Unix(sentSec, sentNs)
}
func (p *DetectionQualityProcessor) ProcessReferee(_ *persistence.Message, frame *referee.Referee) {
switch *frame.Stage {
case referee.Referee_NORMAL_FIRST_HALF,
referee.Referee_NORMAL_SECOND_HALF,
referee.Referee_EXTRA_FIRST_HALF,
referee.Referee_EXTRA_SECOND_HALF:
default:
p.active = false
return
}
switch *frame.Command {
case referee.Referee_HALT,
referee.Referee_TIMEOUT_BLUE,
referee.Referee_TIMEOUT_YELLOW:
p.active = false
default:
p.active = true
}
}
func (p *DetectionQualityProcessor) String() (res string) {
res += p.robotSkippedFrames()
res += p.ballSkippedFrames()
if p.PrintDataLosses {
res += p.robotDataLossOverThreshold()
res += p.ballDataLossOverThreshold()
} else {
res += p.robotDataLossOverThresholdSum()
res += p.ballDataLossOverThresholdSum()
}
return
}
func (p *DetectionQualityProcessor) robotDataLossOverThreshold() (res string) {
res += fmt.Sprintf("Data loss > %v\n", maxRobotLoss)
sort.Slice(p.robotDataLosses, func(i, j int) bool {
return p.robotDataLosses[i].Duration < p.robotDataLosses[j].Duration
})
for _, dataLoss := range p.robotDataLosses {
if dataLoss.Duration > maxRobotLoss {
res += fmt.Sprintf("%v | %2d %v: %4v frames, %13v (%14v old)\n", dataLoss.Time.UnixNano(), dataLoss.RobotId, teamColorStr(dataLoss.TeamColor), dataLoss.NumFrames, dataLoss.Duration, dataLoss.ObjectAge)
}
}
return
}
func (p *DetectionQualityProcessor) ballDataLossOverThreshold() (res string) {
res += fmt.Sprintf("Data loss > %v\n", maxBallLoss)
sort.Slice(p.ballDataLosses, func(i, j int) bool {
return p.ballDataLosses[i].Duration < p.ballDataLosses[j].Duration
})
for _, dataLoss := range p.ballDataLosses {
if dataLoss.Duration > maxBallLoss {
res += fmt.Sprintf("%v | ball: %4v frames, %13v (%14v old)\n", dataLoss.Time.UnixNano(), dataLoss.NumFrames, dataLoss.Duration, dataLoss.ObjectAge)
}
}
return
}
func (p *DetectionQualityProcessor) robotDataLossOverThresholdSum() (res string) {
numOverMax := 0
numOverMaxAndAged := 0
for _, dataLoss := range p.robotDataLosses {
if dataLoss.Duration > maxRobotLoss {
numOverMax++
if dataLoss.ObjectAge > minRobotAge {
numOverMaxAndAged++
}
}
}
res += fmt.Sprintf("Number of robot data losses over %v: %v, %v older than %v\n", maxRobotLoss, numOverMax, numOverMaxAndAged, minRobotAge)
return
}
func (p *DetectionQualityProcessor) ballDataLossOverThresholdSum() (res string) {
numOverMax := 0
numOverMaxAndAged := 0
for _, dataLoss := range p.ballDataLosses {
if dataLoss.Duration > maxBallLoss {
numOverMax++
if dataLoss.ObjectAge > minBallAge {
numOverMaxAndAged++
}
}
}
res += fmt.Sprintf("Number of ball data losses over %v: %v, %v older than %v\n", maxBallLoss, numOverMax, numOverMaxAndAged, minBallAge)
return
}
func teamColorStr(teamColor TeamColor) string {
if teamColor == TeamYellow {
return "Y"
} else if teamColor == TeamBlue {
return "B"
}
return ""
}
func (p *DetectionQualityProcessor) robotSkippedFrames() (res string) {
frameMisses := map[int]uint32{}
for _, dataLoss := range p.robotDataLosses {
frameMisses[int(dataLoss.NumFrames)]++
}
maxNumFrames := 6
maxNumFramesCount := uint32(0)
var numFramesList []int
for numFrames, numFramesCount := range frameMisses {
if numFrames >= maxNumFrames {
maxNumFramesCount += numFramesCount
} else {
numFramesList = append(numFramesList, numFrames)
}
}
sort.Ints(numFramesList)
res += fmt.Sprintf("Robots: skipped frames:\n")
for _, numFrames := range numFramesList {
res += fmt.Sprintf("%v frames: %vx\n", numFrames, frameMisses[numFrames])
}
res += fmt.Sprintf(">=%v frames: %vx\n", maxNumFrames, maxNumFramesCount)
return res
}
func (p *DetectionQualityProcessor) ballSkippedFrames() (res string) {
frameMisses := map[int]uint32{}
for _, dataLoss := range p.ballDataLosses {
frameMisses[int(dataLoss.NumFrames)]++
}
maxNumFrames := 6
maxNumFramesCount := uint32(0)
var numFramesList []int
for numFrames, numFramesCount := range frameMisses {
if numFrames >= maxNumFrames {
maxNumFramesCount += numFramesCount
} else {
numFramesList = append(numFramesList, numFrames)
}
}
sort.Ints(numFramesList)
res += fmt.Sprintf("Balls: skipped frames:\n")
for _, numFrames := range numFramesList {
res += fmt.Sprintf("%v frames: %vx\n", numFrames, frameMisses[numFrames])
}
res += fmt.Sprintf(">=%v frames: %vx\n", maxNumFrames, maxNumFramesCount)
return res
}