I build OpenCV 4.5.5 from source in windows and I activated OPENCV_ENABLE_NONFREE with the help of CMAKE. As I tried 'surf = cv.xfeatures2d.SURF_create(400)' outside of my anaconda environment I am getting no error and it works perfetly...but when I try the same thing inside of my anaconda environment I am getting error module 'cv2' has no attribute 'xfeatures2d'
Asked
Active
Viewed 141 times
0
-
1you didn't install it into that environment, so why should it be there? you have a different OpenCV in that environment, one that you installed previously. – Christoph Rackwitz Feb 19 '22 at 15:19
-
I just followed this tutorial to install OpenCV : [link] https://cv-tricks.com/how-to/installation-of-opencv-4-1-0-in-windows-10-from-source/ – bilel ayech Feb 19 '22 at 15:39
-
so what you suggest to make SURF work also in my anaconda environment ? – bilel ayech Feb 19 '22 at 15:41
1 Answers
0
Instructions how to build open-cv with GPU support on windows on an anaconda environment (also fix for autocomplete not available):
"""
opencv cuda installation on WINDOWS 10:
youtube https://www.youtube.com/watch?v=YsmhKar8oOc&ab_channel=TheCodingBug
** all links for download in the link
** in brackets: my specific installation
* install anaconda
* vs studio 19 with "desktop development C++" and "python development"
* Cuda toolkit (cuda toolkit 10.2)
* cuDNN - version that match the cuda toolkit above (cuDnn v7.6.5 for Cuda 10.2)
* archive: https://developer.nvidia.com/rdp/cudnn-archive
* copy all to cuda (bin to bin, lib to lib, include to include)
* minimum 7.5
* CMake
* openCV source (4.5.2)
* openCV contrib - same version as openCV above (4.5.2)
* place openCV and openCV contrib in the same folder and extract both
1 build a new python env anywhere(conda(base or custom), normal py, venv...)
2 set 4 system environment variables:
let PY_PATH be you python dir (conda env, normal py, venv...)
go to PATH and add the next 4:
* PY_PATH # for the python.exe
* PY_PATH/Scripts # for pip...
* PY_PATH/libs # for python36.lib file
* PY_PATH/include # h files
2b pip install numpy (if you want tf, 1.19.5)
3 open CMake gui
* source code: location of openCV source
* create 2 new dirs in the level of openCV source called build and install
* where to build: location of build folder created above
* configure, set generator x64 and finish
* click grouped checkbox and look in PYTHON3
should have EXECUTABLE, INCLUDE_DIR, LIBRARY, NUMPY_INCLUDE, PACKAGES paths filled according to PY_PATH
look at the output and search for OpenCV modules. if python3 in Unavailable - don't continue. it should be
in the "To be built"
* extra round of flags:
WITH_CUDA
BUILD_opencv_dnn
OPENCV_DNN_CUDA
ENABLE_FAST_MATH
BUILD_opencv_world
BUILD_opencv_python3 # should already be on
OPENCV_EXTRA_MODULES_PATH -> set to openCV contrib on modules folder
* hit configure - in the output you should see your CUDA and cuDNN details
CUDA detected: x.y (10.2)
Found CUDNN path ...
* extra round of flags:
CUDA_FAST_MATH
CUDA_ARCH_BIN -> go to https://en.wikipedia.org/wiki/CUDA and look for your GPU. find it's Compute
capability (version). (my GPU is gtx 1050. version 6.1)
remove all versions other than you GPU version. (i left only 6.1)
CMAKE_INSTALL_PREFIX -> place install path from above (you create this dir with build above)
CMAKE_CONFIGURATION_TYPES -> remove Debug and keep only Release
* hit configure
* hit generate
close CMAKE gui
4 go to build folder and look for OpenCV.sln
* goto solution explorer->CMakeTargets-> right click ALL_BUILD and hit build # should take 10-30 mins
* if done with no errors, right click INSTALL and build.
* if no errors, openCV cuda is ready
5 validation: open terminal and write python (should work due to step 2)
import cv2
print(cv2.__version__)
print(cv2.cuda.getCudaEnabledDeviceCount()) # should be >=1
run this:
pip install wizzi_utils
import wizzi_utils as wu
wu.cvt.test.cuda_on_gpu_test()
6 cleanup
* delete all build folder except build/lib # maybe for future use ?
* delete both zips and extracted open cv and open cv contrib
7 after checking on pycharm, open cv with GPU support should work but no autocomplete.
pip install mypy
stubgen -m cv2 -o ENV_PATH\Lib\site-packages\cv2
# if the stubgen fails, run as administrator
# rename cv2.pyi created to __init__.pyi
# all done
e.g:
stubgen -m cv2 -o C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\cv_cuda\\Lib\\site-packages\\cv2
# a file was created at C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\temp\\Lib\\site-packages\\cv2\\cv2.pyi
# rename cv2.pyi to __init__.pyi and have the file:
C:\\Users\\GiladEiniKbyLake\\.conda\\envs\\temp\\Lib\\site-packages\\cv2\\__init__.pyi
:return:
"""

gilad eini
- 360
- 2
- 6
-
do I need to install cuda ? what should I change in my cmake gui ? – bilel ayech Feb 19 '22 at 16:26
-
Cuda toolkit (i installed cuda toolkit 10.2) and cuDNN - version that match the cuda toolkit above – gilad eini Feb 19 '22 at 18:49
-
that means I have to install Cuda toolkit to make SURF work in my anaconda environment right ? without Cuda doesn't work right ? because I tried to change path into my anaconda environment in CMAKE Gui but still not working – bilel ayech Feb 19 '22 at 19:15
-
you **do not need** CUDA. you just need to understand the environment you've created (are you in the virtualenv or not) – Christoph Rackwitz Feb 19 '22 at 19:18
-
my apologies, as @ChristophRackwitz said, you do not need cuda. you mentioned you build open cv from source and wrongfully i assumed you built it for GPU support. my answer was for building it with GPU support on Ananconda. – gilad eini Feb 20 '22 at 08:01
-
As for your issue, if you built the open-cv correctly with open-cv-contrib and checked the OPENCV_ENABLE_NONFREE it should have worked. here is a post with a good answer using an old version on anaconda [link](https://stackoverflow.com/questions/52305578/sift-cv2-xfeatures2d-sift-create-not-working-even-though-have-contrib-instal) – gilad eini Feb 20 '22 at 08:07