From 41083cb0233b1c7246f702e5047eaa1b2e333748 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Tue, 12 Oct 2021 23:05:10 +0200 Subject: [PATCH] fix: compile after gocv upgrade --- build-docker.sh | 4 -- cmd/rc-road/rc-road.go | 4 +- part/opencv.go | 31 ++++++------ part/opencv_test.go | 111 ++++++++++++++++++++++------------------- part/part.go | 28 +++++------ part/part_test.go | 4 +- 6 files changed, 94 insertions(+), 88 deletions(-) diff --git a/build-docker.sh b/build-docker.sh index 0f2d1c0..467fe58 100755 --- a/build-docker.sh +++ b/build-docker.sh @@ -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 diff --git a/cmd/rc-road/rc-road.go b/cmd/rc-road/rc-road.go index 790552d..1690e1d 100644 --- a/cmd/rc-road/rc-road.go +++ b/cmd/rc-road/rc-road.go @@ -9,8 +9,8 @@ import ( ) const ( - DefaultClientId = "robocar-road" - DefaultHorizon = 20 + DefaultClientId = "robocar-road" + DefaultHorizon = 20 ) func main() { diff --git a/part/opencv.go b/part/opencv.go index 912a563..e98c76c 100644 --- a/part/opencv.go +++ b/part/opencv.go @@ -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 } diff --git a/part/opencv_test.go b/part/opencv_test.go index d6d15d3..032db9a 100644 --- a/part/opencv_test.go +++ b/part/opencv_test.go @@ -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, @@ -114,79 +123,81 @@ func TestRoadDetector_ComputeEllipsis(t *testing.T) { cases := []struct { name string - contour []image.Point + contour []image.Point expectedEllipse events.Ellipse }{ {"image1", []image.Point{image.Point{0, 45}, image.Point{0, 127}, image.Point{144, 127}, image.Point{95, 21}, image.Point{43, 21}}, events.Ellipse{ Center: &events.Point{ - X: 71, - Y: 87, + X: 71, + Y: 87, }, - Width: 139, - Height: 176, - Angle: 92.66927, - Confidence: 1., + Width: 139, + Height: 176, + Angle: 92.66927, + Confidence: 1., }, }, {"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, - Y: 102, + X: 77, + Y: 102, }, - Width: 152, - Height: 168, - Angle: 94.70433, - Confidence: 1., + Width: 152, + Height: 168, + Angle: 94.70433, + Confidence: 1., }, }, {"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, - Y: 86, + X: 112, + Y: 86, }, - Width: 122, - Height: 140, - Angle: 20.761106, - Confidence: 1., + Width: 122, + Height: 140, + Angle: 20.761106, + Confidence: 1., }, }, {"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, - Y: 78, + X: 86, + Y: 78, }, - Width: 154, - Height: 199, - Angle: 90.45744, - Confidence: 1., + Width: 154, + Height: 199, + Angle: 90.45744, + Confidence: 1., }, }, {"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, - Y: 87, + X: 109, + Y: 87, }, - Width: 103, - Height: 247, - Angle: 79.6229, - Confidence: 1.0, + Width: 103, + Height: 247, + Angle: 79.6229, + Confidence: 1.0, }, }, } - for _, c := range cases{ - ellipse := rd.ComputeEllipsis(&c.contour) - if ellipse.String() != c.expectedEllipse.String(){ + for _, c := range cases { + 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()) } } diff --git a/part/part.go b/part/part.go index 11a4df7..168e4c8 100644 --- a/part/part.go +++ b/part/part.go @@ -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 + log.Debug("process msg") + go r.processFrame(&frame) 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)}) } diff --git a/part/part_test.go b/part/part_test.go index 9dd63e3..a20fe1d 100644 --- a/part/part_test.go +++ b/part/part_test.go @@ -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.}, }, }