Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
0001c3e36e | |||
61d8bd22c4 | |||
77a2aa351b | |||
b7be75793b |
@ -18,12 +18,8 @@ func main() {
|
|||||||
var cameraTopic, steeringTopic string
|
var cameraTopic, steeringTopic string
|
||||||
var modelPath string
|
var modelPath string
|
||||||
var edgeVerbosity int
|
var edgeVerbosity int
|
||||||
|
var debug bool
|
||||||
|
|
||||||
lgr, err := zap.NewProductionConfig().Build()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to init logger: %v", err)
|
|
||||||
}
|
|
||||||
zap.ReplaceGlobals(lgr)
|
|
||||||
|
|
||||||
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
|
mqttQos := cli.InitIntFlag("MQTT_QOS", 0)
|
||||||
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
|
_, mqttRetain := os.LookupEnv("MQTT_RETAIN")
|
||||||
@ -34,6 +30,7 @@ func main() {
|
|||||||
flag.StringVar(&steeringTopic, "mqtt-topic-road", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic to publish road detection result, use MQTT_TOPIC_STEERING if args not set")
|
flag.StringVar(&steeringTopic, "mqtt-topic-road", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic to publish road detection result, use MQTT_TOPIC_STEERING if args not set")
|
||||||
flag.StringVar(&cameraTopic, "mqtt-topic-camera", os.Getenv("MQTT_TOPIC_CAMERA"), "Mqtt topic that contains camera frame values, use MQTT_TOPIC_CAMERA if args not set")
|
flag.StringVar(&cameraTopic, "mqtt-topic-camera", os.Getenv("MQTT_TOPIC_CAMERA"), "Mqtt topic that contains camera frame values, use MQTT_TOPIC_CAMERA if args not set")
|
||||||
flag.IntVar(&edgeVerbosity, "edge-verbosity", 0, "Edge TPU Verbosity")
|
flag.IntVar(&edgeVerbosity, "edge-verbosity", 0, "Edge TPU Verbosity")
|
||||||
|
flag.BoolVar(&debug, "debug", false, "Display debug logs")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if len(os.Args) <= 1 {
|
if len(os.Args) <= 1 {
|
||||||
@ -41,6 +38,23 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config := zap.NewDevelopmentConfig()
|
||||||
|
if debug {
|
||||||
|
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
|
||||||
|
} else {
|
||||||
|
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||||
|
}
|
||||||
|
lgr, err := config.Build()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to init logger: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := lgr.Sync(); err != nil {
|
||||||
|
log.Printf("unable to Sync logger: %v\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
zap.ReplaceGlobals(lgr)
|
||||||
|
|
||||||
if modelPath == "" {
|
if modelPath == "" {
|
||||||
zap.L().Error("model path is mandatory")
|
zap.L().Error("model path is mandatory")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
@ -53,7 +67,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer client.Disconnect(50)
|
defer client.Disconnect(50)
|
||||||
|
|
||||||
p := steering.NewPart(client, steeringTopic, cameraTopic, edgeVerbosity)
|
p := steering.NewPart(client, modelPath, steeringTopic, cameraTopic, edgeVerbosity)
|
||||||
defer p.Stop()
|
defer p.Stop()
|
||||||
|
|
||||||
cli.HandleExit(p)
|
cli.HandleExit(p)
|
||||||
|
@ -11,12 +11,14 @@ import (
|
|||||||
"github.com/mattn/go-tflite/delegates/edgetpu"
|
"github.com/mattn/go-tflite/delegates/edgetpu"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"image"
|
"image"
|
||||||
|
_ "image/jpeg"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPart(client mqtt.Client, steeringTopic, cameraTopic string, edgeVerbosity int) *Part {
|
func NewPart(client mqtt.Client, modelPath, steeringTopic, cameraTopic string, edgeVerbosity int) *Part {
|
||||||
return &Part{
|
return &Part{
|
||||||
client: client,
|
client: client,
|
||||||
|
modelPath: modelPath,
|
||||||
steeringTopic: steeringTopic,
|
steeringTopic: steeringTopic,
|
||||||
cameraTopic: cameraTopic,
|
cameraTopic: cameraTopic,
|
||||||
edgeVebosity: edgeVerbosity,
|
edgeVebosity: edgeVerbosity,
|
||||||
@ -39,6 +41,7 @@ type Part struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Part) Start() error {
|
func (p *Part) Start() error {
|
||||||
|
p.cancel = make(chan interface{})
|
||||||
p.model = tflite.NewModelFromFile(p.modelPath)
|
p.model = tflite.NewModelFromFile(p.modelPath)
|
||||||
if p.model == nil {
|
if p.model == nil {
|
||||||
return fmt.Errorf("cannot load model %v", p.modelPath)
|
return fmt.Errorf("cannot load model %v", p.modelPath)
|
||||||
@ -58,19 +61,27 @@ func (p *Part) Start() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot get EdgeTPU version: %w", err)
|
return fmt.Errorf("cannot get EdgeTPU version: %w", err)
|
||||||
}
|
}
|
||||||
zap.S().Infof("EdgeTPU Version: %s\n", edgetpuVersion)
|
zap.S().Infof("EdgeTPU Version: %s", edgetpuVersion)
|
||||||
edgetpu.Verbosity(p.edgeVebosity)
|
edgetpu.Verbosity(p.edgeVebosity)
|
||||||
|
|
||||||
p.options = tflite.NewInterpreterOptions()
|
p.options = tflite.NewInterpreterOptions()
|
||||||
//options.SetNumThread(4)
|
p.options.SetNumThread(4)
|
||||||
p.options.SetErrorReporter(func(msg string, userData interface{}) {
|
p.options.SetErrorReporter(func(msg string, userData interface{}) {
|
||||||
zap.L().Error(msg,
|
zap.S().Errorw(msg,
|
||||||
zap.Any("userData", userData),
|
"userData", userData,
|
||||||
)
|
)
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
|
zap.S().Infof("find %d edgetpu devices", len(devices))
|
||||||
|
zap.S().Infow("configure edgetpu",
|
||||||
|
"path", devices[0].Path,
|
||||||
|
"type", uint32(devices[0].Type),
|
||||||
|
)
|
||||||
// Add the first EdgeTPU device
|
// Add the first EdgeTPU device
|
||||||
d := edgetpu.New(devices[0])
|
d := edgetpu.New(devices[0])
|
||||||
|
if d == nil {
|
||||||
|
return fmt.Errorf("unable to create new EdgeTpu delegate")
|
||||||
|
}
|
||||||
p.options.AddDelegate(d)
|
p.options.AddDelegate(d)
|
||||||
|
|
||||||
p.interpreter = tflite.NewInterpreter(p.model, p.options)
|
p.interpreter = tflite.NewInterpreter(p.model, p.options)
|
||||||
@ -79,7 +90,7 @@ func (p *Part) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := registerCallbacks(p); err != nil {
|
if err := registerCallbacks(p); err != nil {
|
||||||
zap.S().Errorf("unable to register callbacks: %v", err)
|
zap.S().Errorw("unable to register callbacks", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,10 +124,22 @@ func (p *Part) onFrame(_ mqtt.Client, message mqtt.Message) {
|
|||||||
|
|
||||||
img, _, err := image.Decode(bytes.NewReader(msg.GetFrame()))
|
img, _, err := image.Decode(bytes.NewReader(msg.GetFrame()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Error("unable to decode frame", zap.Error(err))
|
zap.L().Error("unable to decode frame, skip frame", zap.Error(err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
steering, confidence, err := p.Value(img)
|
steering, confidence, err := p.Value(img)
|
||||||
|
if err != nil {
|
||||||
|
zap.S().Errorw("unable to compute sterring",
|
||||||
|
"frame", msg.GetId().GetId(),
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
zap.L().Debug("new steering value",
|
||||||
|
zap.Float32("steering", steering),
|
||||||
|
zap.Float32("confidence", confidence),
|
||||||
|
)
|
||||||
msgSteering := &events.SteeringMessage{
|
msgSteering := &events.SteeringMessage{
|
||||||
Steering: steering,
|
Steering: steering,
|
||||||
Confidence: confidence,
|
Confidence: confidence,
|
||||||
@ -201,7 +224,7 @@ func (p *Part) Value(img image.Image) (float32, float32, error) {
|
|||||||
var registerCallbacks = func(p *Part) error {
|
var registerCallbacks = func(p *Part) error {
|
||||||
err := service.RegisterCallback(p.client, p.cameraTopic, p.onFrame)
|
err := service.RegisterCallback(p.client, p.cameraTopic, p.onFrame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("unable to register callback: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user