Ask Your Question
0

I'm compiling opencv3.4.9 and contrib3.4.9. But it doesn't work with python2.7[SOLVED]

asked 2020-02-16 18:15:18 -0600

XinLi gravatar image

updated 2020-02-18 21:42:03 -0600

supra56 gravatar image

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!!!

edit retag flag offensive close merge delete

Comments

python 2 is finished also here

and please repalce all your screenshost with text

LBerger gravatar imageLBerger ( 2020-02-16 20:48:33 -0600 )edit

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...

XinLi gravatar imageXinLi ( 2020-02-17 00:53:32 -0600 )edit
1

@LBerger It's all replaced now.

XinLi gravatar imageXinLi ( 2020-02-17 01:29:13 -0600 )edit

please check cv2.getBuildInformation() , you might be using an older version, not the one you've built

also you need: cmake - DOPENCV_ENABLE_NONFREE=ON for sift/surf

berak gravatar imageberak ( 2020-02-17 02:05:27 -0600 )edit

I 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

XinLi gravatar imageXinLi ( 2020-02-17 02:12:22 -0600 )edit

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 gravatar imageLBerger ( 2020-02-17 02:13:52 -0600 )edit

@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.

XinLi gravatar imageXinLi ( 2020-02-17 02:19:15 -0600 )edit

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 gravatar imagesupra56 ( 2020-02-17 14:46:05 -0600 )edit
1

@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..

XinLi gravatar imageXinLi ( 2020-02-18 02:14:30 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2020-02-18 04:36:39 -0600

XinLi gravatar image

Problem Solved!!!✌️, @LBerger @berak @supra56 thanks all you guys for help. It is because that xfeatures2d requires boostdesc_bgm.i etc, and I have a bad internet connection, so I download them manually, and changed the download link in download_boostdesc.cmake to my local filesystem. However, this causes hash mismatch for those files, so cmake print out warnings and make xfeatures2d unavailable automatically. Everything goes well after I remove that statement that disables xfeatures2d for hash mismatch. Thanks a lot guys!!!(The system says that new users must wait 2 days to answer their own question, so I just leave a comment here.)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-16 15:04:10 -0600

Seen: 1,300 times

Last updated: Feb 18 '20