Skip to content

Add python bindings to cudaobjdetect, cudawarping and cudaarithm #2396

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

Merged
merged 9 commits into from
Jan 29, 2020

Conversation

cudawarped
Copy link
Contributor

@cudawarped cudawarped commented Dec 22, 2019

Main PR: opencv/opencv#16248

resolves #2159 #2393 #2407

This pullrequest changes

Adds overloads to C++ functions to generate the missing python bindings for cudaobjdetect, cudawarping, cudaarithm and cudabgsegm functions.

Python tests have been added for the missing functions and all module specific python tests have also been moved here from the main repo.

force_builders=Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu-cuda:18.04

Add python wrapper to convolution funciton.
@cudawarped cudawarped changed the title Overload cudawarping functions to generate correct python bindings. Fix python bindings for cudawarping functions. Dec 22, 2019
@cudawarped cudawarped changed the title Fix python bindings for cudawarping functions. Add python bindings to cudaobjdetect, cudawarping and cudaarithm Dec 27, 2019
@cudawarped
Copy link
Contributor Author

Moved module specific python cuda tests from main opencv repo.
Updated SURF headers to generate python bindings and modified cv::cuda::meanShiftSegmentation() implementation to generate bindings consistent with the python documentation.

@alalek
Copy link
Member

alalek commented Jan 9, 2020

Could you please re-check modules/xfeatures2d/misc/python/test/test_cuda_xfeatures2d.py test / bindings ?

Crash log with python 2.7 (problem is this=0x0):

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffdce3b319 in (anonymous namespace)::SURF_CUDA_Invoker::SURF_CUDA_Invoker (this=0x7fffffffc0d0, surf=..., img=..., mask=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:148
148	            CV_Assert(surf_.nOctaves > 0 && surf_.nOctaveLayers > 0);
(gdb) p surf_
$1 = (cv::cuda::SURF_CUDA &) <error reading variable>
(gdb) bt
#0  0x00007fffdce3b319 in (anonymous namespace)::SURF_CUDA_Invoker::SURF_CUDA_Invoker (this=0x7fffffffc0d0, surf=..., img=..., mask=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:148
#1  0x00007fffdce3d01e in cv::cuda::SURF_CUDA::operator() (this=0x0, img=..., mask=..., keypoints=...) at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:386
#2  0x00007fffe0ea06f8 in cv::cuda::SURF_CUDA::detect (this=0x0, img=..., mask=..., keypoints=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp:156
#3  0x00007fffe0fb2363 in pyopencv_cv_cuda_cuda_SURF_CUDA_detect (self=0x7fffe2b3bef0, args=0x7fff93777460, kw=0x0)
    at modules/python_bindings_generator/pyopencv_generated_types_content.h:38382

Command line (../../dev is path to renamed "opencv" source repository):

PYTHONPATH=`pwd`/../../dev/modules/python/test ./setup_vars.sh gdb -ex r --args python ../../contrib/modules/xfeatures2d/misc/python/test/test_cuda_xfeatures2d.py --repo ../../dev

Python 3.7:

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffdc36a319 in (anonymous namespace)::SURF_CUDA_Invoker::SURF_CUDA_Invoker (this=0x7fffffffbc50, surf=..., img=..., mask=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:148
148	            CV_Assert(surf_.nOctaves > 0 && surf_.nOctaveLayers > 0);
(gdb) bt
#0  0x00007fffdc36a319 in (anonymous namespace)::SURF_CUDA_Invoker::SURF_CUDA_Invoker (this=0x7fffffffbc50, surf=..., img=..., mask=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:148
#1  0x00007fffdc36c01e in cv::cuda::SURF_CUDA::operator() (this=0x0, img=..., mask=..., keypoints=...) at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/src/surf.cuda.cpp:386
#2  0x00007fffe061d6f2 in cv::cuda::SURF_CUDA::detect (this=0x0, img=..., mask=..., keypoints=...)
    at /home/alalek/projects/opencv/contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp:156
#3  0x00007fffe072f5ef in pyopencv_cv_cuda_cuda_SURF_CUDA_detect (self=0x7fff92d97790, args=0x7fff930c9280, kw=0x0)
    at modules/python_bindings_generator/pyopencv_generated_types_content.h:38382
#4  0x00007ffff7ba8598 in _PyMethodDef_RawFastCallKeywords () from /usr/lib/libpython3.7m.so.1.0
#5  0x00007ffff7bda76f in _PyMethodDescr_FastCallKeywords () from /usr/lib/libpython3.7m.so.1.0

@cudawarped
Copy link
Contributor Author

cudawarped commented Jan 9, 2020

Thanks looking into this.

@asmorkalov asmorkalov self-assigned this Jan 10, 2020
@cudawarped
Copy link
Contributor Author

cudawarped commented Jan 10, 2020

Apologies I committed the old test case, before I introduced the create method.

I have left both the SURF_CUDA constructors unwrapped

    //! the default constructor
    SURF_CUDA();
    //! the full constructor taking all the necessary parameters
    explicit SURF_CUDA(double _hessianThreshold, int _nOctaves=4,
         int _nOctaveLayers=2, bool _extended=false, float _keypointsRatio=0.01f, bool _upright = false);

meaning that constructing cuSurf as

cuSurf = cv.cuda_SURF_CUDA(hessianThreshold,nOctaves,nOctaveLayers,extended,keypointsRatio,upright)

will construct and empty object and still throw an exception when calling

cuSurf.detect(cuMat,cv.cuda_GpuMat())

However this does force the user to call create() allowing python to catch the following exception

OpenCV(4.2.0-dev) Error: The function/feature is not implemented (This algorithm is patented and is excluded in this configuration. Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library) in cv::cuda::SURF_CUDA::create, file \modules\xfeatures2d\src\surf.cuda.cpp, line 56
s

when not compiled with OPENCV_ENABLE_NONFREE=ON and appears to be consistent with the behaviour of methods in the main repo. e.g.

bgmog2 = cv.BackgroundSubtractorMOG2()

works, however

bgmog2.getDefaultName()

throws an exception.

@cudawarped
Copy link
Contributor Author

Overloads have been added for mog and mog2 getBackgroundImage() and the documentation updated to reflect the type of the transform matrix required by the cudawarping functions.

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thank you 👍

@cudawarped
Copy link
Contributor Author

Updated KeyPoints in cuda::ORB::Convert python wrapper to be an output argument.

Comment on lines 401 to 403
_dst.create(src.size(), src.type());
Mat dstMat = _dst.getMat();
dst.copyTo(dstMat);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 lines

Did you try just dst.copyTo(_dst); call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but it works perfectly, I wrongly assumed that this worked the same for Mat and GpuMat.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it makes sense to improve GpuMat support in .copyTo() implementation (in a separate PR).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I can take a look at that once this has been merged.

@cudawarped
Copy link
Contributor Author

@alalek Made suggested changes.

@asmorkalov
Copy link
Contributor

@cudawarped Please remove useless inline option in functions that I mentioned before and rework MOG and MOG2 test to check output objects shape at least.

@alalek
Copy link
Member

alalek commented Jan 28, 2020

that I mentioned before

where?

@asmorkalov
Copy link
Contributor

👍

@asmorkalov asmorkalov self-requested a review January 29, 2020 06:11
Copy link
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@alalek alalek merged commit d7d6360 into opencv:master Jan 29, 2020
@AditTuf
Copy link

AditTuf commented Feb 22, 2021

@cudawarped can you please till me how do I patch this changes on my Jetson Nano which also uses Ubuntu

@cudawarped
Copy link
Contributor Author

@cudawarped can you please till me how do I patch this changes on my Jetson Nano which also uses Ubuntu

These changes were merged over a year ago. Using the latest version 4.5.1 will be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CUDA HOG does not expose detect methods to other languages
4 participants