Skip to content

Instantly share code, notes, and snippets.

@okapies
Forked from pohmelie/build-opencv-for-pyenv.py
Last active September 5, 2021 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okapies/e9676ab44b6da3031731f43df8f3e23c to your computer and use it in GitHub Desktop.
Save okapies/e9676ab44b6da3031731f43df8f3e23c to your computer and use it in GitHub Desktop.
Build opencv for ubuntu 16.04 with pyenv
import getpass
import os
import pathlib
import sys
import sh
BASE_DIR = os.path.abspath(os.path.join(__file__, '..'))
OPENCV_VERSION = '4.5.2'
def build_opencv():
python_path = pathlib.Path(sh.pyenv.which('python').stdout.decode()).parent.parent
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
# make build directory
build_dir = os.path.join(BASE_DIR, "opencv-{}".format(OPENCV_VERSION), 'build')
pathlib.Path(build_dir).mkdir(parents=True, exist_ok=True)
os.chdir(build_dir)
sh.cmake(
"..",
"-DCMAKE_BUILD_TYPE=RELEASE",
"-DCMAKE_INSTALL_PREFIX={}/.local".format(pathlib.Path.home()),
"-DWITH_MKL=OFF",
"-DWITH_CUDA=OFF",
#"-DWITH_FFMPEG=OFF",
"-DINSTALL_C_EXAMPLES=OFF",
"-DBUILD_opencv_legacy=OFF",
"-DPYTHON_DEFAULT_EXECUTABLE={}/bin/python".format(python_path),
"-DBUILD_NEW_PYTHON_SUPPORT=ON",
"-DBUILD_opencv_python2=OFF",
"-DBUILD_opencv_python3=ON",
"-DOPENCV_EXTRA_MODULES_PATH={}/opencv_contrib-{}/modules".format(BASE_DIR, OPENCV_VERSION),
"-DPYTHON_EXECUTABLE={}/bin/python".format(python_path),
"-DPYTHON3_LIBRARY={}/lib/libpython{}.a".format(python_path, version),
"-DPYTHON3_INCLUDE_DIR={}/include/python{}".format(python_path, version),
"-DPYTHON3_PACKAGES_PATH={}/lib/python{}/site-packages/".format(python_path, version),
"-DPYTHON3_NUMPY_INCLUDE_DIRS={}/lib/python{}/site-packages/numpy/core/include".format(python_path, version),
"-DBUILD_EXAMPLES=OFF",
"-DBUILD_TESTS=OFF",
"-DBUILD_PERF_TESTS=OFF",
_out=sys.stdout,
)
if __name__ == "__main__":
build_opencv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment