1

I had a lot of trouble while building opencv 3.1 from source code with Python 3.5 -32 bit on Windows 10. For any custom build, its a painfully long procedure which has to be followed meticulously. My purpose was to enable Ximea support with OpenCV for which I needed to custom build it.

Now that I have finally nailed it, I have decided to put the complete procedure here in case someone wants to repeat it. Believe me, it will save a lot of trouble. I will post the procedure in the answer.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
amathur45
  • 31
  • 3

1 Answers1

2

Procedure to custom build opencv 3.1 for Python 3.5 - 32 bit on Windows 10

  1. Download Python3.5.2
  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
  3. Install CMake (3.4.1 or later version recommended)
  4. Download opencv3.1 for windows from http://opencv.org/downloads.html
  5. Extract it to a folder where you want to build the library. In this example it is C:\opencv_310
  6. 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
  7. Make new folder. In this example, it is named as 'build_custom'
  8. Open CMake and put path for source code 'C:/opencv_310/opencv/sources'
  9. Put path for building the binaries 'C:/opencv_310/opencv/build_custom'
  10. Click Configure
  11. Enter the path for Python3.5
  12. PYTHON3_EXECUTABLE 'C:/Python35/python.exe'
  13. PYTHON3_INCLUDE_DIR 'C:/Python35/lib/site-packages/numpy/core/include'
  14. PYTHON3_LIBRARY 'C:/Python35/libs/python35.lib'
  15. PYTHON3_NUMPY_INCLUDE_DIRS 'C:/Python35/lib/site-packages/numpy/core/include'
  16. PYTHON3_PACKAGES_PATH 'C:/Python35/Lib/site-packages'
  17. In my case, I wanted to enable Ximea camera with OpenCV so I ticked 'WITH_XIMEA'
  18. Untick BUILD_EXAMPLES, BUILD_PERF_TESTS, BUILD_TESTS
  19. Click Configure
  20. Make sure that 'BUILD_opencv_python3' is ticked on. Click Configure again.
  21. 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.

  1. Go to folder 'C:\opencv_310\opencv\build_custom'
  2. Click on "OpenCV.sln" and open it in Visual Studio (I had VisualStudio 10 installed on my system)
  3. In VisualStudio, make sure you have 'open_python3' in 'bindings' section
  4. 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"
  5. 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.
  6. Now change from Debug to Release mode and click on the green arrow next to it.
  7. It will build all the libraries in release mode and this time it should say "30 succeeded, 0 failed"
  8. Now environment variables are to be changed. Go to 'My Computer -> Right click -> Properties -> Advanced system settings -> Environment Variables'
  9. In user variables, add NEW, Variable Name = 'OPENCV_DIR', Variable Path = 'C:\opencv_310\opencv\build_custom'
  10. In system variables, add NEW, Variable Name = 'C:\opencv_310\opencv\build_custom\bin'. Click OK
  11. Check if the changes in environment variables are registered. Open command prompt and enter PATH (check for system variable) and system opencv)
  12. 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'
  13. Now go to the folder 'C:\opencv_310\opencv\build_custom\bin\Release' and copy 17 DLLs to ''C:\Python35\Lib\site-packages'
  14. Copy 'opencv_ffmpeg310.dll' from the same folder to 'C:\Python35'
  15. Now go to the directory where Python is installed - "C:\Python35". Open python console by entering 'python'
  16. Once Python console is open, enter 'import cv2'. You should get no error.
  17. 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

  1. Make sure User Variable and System Variable path are correct (This part costed me a lot of time)

  2. Make sure no red lines are present while configuring CMake

  3. Make sure BUILD_opencv_python3 is ticked on

  4. Make sure 'cv2.cp35-win32.pyd' and all 17 DLLs are copied to the right place

amathur45
  • 31
  • 3