Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem that the non-maximum suppression return position is 0 #999

Open
wants to merge 12 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile-test.gpu-cuda-10
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# To run tests:
# docker run -it --rm --gpus all gocv-test-gpu-cuda-10
#

FROM gocv/opencv:4.6.0-gpu-cuda-10 AS gocv-gpu-test-cuda-10

ENV GOPATH /go
Expand Down
48 changes: 48 additions & 0 deletions Dockerfile.opencv-arm64
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 8 additions & 0 deletions cuda/cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions cuda/cuda.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions cuda/cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions cuda/cuda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion dnn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 6 additions & 4 deletions dnn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down