2019-12-29 17:39:08 +00:00
|
|
|
package part
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-31 16:22:28 +00:00
|
|
|
"github.com/cyrilix/robocar-base/service"
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2019-12-29 17:39:08 +00:00
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2019-12-31 16:22:28 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-10-17 17:15:44 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-29 17:39:08 +00:00
|
|
|
"gocv.io/x/gocv"
|
2019-12-31 16:22:28 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2021-10-17 17:15:44 +00:00
|
|
|
"log"
|
2019-12-29 17:39:08 +00:00
|
|
|
)
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
func NewPart(client mqtt.Client, frameTopic, objectsTopic, roadTopic string, withObjects, withRoad bool) *FramePart {
|
2019-12-29 17:39:08 +00:00
|
|
|
return &FramePart{
|
2019-12-31 16:22:28 +00:00
|
|
|
client: client,
|
|
|
|
frameTopic: frameTopic,
|
|
|
|
objectsTopic: objectsTopic,
|
2020-01-04 14:01:10 +00:00
|
|
|
roadTopic: roadTopic,
|
2021-10-17 17:15:44 +00:00
|
|
|
window: gocv.NewWindow("frameTopic"),
|
2019-12-31 16:22:28 +00:00
|
|
|
withObjects: withObjects,
|
2020-01-04 14:01:10 +00:00
|
|
|
withRoad: withRoad,
|
2019-12-31 16:22:28 +00:00
|
|
|
imgChan: make(chan gocv.Mat),
|
2020-01-04 13:36:39 +00:00
|
|
|
objectsChan: make(chan events.ObjectsMessage),
|
2020-01-04 14:01:10 +00:00
|
|
|
roadChan: make(chan events.RoadMessage),
|
2019-12-31 16:22:28 +00:00
|
|
|
cancel: make(chan interface{}),
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type FramePart struct {
|
2020-01-19 16:15:36 +00:00
|
|
|
client mqtt.Client
|
2020-01-04 14:01:10 +00:00
|
|
|
frameTopic, objectsTopic, roadTopic string
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
window *gocv.Window
|
|
|
|
withObjects bool
|
2020-01-04 14:01:10 +00:00
|
|
|
withRoad bool
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
imgChan chan gocv.Mat
|
2020-01-04 13:36:39 +00:00
|
|
|
objectsChan chan events.ObjectsMessage
|
2020-01-04 14:01:10 +00:00
|
|
|
roadChan chan events.RoadMessage
|
2019-12-31 16:22:28 +00:00
|
|
|
cancel chan interface{}
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) Start() error {
|
|
|
|
if err := p.registerCallbacks(); err != nil {
|
|
|
|
return fmt.Errorf("unable to start service: %v", err)
|
|
|
|
}
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
var img = gocv.NewMat()
|
2020-01-04 13:36:39 +00:00
|
|
|
var objectsMsg events.ObjectsMessage
|
2020-01-04 14:01:10 +00:00
|
|
|
var roadMsg events.RoadMessage
|
|
|
|
|
2019-12-29 17:39:08 +00:00
|
|
|
for {
|
2019-12-31 16:22:28 +00:00
|
|
|
select {
|
|
|
|
case newImg := <-p.imgChan:
|
|
|
|
img.Close()
|
|
|
|
img = newImg
|
2020-01-04 13:36:39 +00:00
|
|
|
case objects := <-p.objectsChan:
|
|
|
|
objectsMsg = objects
|
2020-01-04 14:01:10 +00:00
|
|
|
case road := <-p.roadChan:
|
|
|
|
roadMsg = road
|
2019-12-31 16:22:28 +00:00
|
|
|
case <-p.cancel:
|
|
|
|
img.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-04 14:01:10 +00:00
|
|
|
p.drawFrame(&img, &objectsMsg, &roadMsg)
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) Stop() {
|
|
|
|
defer p.window.Close()
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
close(p.cancel)
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
StopService("frame-display", p.client, p.frameTopic, p.roadTopic)
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) onFrame(_ mqtt.Client, message mqtt.Message) {
|
2019-12-31 16:22:28 +00:00
|
|
|
var msg events.FrameMessage
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal protobuf FrameMessage: %v", err)
|
2019-12-31 16:22:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Infow("new frame", zap.String("topic", message.Topic()), zap.String("frameId", msg.GetId().GetId()))
|
|
|
|
|
2019-12-31 16:22:28 +00:00
|
|
|
img, err := gocv.IMDecode(msg.Frame, gocv.IMReadUnchanged)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Errorf("unable to decode image: %v", err)
|
2019-12-31 16:22:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.imgChan <- img
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) onObjects(_ mqtt.Client, message mqtt.Message) {
|
2020-01-04 14:01:10 +00:00
|
|
|
var msg events.ObjectsMessage
|
|
|
|
|
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal msg %T: %v", msg, err)
|
2020-01-04 14:01:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.objectsChan <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) onRoad(_ mqtt.Client, message mqtt.Message) {
|
|
|
|
var msg events.RoadMessage
|
2019-12-31 16:22:28 +00:00
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
err := proto.Unmarshal(message.Payload(), &msg)
|
2019-12-31 16:22:28 +00:00
|
|
|
if err != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Errorf("unable to unmarshal msg %T: %v", msg, err)
|
2019-12-31 16:22:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
p.roadChan <- msg
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) registerCallbacks() error {
|
|
|
|
err := RegisterCallback(p.client, p.frameTopic, p.onFrame)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:22:28 +00:00
|
|
|
if p.withObjects {
|
|
|
|
err := service.RegisterCallback(p.client, p.objectsTopic, p.onObjects)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
if p.withRoad {
|
|
|
|
err := service.RegisterCallback(p.client, p.roadTopic, p.onRoad)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 17:39:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
func (p *FramePart) drawFrame(img *gocv.Mat, objects *events.ObjectsMessage, road *events.RoadMessage) {
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
if p.withObjects {
|
|
|
|
p.drawObjects(img, objects)
|
|
|
|
}
|
2020-01-04 14:01:10 +00:00
|
|
|
if p.withRoad {
|
|
|
|
p.drawRoad(img, road)
|
|
|
|
}
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
p.window.IMShow(*img)
|
|
|
|
p.window.WaitKey(1)
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:36:39 +00:00
|
|
|
func (p *FramePart) drawObjects(img *gocv.Mat, objects *events.ObjectsMessage) {
|
|
|
|
for _, obj := range objects.GetObjects() {
|
|
|
|
gocv.Rectangle(
|
|
|
|
img,
|
|
|
|
image.Rect(int(obj.GetLeft()), int(obj.GetTop()), int(obj.GetRight()), int(obj.GetBottom())),
|
|
|
|
color.RGBA{0, 255, 0, 0},
|
|
|
|
2)
|
2019-12-31 16:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:01:10 +00:00
|
|
|
func (p *FramePart) drawRoad(img *gocv.Mat, road *events.RoadMessage) {
|
|
|
|
cntr := make([]image.Point, 0, len(road.GetContour()))
|
2020-01-19 16:15:36 +00:00
|
|
|
if road.GetContour() == nil || len(road.GetContour()) < 3 {
|
|
|
|
return
|
|
|
|
}
|
2020-01-04 14:01:10 +00:00
|
|
|
for _, pt := range road.GetContour() {
|
|
|
|
cntr = append(cntr, image.Point{X: int(pt.GetX()), Y: int(pt.GetY())})
|
|
|
|
}
|
|
|
|
|
|
|
|
gocv.DrawContours(
|
|
|
|
img,
|
2021-09-02 10:03:56 +00:00
|
|
|
gocv.NewPointsVectorFromPoints([][]image.Point{cntr}),
|
2020-01-04 14:01:10 +00:00
|
|
|
0,
|
2020-01-28 22:18:35 +00:00
|
|
|
color.RGBA{R: 255, G: 0, B: 0, A: 128},
|
2020-01-04 14:01:10 +00:00
|
|
|
-1)
|
2020-01-19 16:15:36 +00:00
|
|
|
|
2022-06-09 10:19:54 +00:00
|
|
|
p.drawRoadText(img, road)
|
|
|
|
|
|
|
|
}
|
|
|
|
func (p *FramePart) drawRoadText(img *gocv.Mat, road *events.RoadMessage) {
|
|
|
|
gocv.PutText(
|
|
|
|
img,
|
|
|
|
fmt.Sprintf("Confidence: %.3f", road.Ellipse.Confidence),
|
|
|
|
image.Point{X: 20, Y: 20},
|
|
|
|
gocv.FontHersheyPlain,
|
|
|
|
1.,
|
|
|
|
color.RGBA{R: 0, G: 255, B: 0, A: 255},
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
gocv.PutText(
|
|
|
|
img,
|
|
|
|
fmt.Sprintf("Angle ellipse: %.3f", road.Ellipse.Angle),
|
|
|
|
image.Point{X: 20, Y: 40},
|
|
|
|
gocv.FontHersheyPlain,
|
|
|
|
1.,
|
|
|
|
color.RGBA{R: 0, G: 255, B: 0, A: 255},
|
|
|
|
1,
|
|
|
|
)
|
2020-01-04 14:01:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 17:39:08 +00:00
|
|
|
func StopService(name string, client mqtt.Client, topics ...string) {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Infof("Stop %s service", name)
|
2019-12-29 17:39:08 +00:00
|
|
|
token := client.Unsubscribe(topics...)
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
2021-10-17 17:15:44 +00:00
|
|
|
zap.S().Errorf("unable to unsubscribe service: %v", token.Error())
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
client.Disconnect(50)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterCallback(client mqtt.Client, topic string, callback mqtt.MessageHandler) error {
|
|
|
|
log.Printf("Register callback on topic %v", topic)
|
|
|
|
token := client.Subscribe(topic, 0, callback)
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
|
|
|
return fmt.Errorf("unable to register callback on topic %s: %v", topic, token.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|