I'm compiling opencv3.4.9 and contrib3.4.9. But it doesn't work with python2.7[SOLVED]
I solved many problems and finally, cmake, make, sudo make install, but when I run this code:
sift = cv2.xfeatures2d.SIFT_create()
in python, it says
AttributeError: 'module' object has no attribute 'xfeatures2d'
although everything works perfectly well in c++, I can totally use SIFT in c++. I wonder how I can solve this problem so that I'm able to also use SIFT in python.
here is my Cmake config:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/Users/lixin/Desktop/material/opencv/opencv_contrib-3.4.9/modules \
-D BUILD_EXAMPLES=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D PYTHON_INCLUDE_DIR=/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/ ..
In the output of Cmake, I notice something unusual, that xfeatures2d is in both To be built list and Unavailable list but I don't know why:
OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf java js matlab ovis python3 sfm viz xfeatures2d
-- Applications: tests perf_tests examples apps
-- Documentation: NO
-- Non-free algorithms: YES
However, it goes well in the make and make install process, I can even find these infos:
[ 78%] Built target opencv_xfeatures2d
[ 78%] Built target example_xfeatures2d_surf_matcher
[ 78%] Built target example_xfeatures2d_shape_transformation
[ 78%] Built target example_xfeatures2d_pct_signatures
[ 79%] Built target example_xfeatures2d_pct_webcam
[ 80%] Built target opencv_perf_xfeatures2d
[ 80%] Built target example_xfeatures2d_video_homography
[ 80%] Built target example_xfeatures2d_gms_matcher
[ 80%] Built target opencv_test_xfeatures2d
[ 80%] Built target example_xfeatures2d_bagofwords_classification
opencv_python also builds, but won't work with xfeatures2d though:
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2
somebody please help!! I'm so desperate now....T_T
Also, I would like to know how I can build opencv with both python2 and python3, I've tried several ways but all failed. I am using MacOS, and my python3 is built in Xcode, it seems opencv only compiles with python2 for some reason.. I've tried:
-DPYTHON_DEFAULT_EXECUTABLE
-DPYTHON3_EXECUTABLE
-DPYTHON3_NUMPY_INCLUDE_DIRS
-DPYTHON3_PACKAGES_PATH
-DPYTHON3_LIBRARIES
-DPYTHON3_INCLUDE_DIRS
-D __INSTALL_PATH_PYTHON3
-DBUILD_NEW_PYTHON_SUPPORT=ON
-DBUILD_opencv_python3=ON
-DHAVE_opencv_python3=ON and such. Please HELP!!!
python 2 is finished also here
and please repalce all your screenshost with text
I found this in one of the links: "upcoming 4.2.0 / 3.4.9 opencv-python release will be the last release which includes Python 2.7 builds." and I'm using 3.4.9 luckily :-) , plus I can't build cv2.so for python3.7 as I said...
@LBerger It's all replaced now.
please check
cv2.getBuildInformation()
, you might be using an older version, not the one you've builtalso you need:
cmake - DOPENCV_ENABLE_NONFREE=ON
for sift/surfI do have - DOPENCV_ENABLE_NONFREE=ON in my cmake options. when I run cv2.getBuildInformation(), I get something like this: '\nGeneral configuration for OpenCV 3.4.9 =====================================\n Version control: unknown\n\n Extra modules:\n Location (extra): /Users/lixin/Desktop/material/opencv/opencv_contrib-3.4.9/modules\n Version control (extra): unknown\n\n Platform:\n Timestamp: 2020-02-17T07:08:30Z\n Host: Darwin 19.3.0 x86_64\n CMake: 3.16.4\n CMake generator: Unix Makefiles\n CMake build tool: /usr/bin/make\n Configuration: RELEASE\n\n CPU/HW features:\n Baseline: SSE SSE2 SSE3 SSSE3 SSE @berak
I don't know how to build python binding on linux but may be you will be able to find some recipes here https://github.com/skvark/opencv-pyth...
Now are you sure that your installed version is builded version? uninstall opencv_contrib and opencv and try import cv2 an error must occured and then rebuild python binding
@LBerger I run cv2.__version__ and it shows'3.4.9', and when I move cv2.so to trash then I can't import cv2 any more. no module named cv2. So I guess it is the builded version.
You're using in cmake
PYTHON_INCLUDE_DIR=/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/
This doesn't run on python 3@supra56 thanks for trying to help. I didn't use that option when I tried to build opencv_python3, but still it never builds, only opencv_python2 builds. I've given up on opencv_python3 already, now the problem is the built cv2.so for python2 does not contain xfeatures2d. I think it might because xfeatures2d is in Unavailable list of cmake output, but I don't know what to do to remove xfeatures2d from Unavailable list..