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"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-12-29 17:39:08 +00:00
|
|
|
"gocv.io/x/gocv"
|
2019-12-31 16:22:28 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2019-12-29 17:39:08 +00:00
|
|
|
)
|
|
|
|
|
2019-12-31 16:22:28 +00:00
|
|
|
func NewPart(client mqtt.Client, frameTopic, objectsTopic string, withObjects 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,
|
|
|
|
window: gocv.NewWindow(frameTopic),
|
|
|
|
withObjects: withObjects,
|
|
|
|
imgChan: make(chan gocv.Mat),
|
2020-01-04 13:36:39 +00:00
|
|
|
objectsChan: make(chan events.ObjectsMessage),
|
2019-12-31 16:22:28 +00:00
|
|
|
cancel: make(chan interface{}),
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type FramePart struct {
|
2019-12-31 16:22:28 +00:00
|
|
|
client mqtt.Client
|
|
|
|
frameTopic, objectsTopic string
|
|
|
|
|
|
|
|
window *gocv.Window
|
|
|
|
withObjects bool
|
|
|
|
|
|
|
|
imgChan chan gocv.Mat
|
2020-01-04 13:36:39 +00:00
|
|
|
objectsChan chan events.ObjectsMessage
|
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
|
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
|
2019-12-31 16:22:28 +00:00
|
|
|
case <-p.cancel:
|
|
|
|
img.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-04 13:36:39 +00:00
|
|
|
p.drawFrame(&img, &objectsMsg)
|
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)
|
|
|
|
|
2019-12-29 17:39:08 +00:00
|
|
|
StopService("frame-display", p.client, p.frameTopic)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Errorf("unable to unmarshal protobuf FrameMessage: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := gocv.IMDecode(msg.Frame, gocv.IMReadUnchanged)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to decode image: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("[%v] frame %v", message.Topic(), msg.GetId())
|
|
|
|
p.imgChan <- img
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *FramePart) onObjects(_ mqtt.Client, message mqtt.Message) {
|
2020-01-04 13:36:39 +00:00
|
|
|
var objectsMsg events.ObjectsMessage
|
2019-12-31 16:22:28 +00:00
|
|
|
|
2020-01-04 13:36:39 +00:00
|
|
|
err := proto.Unmarshal(message.Payload(), &objectsMsg)
|
2019-12-31 16:22:28 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to unmarshal detected objects: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:36:39 +00:00
|
|
|
p.objectsChan <- objectsMsg
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:39:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:36:39 +00:00
|
|
|
func (p *FramePart) drawFrame(img *gocv.Mat, objects *events.ObjectsMessage) {
|
2019-12-31 16:22:28 +00:00
|
|
|
|
|
|
|
if p.withObjects {
|
|
|
|
p.drawObjects(img, objects)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:39:08 +00:00
|
|
|
func StopService(name string, client mqtt.Client, topics ...string) {
|
|
|
|
log.Printf("Stop %s service", name)
|
|
|
|
token := client.Unsubscribe(topics...)
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
|
|
|
log.Printf("unable to unsubscribe service: %v", token.Error())
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|