Install OpenCV into a Python Virtual Environment

Robert Taylor April 19, 2019

Python and OpenCV

I’ve never found installing OpenCV to be an enjoyable, streamlined process. It’s always been a bit of a pain. Why? Because there are dozens of system dependencies, dozens of build options, and every gorram computer vision package that I’ve ever tinkered with seems to require a different OpenCV version. End result: An OS with about 5 conflicting versions of OpenCV and many, many hours wasted.

Recently, I took some time to learn more about the OpenCV build process because I wanted to be able to install specific versions of OpenCV into isolated Python virtual environments. Sadly, the online documentation is a little sparse when it comes to such tasks. It required a fair amount of digging and reading source files to understand how to accomplish what I wanted (CMake is not my forte).

It turns out that installing OpenCV to a Python virtual environment is pretty easy assuming you know what you’re doing. To save myself time in the future, I wrote a shell script to handle the details. It’s possible that others might find it useful, so I saved it to a public repo: opencv_venv.

Detailed, up-to-date instructions can be found in the project’s readme, but here’s the gist:

Clone the project:

git clone https://rtaylor@bitbucket.org/rtaylor/opencv_venv.git
cd opencv_venv

Checkout the branch you want and update the OpenCV submodules:

git checkout ubuntu17.10-py3-cv3.4
git submodule update --init --recursive

Run the configuration script, supplying it with the path to your Python virtual environment:

./configure.sh ~/path/to/venv/

The script will give you some options, then start to configure your OpenCV build. Once it’s finished, then you can build and install OpenCV to your virtual environment:

cd build
make -j4
make install

Again: More detailed instructions can found in the project repo. I hope it is useful to others. Enjoy!


GET IN TOUCH

Have a project in mind?

Reach out directly to hello@humaticlabs.com or use the contact form.

HUMATIC LABS LLC

All rights reserved