Procedure to custom build opencv 3.1 for Python 3.5 - 32 bit on Windows 10
- Download Python3.5.2
- Install numpy (atleast), scipy, matplotlib from http://www.lfd.uci.edu/~gohlke/pythonlibs/. All libraries should be 32-bit version for Python 3.5 version
- Install CMake (3.4.1 or later version recommended)
- Download opencv3.1 for windows from http://opencv.org/downloads.html
- Extract it to a folder where you want to build the library. In this example it is C:\opencv_310
- After extracting, you will get two folders - build and source. Pre-build library has support for Python 2.7. We will need to build the library from source code for Python 3.5
- Make new folder. In this example, it is named as 'build_custom'
- Open CMake and put path for source code 'C:/opencv_310/opencv/sources'
- Put path for building the binaries 'C:/opencv_310/opencv/build_custom'
- Click Configure
- Enter the path for Python3.5
- PYTHON3_EXECUTABLE 'C:/Python35/python.exe'
- PYTHON3_INCLUDE_DIR 'C:/Python35/lib/site-packages/numpy/core/include'
- PYTHON3_LIBRARY 'C:/Python35/libs/python35.lib'
- PYTHON3_NUMPY_INCLUDE_DIRS 'C:/Python35/lib/site-packages/numpy/core/include'
- PYTHON3_PACKAGES_PATH 'C:/Python35/Lib/site-packages'
- In my case, I wanted to enable Ximea camera with OpenCV so I ticked 'WITH_XIMEA'
- Untick BUILD_EXAMPLES, BUILD_PERF_TESTS, BUILD_TESTS
- Click Configure
- Make sure that 'BUILD_opencv_python3' is ticked on. Click Configure again.
- Make sure there are no red lines after configure. Now click Generate.
In my case I was trying to get Ximea camera enabled with OpenCV. For that purpose before buliding solution with VisualStudio, make sure you add 'C:\XIMEA\API\x86\' to Environment Variables first.
- Go to folder 'C:\opencv_310\opencv\build_custom'
- Click on "OpenCV.sln" and open it in Visual Studio (I had VisualStudio 10 installed on my system)
- In VisualStudio, make sure you have 'open_python3' in 'bindings' section
- Click on Build -> Build Solution. It will take some time to build all the libraries. At the end of build, it should display "29 succeeded, 1 failed"
- Number of succeeded build libraries may vary. But for failed, only 1 should fail where python35_d.lib is not found, mainly because Python3.5 doesnt have debug mode library.
- Now change from Debug to Release mode and click on the green arrow next to it.
- It will build all the libraries in release mode and this time it should say "30 succeeded, 0 failed"
- Now environment variables are to be changed. Go to 'My Computer -> Right click -> Properties -> Advanced system settings -> Environment Variables'
- In user variables, add NEW, Variable Name = 'OPENCV_DIR', Variable Path = 'C:\opencv_310\opencv\build_custom'
- In system variables, add NEW, Variable Name = 'C:\opencv_310\opencv\build_custom\bin'. Click OK
- Check if the changes in environment variables are registered. Open command prompt and enter PATH (check for system variable) and system opencv)
- Now go to the folder 'C:\opencv_310\opencv\build_custom\lib\python3\Release' and copy 'cv2.cp35-win32.pyd' to 'C:\Python35\Lib\site-packages'
- Now go to the folder 'C:\opencv_310\opencv\build_custom\bin\Release' and copy 17 DLLs to ''C:\Python35\Lib\site-packages'
- Copy 'opencv_ffmpeg310.dll' from the same folder to 'C:\Python35'
- Now go to the directory where Python is installed - "C:\Python35". Open python console by entering 'python'
- Once Python console is open, enter 'import cv2'. You should get no error.
- Enter 'print(cv2.getBuildInformation())'. It will display all the build properties for OpenCV.
In my case I was trying to get Ximea camera enabled with OpenCV. For that purpose before buliding solution with VisualStudio, make sure you add 'C:\XIMEA\API\x86\' to Environment Variables first.
Or else you will get error while building the solution - 'xiApi.h' not found.
Common errors
1. Make sure everything is 32 bit
Make sure User Variable and System Variable path are correct (This part costed me a lot of time)
Make sure no red lines are present while configuring CMake
Make sure BUILD_opencv_python3 is ticked on
Make sure 'cv2.cp35-win32.pyd' and all 17 DLLs are copied to the right place