fix: compile after gocv upgrade

This commit is contained in:
Cyrille Nofficial 2021-10-12 23:05:10 +02:00
parent ffb34da5de
commit 41083cb023
6 changed files with 94 additions and 88 deletions

View File

@ -126,10 +126,6 @@ image_final(){
#buildah rmi localhost/$IMAGE_NAME
#buildah manifest rm localhost/${IMAGE_NAME}
image_build linux/amd64
image_build linux/arm64
image_build linux/arm/v7
image_build
# push image

View File

@ -44,7 +44,7 @@ func NewRoadDetector() *RoadDetector {
}
}
func (rd *RoadDetector) DetectRoadContour(imgGray *gocv.Mat, horizonRow int) *[]image.Point {
func (rd *RoadDetector) DetectRoadContour(imgGray *gocv.Mat, horizonRow int) *gocv.PointVector {
kernel := gocv.NewMatWithSizeFromScalar(gocv.NewScalar(1, 1, 1, 1), rd.kernelSize, rd.kernelSize, gocv.MatTypeCV8U)
@ -77,34 +77,37 @@ func (rd *RoadDetector) DetectRoadContour(imgGray *gocv.Mat, horizonRow int) *[]
return rd.detectRoadContour(&img)
}
func (rd *RoadDetector) detectRoadContour(imgInversed *gocv.Mat) *[]image.Point {
func (rd *RoadDetector) detectRoadContour(imgInversed *gocv.Mat) *gocv.PointVector {
var (
epsilon float64
cntr []image.Point
cntr gocv.PointVector
)
cntrs := gocv.FindContours(*imgInversed, gocv.RetrievalExternal, gocv.ChainApproxSimple)
ptsVec := gocv.FindContours(*imgInversed, gocv.RetrievalExternal, gocv.ChainApproxSimple)
defer ptsVec.Close()
if len(cntrs) == 0 {
emptyContours := make([]image.Point, 0)
if ptsVec.Size() == 0 {
emptyContours := gocv.NewPointVector()
return &emptyContours
} else if len(cntrs) == 1 {
epsilon = rd.approxPolyEpsilonFactor * gocv.ArcLength(cntrs[0], true)
cntr = cntrs[0]
} else if ptsVec.Size() == 1 {
epsilon = rd.approxPolyEpsilonFactor * gocv.ArcLength(ptsVec.At(0), true)
cntr = ptsVec.At(0)
} else {
// Search biggest contour
peris := make([]float64, len(cntrs))
peris := make([]float64, ptsVec.Size())
maxArcIdx := 0
maxArcValue := 0.
for i, c := range cntrs {
//for i, c := range cntrs {
for i := 0; i< ptsVec.Size(); i++ {
c := ptsVec.At(i)
peri := gocv.ArcLength(c, true)
peris[i] = peri
if peri > maxArcValue {
maxArcValue = peri
maxArcIdx = i
}
cntr = cntrs[maxArcIdx]
cntr = ptsVec.At(maxArcIdx)
}
epsilon = rd.approxPolyEpsilonFactor * peris[maxArcIdx]
}
@ -114,8 +117,8 @@ func (rd *RoadDetector) detectRoadContour(imgInversed *gocv.Mat) *[]image.Point
var EllipseNotFound = events.Ellipse{Confidence: 0.}
func (rd *RoadDetector) ComputeEllipsis(road *[]image.Point) *events.Ellipse {
if len(*road) < 5 {
func (rd *RoadDetector) ComputeEllipsis(road *gocv.PointVector) *events.Ellipse {
if road.Size() < 5 {
return &EllipseNotFound
}

View File

@ -65,42 +65,51 @@ func TestRoadDetection_DetectRoadContour(t *testing.T) {
[]image.Point{image.Point{0, 45}, image.Point{0, 127}, image.Point{144, 127}, image.Point{95, 21}, image.Point{43, 21}},
},
{"image2", img2, 20,
[]image.Point{{159,69}, {128,53}, {125,41}, {113,42}, {108,21}, {87,21}, {79,41}, {72,30}, {44,39}, {29,34}, {0,67}, {0,127}, {159,127}, {152,101},},
[]image.Point{{159, 69}, {128, 53}, {125, 41}, {113, 42}, {108, 21}, {87, 21}, {79, 41}, {72, 30}, {44, 39}, {29, 34}, {0, 67}, {0, 127}, {159, 127}, {152, 101}},
},
{"image3", img3, 20,
[]image.Point{{97,21}, {59,127}, {159,127}, {159,36}, {138,21},},
[]image.Point{{97, 21}, {59, 127}, {159, 127}, {159, 36}, {138, 21}},
},
{"image4", img4, 20,
[]image.Point{{0,21}, {0,77}, {68,22}, {0,96}, {0,127}, {159,127}, {159,21},},
[]image.Point{{0, 21}, {0, 77}, {68, 22}, {0, 96}, {0, 127}, {159, 127}, {159, 21}},
},
{"image5", img5, 20,
[]image.Point{{159,32}, {100,36}, {29,60}, {0,79}, {0,127}, {159,127},},
[]image.Point{{159, 32}, {100, 36}, {29, 60}, {0, 79}, {0, 127}, {159, 127}},
},
}
for _, c := range cases {
imgGray := toGray(*c.img)
contours := rd.DetectRoadContour(imgGray, c.horizon)
imgGray.Close()
log.Infof("[%v] contour: %v", c.name, *contours)
if len(*contours) != len(c.expectedContour) {
t.Errorf("[%v] bad contour size: %v point(s), wants %v", c.name, len(*contours), len(c.expectedContour))
expected := gocv.NewPointVectorFromPoints(c.expectedContour)
if contours.Size() != expected.Size() {
t.Errorf("[%v] bad contour size: %v point(s), wants %v", c.name, contours.Size(), expected.Size())
}
for idx, pt := range c.expectedContour {
if pt != (*contours)[idx] {
t.Errorf("[%v] bad point: %v, wants %v", c.name, (*contours)[idx], pt)
for idx := 0; idx< expected.Size(); idx++ {
pt := expected.At(idx)
if pt != contours.At(idx) {
t.Errorf("[%v] bad point: %v, wants %v", c.name, contours.At(idx), pt)
}
}
debugContour(*c.img, contours, fmt.Sprintf("/tmp/%v.jpg", c.name))
expected.Close()
imgGray.Close()
contours.Close()
}
}
func debugContour(img gocv.Mat, contour *[]image.Point, imgPath string) {
func debugContour(img gocv.Mat, contour *gocv.PointVector, imgPath string) {
imgColor := img.Clone()
defer imgColor.Close()
gocv.DrawContours(&imgColor, [][]image.Point{*contour,}, 0, color.RGBA{
ptsVec := gocv.NewPointsVector()
defer ptsVec.Close()
ptsVec.Append(*contour)
gocv.DrawContours(&imgColor, ptsVec, 0, color.RGBA{
R: 0,
G: 255,
B: 0,
@ -131,7 +140,7 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) {
},
},
{"image2",
[]image.Point{{159,69}, {128,53}, {125,41}, {113,42}, {108,21}, {87,21}, {79,41}, {72,30}, {44,39}, {29,34}, {0,67}, {0,127}, {159,127}, {152,101},},
[]image.Point{{159, 69}, {128, 53}, {125, 41}, {113, 42}, {108, 21}, {87, 21}, {79, 41}, {72, 30}, {44, 39}, {29, 34}, {0, 67}, {0, 127}, {159, 127}, {152, 101}},
events.Ellipse{
Center: &events.Point{
X: 77,
@ -144,7 +153,7 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) {
},
},
{"image3",
[]image.Point{{97,21}, {59,127}, {159,127}, {159,36}, {138,21},},
[]image.Point{{97, 21}, {59, 127}, {159, 127}, {159, 36}, {138, 21}},
events.Ellipse{
Center: &events.Point{
X: 112,
@ -157,7 +166,7 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) {
},
},
{"image4",
[]image.Point{{0,21}, {0,77}, {68,22}, {0,96}, {0,127}, {159,127}, {159,21},},
[]image.Point{{0, 21}, {0, 77}, {68, 22}, {0, 96}, {0, 127}, {159, 127}, {159, 21}},
events.Ellipse{
Center: &events.Point{
X: 86,
@ -170,7 +179,7 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) {
},
},
{"image5",
[]image.Point{{159,32}, {100,36}, {29,60}, {0,79}, {0,127}, {159,127},},
[]image.Point{{159, 32}, {100, 36}, {29, 60}, {0, 79}, {0, 127}, {159, 127}},
events.Ellipse{
Center: &events.Point{
X: 109,
@ -185,7 +194,9 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) {
}
for _, c := range cases {
ellipse := rd.ComputeEllipsis(&c.contour)
ct := gocv.NewPointVectorFromPoints(c.contour)
ellipse := rd.ComputeEllipsis(&ct)
ct.Close()
if ellipse.String() != c.expectedEllipse.String() {
t.Errorf("ComputeEllipsis(%v): %v, wants %v", c.name, ellipse.String(), c.expectedEllipse.String())
}

View File

@ -23,7 +23,6 @@ func NewRoadPart(client mqtt.Client, horizon int, cameraTopic, roadTopic string)
return &RoadPart{
client: client,
frameChan: make(chan frameToProcess),
readyForNext: make(chan interface{}, 1),
cancel: make(chan interface{}),
roadDetector: NewRoadDetector(),
horizon: horizon,
@ -35,7 +34,6 @@ func NewRoadPart(client mqtt.Client, horizon int, cameraTopic, roadTopic string)
func (r *RoadPart) Start() error {
registerCallBacks(r)
ready := true
var frame = frameToProcess{}
defer func() {
if err := frame.Close(); err != nil {
@ -52,13 +50,8 @@ func (r *RoadPart) Start() error {
if err := oldFrame.Close(); err != nil {
log.Errorf("unable to close msg: %v", err)
}
if ready {
log.Debug("process msg")
go r.processFrame(&frame)
ready = false
}
case <-r.readyForNext:
ready = true
case <-r.cancel:
log.Infof("Stop service")
return nil
@ -73,15 +66,15 @@ var registerCallBacks = func(r *RoadPart) {
}
}
func (o *RoadPart) Stop() {
func (r *RoadPart) Stop() {
defer func() {
if err := o.roadDetector.Close(); err != nil {
if err := r.roadDetector.Close(); err != nil {
log.Errorf("unable to close roadDetector: %v", err)
}
}()
close(o.readyForNext)
close(o.cancel)
service.StopService("road", o.client, o.roadTopic)
close(r.readyForNext)
close(r.cancel)
service.StopService("road", r.client, r.roadTopic)
}
func (r *RoadPart) OnFrame(_ mqtt.Client, msg mqtt.Message) {
@ -120,10 +113,13 @@ func (r *RoadPart) processFrame(frame *frameToProcess) {
gocv.CvtColor(img, &imgGray, gocv.ColorRGBToGray)
road := r.roadDetector.DetectRoadContour(&imgGray, r.horizon)
defer road.Close()
ellipse := r.roadDetector.ComputeEllipsis(road)
cntr := make([]*events.Point, 0, len(*road))
for _, pt := range *road {
cntr := make([]*events.Point, 0, road.Size())
for i:=0;i< road.Size(); i++ {
pt := road.At(i)
cntr = append(cntr, &events.Point{X: int32(pt.X), Y: int32(pt.Y)})
}

View File

@ -35,7 +35,7 @@ func TestRoadPart_OnFrame(t *testing.T) {
cameraTopic := "topic/camera"
roadTopic := "topic/road"
rp := NewRoadPart(nil, 20, roadTopic)
rp := NewRoadPart(nil, 20, cameraTopic, roadTopic)
go func() {
if err := rp.Start(); err != nil {
t.Errorf("unable to start roadPart: %v", err)
@ -53,7 +53,7 @@ func TestRoadPart_OnFrame(t *testing.T) {
name: "image1",
msg: loadFrame(t, cameraTopic, "image"),
expectedCntr: []*events.Point{&events.Point{X: 0, Y: int32(45)}, &events.Point{X: 0, Y: 127}, &events.Point{X: 144, Y: 127}, &events.Point{X: 95, Y: 21}, &events.Point{X: 43, Y: 21}},
expectedEllipse: events.Ellipse{Center: &events.Point{X: 71, Y: 87,}, Width: 139, Height: 176, Angle: 92.66927, Confidence: 1.,},
expectedEllipse: events.Ellipse{Center: &events.Point{X: 71, Y: 87}, Width: 139, Height: 176, Angle: 92.66927, Confidence: 1.},
},
}