monitoring: add configuration logs

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

View File

@ -77,12 +77,30 @@ func main() {
}() }()
zap.ReplaceGlobals(lgr) zap.ReplaceGlobals(lgr)
zap.S().Infof("topic road : %v", roadTopic)
zap.S().Infof("topic camera : %v", cameraTopic)
zap.S().Infof("horizon : %v", horizon)
zap.S().Infof("image width : %v", imgWidth)
zap.S().Infof("image height : %v", imgHeight)
zap.S().Infof("white threshold low : %v", whiteThresholdLow)
zap.S().Infof("white threshold high : %v", whiteThresholdHigh)
zap.S().Infof("canny threshold low : %v", cannyThresholdLow)
zap.S().Infof("canny threshold high : %v", cannyThresholdHigh)
zap.S().Infof("hough lines rho : %v", houghLinesRho)
zap.S().Infof("hough lines theta : %v", houghLinesTheta)
zap.S().Infof("hough lines threshold : %v", houghLinesThreshold)
zap.S().Infof("hough lines min line length: %v", houghLinesMinLineLength)
zap.S().Infof("hough lines max line gap : %v", houghLinesMaxLineGap)
client, err := cli.Connect(mqttBroker, username, password, clientId) client, err := cli.Connect(mqttBroker, username, password, clientId)
if err != nil { if err != nil {
zap.S().Fatalf("unable to connect to mqtt bus: %v", err) zap.S().Fatalf("unable to connect to mqtt bus: %v", err)
} }
defer client.Disconnect(50) defer client.Disconnect(50)
monitor := &road.DefaultMonitor{}
go monitor.Start()
p := road.NewPart(client, p := road.NewPart(client,
cameraTopic, roadTopic, cameraTopic, roadTopic,
road.NewDetector( road.NewDetector(
@ -96,6 +114,7 @@ func main() {
road.WithRegionOfInterest(imgWidth, imgHeight, horizon), road.WithRegionOfInterest(imgWidth, imgHeight, horizon),
road.WithPointOnRoad(image.Point{X: imgWidth / 2, Y: imgHeight - 30}), road.WithPointOnRoad(image.Point{X: imgWidth / 2, Y: imgHeight - 30}),
road.WithHoughLines(houghLinesRho, float32(houghLinesTheta), houghLinesThreshold, houghLinesMinLineLength, houghLinesMaxLineGap), road.WithHoughLines(houghLinesRho, float32(houghLinesTheta), houghLinesThreshold, houghLinesMinLineLength, houghLinesMaxLineGap),
road.WithMonitor(monitor),
), ),
) )
defer p.Stop() defer p.Stop()

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 { func NewDetector(options ...DetectorOption) *Detector {
whiteThreshold := 20. whiteThreshold := 20.
@ -112,6 +118,8 @@ func NewDetector(options ...DetectorOption) *Detector {
roiMask: *roiMask, roiMask: *roiMask,
pointOnRoad: pointOnRoad, pointOnRoad: pointOnRoad,
monitor: &FakeMonitor{},
} }
for _, option := range options { for _, option := range options {
@ -162,6 +170,8 @@ type Detector struct {
roiMask gocv.Mat roiMask gocv.Mat
pointOnRoad image.Point pointOnRoad image.Point
monitor Monitor
} }
func (d *Detector) Close() { 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 // Run Hough on edge detected image
road, ellipsis := d.getRoadShapeWithHoughLines(maskedEdges) road, ellipsis := d.getRoadShapeWithHoughLines(maskedEdges)
d.monitor.Increment()
return road, ellipsis 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() {}