Skip to content

Instantly share code, notes, and snippets.

@rafou
Last active February 6, 2018 11:48
Show Gist options
  • Save rafou/fd2cff31bb5899f6f75ced9ac43b281a to your computer and use it in GitHub Desktop.
Save rafou/fd2cff31bb5899f6f75ced9ac43b281a to your computer and use it in GitHub Desktop.

How to compile OpenCV 2 for Python 2.7 64-bit on Windows

Step 1 - Install Microsoft Visual C++ Compiler for Python 2.7

First of all, download and install Microsoft Visual C++ Compiler for Python 2.7 from the Microsoft download page


Step 2 - Install CMake

Install CMake with the binary installer for Windows x64 on the CMake download page..

During installation, select Add CMake to system PATH for all users.


Step 3 - Compile OpenCV

  • Download the last OpenCV 2.4 release for Windows on the OpenCV download page and extract it somewhere

  • Open the file /sources/modules/core/src/drawing.cpp

  • In this file, replace the 3 occurences of:

_pts.data()

by :

&_pts[0]
  • In this file also, replace :
axes.width = std::abs(axes.width), axes.height = std::abs(axes.height);

by:

axes.width = std::abs((int)axes.width), axes.height = std::abs((int)axes.height);
  • Open the "Visual C++ 2008 64-bit Command Prompt" application

  • Go inside the opencv/sources directoryand create a "build_vc9" folder

  • Go inside the build_vc9 folder and run:

cmake -G "NMake Makefiles" ..
  • Then just run:
nmake

There you go, you know have compiled OpenCV with VC9.


Step 5 - Add the right env variables

You know have to add the two following environment variables :

  • BOOST_ROOT
  • BOOST_LIBRARYDIR

Set them to whatever the compilation output on Step 4. For instance during my compilation, here is the final output :

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

C:\Users\Angus\Downloads\boost_1_64_0

The following directory should be added to linker library paths:

C:\Users\Angus\Downloads\boost_1_64_0\stage\lib"

So you will set the 2 vars :

set BOOST_ROOT=C:\Users\Angus\Downloads\boost_1_64_0
set BOOST_LIBRARYDIR=C:\Users\Angus\Downloads\boost_1_64_0\stage\lib

Note : these variables are set only for this prompt. If you want to set them permanently, go to the Advanced System Settings and set the two new vars.


Step 6 - Install virtualenv (Optional but recommanded)

  • Install virtualenv by running :
pip install virtualenv
  • Create a virtualenv by running :
virtualenv dlib-venv
  • Then activate it with :
dlib-venv\Scripts\activate

Step 6 (bis) - Install the wheel package (it you are not in a virtualenv)

  • Install the wheel package by running :
pip install wheel

Step 7 - Create the dlib wheel

  • If it's not already done, download the last Dlib archive on the Dlib home page and extract it somewhere
  • Go inside the dlib directory you just extracted and run :
python setup.py bdist_wheel

After it's finished, that's it, your wheel should have been created ! Go to the newly created dist directory and you should find the file dlib-X.Y.Z-cp27-cp27m-win_amd64.whl file which you can install where you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment