From 6a42abd2d790479577a9e22732e5d10b8e559c03 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 22 Apr 2021 20:37:07 +0200 Subject: [PATCH 1/7] test: add gotest for more colorful test output running tests from containers --- Dockerfile-test | 19 +++++++++++++++++++ Dockerfile-test.gpu-cuda-10 | 12 ++++++++++-- Dockerfile-test.gpu-cuda-11 | 12 ++++++++++-- go.mod | 5 ++++- go.sum | 2 ++ 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 Dockerfile-test diff --git a/Dockerfile-test b/Dockerfile-test new file mode 100644 index 00000000..377bb311 --- /dev/null +++ b/Dockerfile-test @@ -0,0 +1,19 @@ +# To build: +# docker build -f Dockerfile-test -t gocv-test . +# +# To run tests: +# xhost + +# docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix gocv-test +# xhost - +# +FROM gocv/opencv:4.5.2 AS gocv-test + +ENV GOPATH /go + +COPY . /go/src/gocv.io/x/gocv/ + +WORKDIR /go/src/gocv.io/x/gocv + +RUN go get -u github.com/rakyll/gotest + +ENTRYPOINT ["gotest", "-v", ".", "./contrib/..."] diff --git a/Dockerfile-test.gpu-cuda-10 b/Dockerfile-test.gpu-cuda-10 index 0c2c6804..59d54010 100644 --- a/Dockerfile-test.gpu-cuda-10 +++ b/Dockerfile-test.gpu-cuda-10 @@ -1,4 +1,11 @@ +# To build: # docker build -f Dockerfile-test.gpu-cuda-10 -t gocv-test-gpu-cuda-10 . +# +# To run tests: +# xhost + +# docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --gpus all gocv-test-gpu-cuda-10 +# xhost - +# FROM gocv/opencv:4.5.2-gpu-cuda-10 AS gocv-gpu-test-cuda-10 ENV GOPATH /go @@ -6,6 +13,7 @@ ENV GOPATH /go COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -RUN go build -tags example -o /build/gocv_cuda_version ./cmd/cuda/ -ENTRYPOINT ["go", "test", "-v", "./cuda/..."] +RUN go get -u github.com/rakyll/gotest + +ENTRYPOINT ["gotest", "-v", ".", "./contrib/...", "./cuda/..."] diff --git a/Dockerfile-test.gpu-cuda-11 b/Dockerfile-test.gpu-cuda-11 index fb57ef37..f91bdfa7 100644 --- a/Dockerfile-test.gpu-cuda-11 +++ b/Dockerfile-test.gpu-cuda-11 @@ -1,5 +1,11 @@ -# to build this docker image: +# To build: # docker build -f Dockerfile-test.gpu-cuda-11 -t gocv-test-gpu-cuda-11 . +# +# To run tests: +# xhost + +# docker run -it --rm --gpus all -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix gocv-test-gpu-cuda-11 +# xhost - +# FROM gocv/opencv:4.5.2-gpu-cuda-11 AS gocv-gpu-test-cuda-11 ENV GOPATH /go @@ -8,4 +14,6 @@ COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -ENTRYPOINT ["go", "test", "-v", "./cuda/..."] \ No newline at end of file +RUN go get -u github.com/rakyll/gotest + +ENTRYPOINT ["gotest", "-v", ".", "./contrib/...","./cuda/..."] diff --git a/go.mod b/go.mod index b8add96f..190fda26 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module gocv.io/x/gocv go 1.13 -require github.com/pascaldekloe/goe v0.1.0 +require ( + github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e + github.com/pascaldekloe/goe v0.1.0 +) diff --git a/go.sum b/go.sum index a0a5f274..1b71bf1b 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e h1:xCcwD5FOXul+j1dn8xD16nbrhJkkum/Cn+jTd/u1LhY= +github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= From d00eb3302126486f4e060d3cb8334cd7de997478 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 22 Apr 2021 20:40:28 +0200 Subject: [PATCH 2/7] test: add gotest for more colorful test output running tests from containers --- Dockerfile-test.gpu-cuda-10 | 8 ++------ Dockerfile-test.gpu-cuda-11 | 6 ++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Dockerfile-test.gpu-cuda-10 b/Dockerfile-test.gpu-cuda-10 index 59d54010..a24d962c 100644 --- a/Dockerfile-test.gpu-cuda-10 +++ b/Dockerfile-test.gpu-cuda-10 @@ -2,9 +2,7 @@ # docker build -f Dockerfile-test.gpu-cuda-10 -t gocv-test-gpu-cuda-10 . # # To run tests: -# xhost + -# docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --gpus all gocv-test-gpu-cuda-10 -# xhost - +# docker run -it --rm --gpus all gocv-test-gpu-cuda-10 # FROM gocv/opencv:4.5.2-gpu-cuda-10 AS gocv-gpu-test-cuda-10 @@ -14,6 +12,4 @@ COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -RUN go get -u github.com/rakyll/gotest - -ENTRYPOINT ["gotest", "-v", ".", "./contrib/...", "./cuda/..."] +ENTRYPOINT ["gotest", "-v", "./cuda/..."] diff --git a/Dockerfile-test.gpu-cuda-11 b/Dockerfile-test.gpu-cuda-11 index f91bdfa7..3c7fde07 100644 --- a/Dockerfile-test.gpu-cuda-11 +++ b/Dockerfile-test.gpu-cuda-11 @@ -2,9 +2,7 @@ # docker build -f Dockerfile-test.gpu-cuda-11 -t gocv-test-gpu-cuda-11 . # # To run tests: -# xhost + -# docker run -it --rm --gpus all -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix gocv-test-gpu-cuda-11 -# xhost - +# docker run -it --rm --gpus all gocv-test-gpu-cuda-11 # FROM gocv/opencv:4.5.2-gpu-cuda-11 AS gocv-gpu-test-cuda-11 @@ -16,4 +14,4 @@ WORKDIR /go/src/gocv.io/x/gocv RUN go get -u github.com/rakyll/gotest -ENTRYPOINT ["gotest", "-v", ".", "./contrib/...","./cuda/..."] +ENTRYPOINT ["gotest", "-v", "./cuda/..."] From d94c04b2153ae7c5ae88aa5e5dcc9824309b6753 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 22 Apr 2021 20:49:03 +0200 Subject: [PATCH 3/7] test: add gotest for more colorful test output running tests from containers --- Dockerfile-test.gpu-cuda-10 | 2 ++ Dockerfile-test.gpu-cuda-11 | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile-test.gpu-cuda-10 b/Dockerfile-test.gpu-cuda-10 index a24d962c..5f3bb5ac 100644 --- a/Dockerfile-test.gpu-cuda-10 +++ b/Dockerfile-test.gpu-cuda-10 @@ -12,4 +12,6 @@ COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv +RUN curl http://storage.googleapis.com/jbd-releases/gotest_linux > gotest && chmod +x gotest + ENTRYPOINT ["gotest", "-v", "./cuda/..."] diff --git a/Dockerfile-test.gpu-cuda-11 b/Dockerfile-test.gpu-cuda-11 index 3c7fde07..1766d163 100644 --- a/Dockerfile-test.gpu-cuda-11 +++ b/Dockerfile-test.gpu-cuda-11 @@ -12,6 +12,6 @@ COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -RUN go get -u github.com/rakyll/gotest +RUN curl http://storage.googleapis.com/jbd-releases/gotest_linux > gotest && chmod +x gotest ENTRYPOINT ["gotest", "-v", "./cuda/..."] From 42f62d6b11f868966497f0d0ed62672eaf55c3c4 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Thu, 22 Apr 2021 20:58:07 +0200 Subject: [PATCH 4/7] test: add gotest for more colorful test output running tests from containers --- Dockerfile-test.gpu-cuda-10 | 3 ++- Dockerfile-test.gpu-cuda-11 | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile-test.gpu-cuda-10 b/Dockerfile-test.gpu-cuda-10 index 5f3bb5ac..838b498c 100644 --- a/Dockerfile-test.gpu-cuda-10 +++ b/Dockerfile-test.gpu-cuda-10 @@ -7,11 +7,12 @@ FROM gocv/opencv:4.5.2-gpu-cuda-10 AS gocv-gpu-test-cuda-10 ENV GOPATH /go +ENV PATH="${PATH}:/go/bin" COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -RUN curl http://storage.googleapis.com/jbd-releases/gotest_linux > gotest && chmod +x gotest +RUN go get -u github.com/rakyll/gotest ENTRYPOINT ["gotest", "-v", "./cuda/..."] diff --git a/Dockerfile-test.gpu-cuda-11 b/Dockerfile-test.gpu-cuda-11 index 1766d163..3762e7e1 100644 --- a/Dockerfile-test.gpu-cuda-11 +++ b/Dockerfile-test.gpu-cuda-11 @@ -7,11 +7,12 @@ FROM gocv/opencv:4.5.2-gpu-cuda-11 AS gocv-gpu-test-cuda-11 ENV GOPATH /go +ENV PATH="${PATH}:/go/bin" COPY . /go/src/gocv.io/x/gocv/ WORKDIR /go/src/gocv.io/x/gocv -RUN curl http://storage.googleapis.com/jbd-releases/gotest_linux > gotest && chmod +x gotest +RUN go get -u github.com/rakyll/gotest ENTRYPOINT ["gotest", "-v", "./cuda/..."] From 1a7d1cc28403c9689489a524935003eca421a165 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 11 Jun 2022 13:14:31 +0200 Subject: [PATCH 5/7] cuda: add implementations for ConvertToWithParams and ConvertToWithParamsStream Signed-off-by: deadprogram --- cuda/cuda.cpp | 8 ++++++++ cuda/cuda.go | 20 ++++++++++++++++++++ cuda/cuda.h | 1 + cuda/cuda_test.go | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/cuda/cuda.cpp b/cuda/cuda.cpp index 3195ba90..9da50b02 100644 --- a/cuda/cuda.cpp +++ b/cuda/cuda.cpp @@ -68,6 +68,14 @@ void GpuMat_ConvertTo(GpuMat m, GpuMat dst, int type, Stream s) { m->convertTo(*dst, type, *s); } +void GpuMat_ConvertToWithParams(GpuMat m, GpuMat dst, int type, float alpha, float beta, Stream s) { + if (s == NULL) { + m->convertTo(*dst, type, alpha, beta); + return; + } + m->convertTo(*dst, type, alpha, beta, *s); +} + void GpuMat_CopyTo(GpuMat m, GpuMat dst, Stream s) { if (s == NULL) { m->copyTo(*dst); diff --git a/cuda/cuda.go b/cuda/cuda.go index 542670af..0b88da0d 100644 --- a/cuda/cuda.go +++ b/cuda/cuda.go @@ -152,6 +152,26 @@ func (m *GpuMat) ConvertToWithStream(dst *GpuMat, mt gocv.MatType, s Stream) { return } +// ConvertToWithParams converts GpuMat into destination GpuMat. +// +// For further details, please see: +// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a3a1b076e54d8a8503014e27a5440d98a +// +func (m *GpuMat) ConvertToWithParams(dst *GpuMat, mt gocv.MatType, alpha, beta float32) { + C.GpuMat_ConvertToWithParams(m.p, dst.p, C.int(mt), C.float(alpha), C.float(beta), nil) + return +} + +// ConvertToWithParamsStream converts GpuMat into destination GpuMat. +// +// For further details, please see: +// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a3a1b076e54d8a8503014e27a5440d98a +// +func (m *GpuMat) ConvertToWithParamsStream(dst *GpuMat, mt gocv.MatType, alpha, beta float32, s Stream) { + C.GpuMat_ConvertToWithParams(m.p, dst.p, C.int(mt), C.float(alpha), C.float(beta), s.p) + return +} + // CopyTo copies GpuMat into destination GpuMat. // // For further details, please see: diff --git a/cuda/cuda.h b/cuda/cuda.h index 7e9f2e9f..2a8a9dea 100644 --- a/cuda/cuda.h +++ b/cuda/cuda.h @@ -26,6 +26,7 @@ void GpuMat_Download(GpuMat m, Mat dst, Stream s); void GpuMat_Close(GpuMat m); int GpuMat_Empty(GpuMat m); void GpuMat_ConvertTo(GpuMat m, GpuMat dst, int type, Stream s); +void GpuMat_ConvertToWithParams(GpuMat m, GpuMat dst, int type, float alpha, float beta, Stream s); void GpuMat_CopyTo(GpuMat m, GpuMat dst, Stream s); GpuMat GpuMat_Reshape(GpuMat m, int cn, int rows); int GpuMat_Cols(GpuMat m); diff --git a/cuda/cuda_test.go b/cuda/cuda_test.go index 0ed1805a..90c72285 100644 --- a/cuda/cuda_test.go +++ b/cuda/cuda_test.go @@ -72,6 +72,29 @@ func TestNewGpuMatWithSize(t *testing.T) { } } +func TestConvertToWithParams(t *testing.T) { + src1 := gocv.IMRead("../images/gocvlogo.jpg", gocv.IMReadColor) + if src1.Empty() { + t.Error("Invalid read of Mat in ConvertToWithParams test") + } + defer src1.Close() + + var cimg1, dimg = NewGpuMat(), NewGpuMat() + defer cimg1.Close() + defer dimg.Close() + + cimg1.Upload(src1) + + dest := gocv.NewMat() + defer dest.Close() + + cimg.ConvertToWithParams(&dimg, cimg.Type(), 1.0, 10.0) + dimg.Download(&dest) + if dest.Empty() || src1.Rows() != dest.Rows() || src1.Cols() != dest.Cols() { + t.Error("Invalid ConvertToWithParams test") + } +} + func TestGetCudaEnabledDeviceCount(t *testing.T) { if GetCudaEnabledDeviceCount() < 1 { t.Fatal("expected atleast one cuda enabled device") From 43177ab95bda938f7f0afa88588617e8c5bde659 Mon Sep 17 00:00:00 2001 From: hehao <908438654@qq.com> Date: Wed, 6 Jul 2022 01:33:04 +0800 Subject: [PATCH 6/7] Fix the problem that the non-maximum suppression return position is 0 --- dnn.go | 3 ++- dnn_test.go | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/dnn.go b/dnn.go index 979d68b3..e32691bd 100644 --- a/dnn.go +++ b/dnn.go @@ -522,7 +522,7 @@ func (l *Layer) OutputNameToIndex(name string) int { // // For futher details, please see: // https://docs.opencv.org/4.4.0/d6/d0f/group__dnn.html#ga9d118d70a1659af729d01b10233213ee -func NMSBoxes(bboxes []image.Rectangle, scores []float32, scoreThreshold float32, nmsThreshold float32, indices []int) { +func NMSBoxes(bboxes []image.Rectangle, scores []float32, scoreThreshold float32, nmsThreshold float32, indices []int, count *int) { bboxesRectArr := []C.struct_Rect{} for _, v := range bboxes { bbox := C.struct_Rect{ @@ -563,6 +563,7 @@ func NMSBoxes(bboxes []image.Rectangle, scores []float32, scoreThreshold float32 for i := 0; i < int(indicesVector.length); i++ { indices[i] = int(ptr[i]) } + *count = int(indicesVector.length) return } diff --git a/dnn_test.go b/dnn_test.go index e4474bf7..3851cfd1 100644 --- a/dnn_test.go +++ b/dnn_test.go @@ -594,11 +594,13 @@ func TestNMSBoxes(t *testing.T) { indices := make([]int, 10) scoreThreshold := float32(0.5) nmsThreshold := float32(0.4) + var count int - NMSBoxes(bboxes, scores, scoreThreshold, nmsThreshold, indices) - - if indices[0] != 3 { - t.Errorf("Invalid NMSBoxes test indices: %v", indices) + NMSBoxes(bboxes, scores, scoreThreshold, nmsThreshold, indices, &count) + for i := count - 1; i >= 0; i-- { + if indices[i] != 3 { + t.Errorf("Invalid NMSBoxes test indices: %v", indices) + } } } From 5bfe889966be95c9742eb69fac4feb8eac95c03b Mon Sep 17 00:00:00 2001 From: deadprogram Date: Wed, 24 Aug 2022 17:20:04 +0200 Subject: [PATCH 7/7] docker: build arm64 OpenCV 4.6.0 image --- Dockerfile.opencv-arm64 | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Dockerfile.opencv-arm64 diff --git a/Dockerfile.opencv-arm64 b/Dockerfile.opencv-arm64 new file mode 100644 index 00000000..7f3c8c25 --- /dev/null +++ b/Dockerfile.opencv-arm64 @@ -0,0 +1,48 @@ +# to build this docker image: +# docker build -f Dockerfile.opencv -t gocv/opencv:4.6.0 . +FROM golang:1.18-buster AS opencv +LABEL maintainer="hybridgroup" + +RUN apt-get update && apt-get install -y --no-install-recommends \ + git build-essential cmake pkg-config unzip libgtk2.0-dev \ + curl ca-certificates libcurl4-openssl-dev libssl-dev \ + libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \ + libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev && \ + rm -rf /var/lib/apt/lists/* + +ARG OPENCV_VERSION="4.6.0" +ENV OPENCV_VERSION $OPENCV_VERSION + +RUN curl -Lo opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \ + unzip -q opencv.zip && \ + curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \ + unzip -q opencv_contrib.zip && \ + rm opencv.zip opencv_contrib.zip +RUN cd opencv-${OPENCV_VERSION} && \ + mkdir build && cd build && \ + cmake -D CMAKE_BUILD_TYPE=RELEASE \ + -D CMAKE_INSTALL_PREFIX=/usr/local \ + -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${OPENCV_VERSION}/modules \ + -D ENABLE_NEON=ON \ + -D WITH_FFMPEG=ON \ + -D WITH_TBB=ON \ + -D BUILD_TBB=ON \ + -D BUILD_TESTS=OFF \ + -D WITH_EIGEN=OFF \ + -D WITH_GSTREAMER=OFF \ + -D WITH_V4L=ON \ + -D WITH_LIBV4L=ON \ + -D WITH_VTK=OFF \ + -D WITH_QT=OFF \ + -D OPENCV_ENABLE_NONFREE=ON \ + -D INSTALL_C_EXAMPLES=OFF \ + -D INSTALL_PYTHON_EXAMPLES=OFF \ + -D BUILD_opencv_python3=TRUE \ + -D OPENCV_GENERATE_PKGCONFIG=ON \ + -D BUILD_EXAMPLES=OFF \ + -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/aarch64-gnu.toolchain.cmake .. && \ + make -j $(nproc --all) && \ + make preinstall && make install && ldconfig && \ + cd / && rm -rf opencv* + +CMD ["go version"]