monitoring: add configuration logs

This commit is contained in:
2024-05-02 20:51:42 +02:00
parent f82570e8ae
commit 9db7c9b5d3
3 changed files with 86 additions and 0 deletions

View File

@@ -83,6 +83,12 @@ func WithHoughLines(rho int, theta float32, threshold int, minLineLength int, ma
}
}
func WithMonitor(monitor Monitor) DetectorOption {
return func(d *Detector) {
d.monitor = monitor
}
}
func NewDetector(options ...DetectorOption) *Detector {
whiteThreshold := 20.
@@ -112,6 +118,8 @@ func NewDetector(options ...DetectorOption) *Detector {
roiMask: *roiMask,
pointOnRoad: pointOnRoad,
monitor: &FakeMonitor{},
}
for _, option := range options {
@@ -162,6 +170,8 @@ type Detector struct {
roiMask gocv.Mat
pointOnRoad image.Point
monitor Monitor
}
func (d *Detector) Close() {
@@ -248,6 +258,9 @@ func (d *Detector) Detect(img *gocv.Mat) ([]*events.Point, *events.Ellipse) {
// Run Hough on edge detected image
road, ellipsis := d.getRoadShapeWithHoughLines(maskedEdges)
d.monitor.Increment()
return road, ellipsis
}

54
road/monitor.go Normal file
View File

@@ -0,0 +1,54 @@
package road
import (
"go.uber.org/zap"
"sync"
"time"
)
type Monitor interface {
Increment()
Start()
}
type DefaultMonitor struct {
muCounter sync.Mutex
counter uint
}
func (d *DefaultMonitor) Increment() {
d.muCounter.Lock()
defer d.muCounter.Unlock()
d.counter = d.counter + 1
}
func (d *DefaultMonitor) Start() {
start := time.Now().UnixNano()
for {
select {
case <-time.After(10 * time.Second):
start = d.displayFPS(start)
}
}
}
func (d *DefaultMonitor) displayFPS(start int64) int64 {
d.muCounter.Lock()
defer d.muCounter.Unlock()
end := time.Now().UnixNano()
count := int64(d.counter)
go func() {
zap.S().Infof("Current FPS: %d img/s", count/((end-start)/1_000_000_000))
}()
d.counter = 0
return end
}
type FakeMonitor struct{}
func (f *FakeMonitor) Increment() {}
func (f *FakeMonitor) Start() {}