Skip to content

Instructions: Custom builds on Windows with Visual Studio 2019 or newer! #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Arcitec opened this issue Oct 24, 2019 · 1 comment
Closed

Comments

@Arcitec
Copy link

Arcitec commented Oct 24, 2019

This was inspired by needing a brand new feature (opencv/opencv#15739) even though OpenCV only officially releases new versions ~4 times per year. I don't have time to wait for 3 months, so I needed to build a .whl file to distribute to users immediately.

The build process was pretty easy, but complicated at the same time. So this documents the entire process to help others!

Requirements

  1. Python 3 64-bit. Go to https://www.python.org/downloads/windows/ and under the correct heading (currently it's Python 3.7.5), get the Windows x86-64 executable installer. You do NOT want the default 32-bit version! And during the install you MUST choose "Add to PATH".

  2. Install the free Visual Studio 2019 Community Edition, and enable the C++ language package during install.

  3. Install cmake for Windows. Go to https://cmake.org/download/ and get the win64-x64 version.

  4. Install git for Windows. Go to https://git-scm.com/downloads and it should auto-select the 64-bit version. During install you must add Git to Path, and install the git-bash tool (which is a Cygwin linux-like terminal for Windows).

  5. Add cmake and Visual Studio's compiler to your system PATH. Press Windows + i to open Settings, then search for environment and choose "edit the system environment variables". Click on "Environment Variables" in the window that pops up. Then double-click on PATH. Add the correct folders. I had to add these two: A) C:\Program Files\CMake\bin and B) C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\amd64. Without doing this, cmake or the compiler will not be found!

Preparing to Build!

  1. Clone the OpenCV-Python repository and all submodules. This is the repo used for making the semi-official pip (pypi.org) packages for opencv, so you can trust that it will do the right thing.
git clone --recurse-submodules https://github.com/skvark/opencv-python.git
  1. Put your custom patch in the opencv-python/patches/ folder. In my example, I want https://github.com/opencv/opencv/pull/15739.patch (from Enable Eltwise layer with different numbers of inputs channels opencv#15739, which adds support for a great new neural network). To do this, it's as easy as starting "Git Bash" and navigating to the opencv-python/patches/ and then typing the following command.
curl -O "https://patch-diff.githubusercontent.com/raw/opencv/opencv/pull/15739.patch"
  1. Edit setup.py in a text editor and search for "Visual Studio". It will be set to an ancient version for compatibility with old Windows versions, but I don't care about that. Edit the line as follows to make it use Visual Studio 2019 (comment out the old line as seen below, and add two new lines):
        #"-G", "Visual Studio 14" + (" Win64" if x64 else '')
        "-G", "Visual Studio 16 2019",
        "-A", "x64" if x64 else "Win32"
  1. Now it's time to add the patch command. Look for the line that says if 'CMAKE_ARGS' in os.environ:, and ABOVE THAT LINE, insert the following.
subprocess.check_call("patch -p1 -d opencv < patches/15739.patch", shell=True)

Building!

  1. Now we're going to run the remaining build process as described at https://github.com/skvark/opencv-python#build-process. You must do this step in Git Bash (so that you have support for passing in the ENABLE_CONTRIB variable as seen below), and type the following command to build the Python package.
ENABLE_CONTRIB=1 python setup.py bdist_wheel
  1. (Optional) If the build above fails and you need to re-build (after fixing whatever was wrong), you must first "clean" the source dir again since the patch modifies the source files and would fail to apply itself the next time. If so, just enter the opencv-python/opencv folder and type the following.
git reset --hard
  1. On Mac/Linux there seems to be some extra step to fix the package before using it. There's no such step on Windows.

  2. The built package now exists as opencv-python/_skbuild/win-amd64-3.7/setuptools/lib.win-amd64-3.7 (if you want to look at what is inside the wheel), and the wheel file is in opencv-python/dist.

After the Build!

  1. The instructions on this repo says that step 6 is "Rearrange OpenCV's build result, add our custom files and generate wheel", which makes no sense (rearrange what? add what custom files?). Either way, the folder contents are already perfect (they completely match what the pypi package installs). So now we'll simply use the finished wheel file, which is ready for easy distribution...

  2. Navigate to opencv-python/dist, and forcibly install the wheel (so that it overwrites any existing official installation), by typing the following command. The exact name of the wheel file will vary based on version.

python -m pip install --upgrade --force-reinstall opencv_contrib_python-4.1.1.26-cp37-cp37m-win_amd64.whl
  1. Distribute the .whl file to anyone who needs your custom-built OpenCV, until the pypi package catches up and you can replace it with the "official" version again.

Celebrate.

https://www.youtube.com/watch?v=maAFcEU6atk

@Scafyst
Copy link

Scafyst commented Dec 28, 2020

I´m following your steps and when i use the command:

ENABLE_CONTRIB=1 python setup.py bdist_wheel

I get the following message:
scikit-build could not get a working generator for your system. Aborting build.

Building windows wheels for Python 3.8 requires Microsoft Visual Studio 2017.
Get it with "Visual Studio 2017":

I´m installing the Microsoft Visual Studio 2017, and the problem is still there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants