简体   繁体   中英

Compiling OpenCV: how to build the cv2.so module for python3.6?

I have been struggling with compiling OpenCV for a while. The system is Linux Ubuntu 16.10.

Unfortunately it seems that the only library binding / module for Python that is being compiled is the one for Python 2.7, that gets installed at this location:

/usr/local/lib/python2.7/dist-packages/cv2.so

What I need instead is cv2 for python3.6 . I have set up the link /usr/bin/python to point to /usr/bin/python3.6 .

this is my cmake directive:

cmake \                                                                                   
-D CMAKE_BUILD_TYPE=RELEASE \                                                         
-D CMAKE_INSTALL_PREFIX=/usr/local \                                                  
-D BUILD_NEW_PYTHON_SUPPORT=ON \                                                      
-D INSTALL_C_EXAMPLES=ON \                                                            
-D INSTALL_PYTHON_EXAMPLES=ON \                                                       
-D BUILD_EXAMPLES=ON \                                                                
-DENABLE_PRECOMPILED_HEADERS=OFF \                                                    
-D OPENCV_EXTRA_MODULES_PATH=/home/foobar/Downloads/opencv_contrib/modules \
..         

any idea on what might be going wrong?

I followed the instructions here Install opencv for Python 3.3 on my Debian 8. All went well! import cv2 passes.

-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 \

I suggest the main key for your Cmake should be:

  -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \

Please check Cmake output for Python version for build.

I am currently working with Google VM (ubuntu 14.04). Installing opencv on python3.4 version has been quite a task. I wanted opencv to be installed for python 3.4 but everytime it was getting installed on 2.7 version.

I will share the steps I followed so as to help others on it.

Step 1 Follow all the steps as mentioned on openCv installation part till cmake. Link is given below: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

Note: Install all the 3 packages mentioned at start. That optional one too..!! And dont forget to change the python version for which you are installing.

I did

sudo apt-get install python3 -dev python3 -numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Follow step 2 for cmake.

Step 2 For installing opencv in specific version of python (ubuntu), you have to set the default (PYTHON_DEFAULT_EXECUTABLE) with the path to where your python is installed. You can find that out by using command whereis python3.4 (or, your version) . Mine was in /usr/bin/python3.4

Instead of cmake mentioned on the page, use this,

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..

Note: Dont forget to change your python version and path in PYTHON_DEFAULT_EXECUTABLE.

Step 3 Follow the remaining steps as mentioned in the link till sudo make install

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM