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
  • Select an option

  • Save okapies/e9676ab44b6da3031731f43df8f3e23c to your computer and use it in GitHub Desktop.

Select an option

Save okapies/e9676ab44b6da3031731f43df8f3e23c to your computer and use it in GitHub Desktop.

Revisions

  1. okapies revised this gist May 13, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions build-opencv-for-pyenv.py
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ def build_opencv():
    "..",
    "-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",
  2. okapies revised this gist May 13, 2021. 1 changed file with 22 additions and 20 deletions.
    42 changes: 22 additions & 20 deletions build-opencv-for-pyenv.py
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,47 @@
    import getpass
    import os
    import pathlib
    import sys

    import sh


    def clone_if_not_exists(name, url, **kwargs):
    if not pathlib.Path(name).exists():
    print("Cloning", url, "...")
    sh.git.clone(url, name, depth=1, **kwargs)
    BASE_DIR = os.path.abspath(os.path.join(__file__, '..'))
    OPENCV_VERSION = '4.5.2'


    def build_opencv():
    sh.pip.install("numpy")
    clone_if_not_exists("opencv", "https://github.com/PolarNick239/opencv.git", branch="stable_3.0.0")
    clone_if_not_exists("opencv_contrib", "https://github.com/PolarNick239/opencv_contrib.git", branch="stable_3.0.0")
    sh.rm("-rf", "opencv-build")
    sh.mkdir("opencv-build")
    sh.cd("opencv-build")
    python_path = pathlib.Path(sh.pyenv.which("python").stdout.decode()).parent.parent
    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(
    "../opencv",
    "..",
    "-DCMAKE_BUILD_TYPE=RELEASE",
    "-DCMAKE_INSTALL_PREFIX={}/usr/local".format(python_path),
    "-DCMAKE_INSTALL_PREFIX={}/.local".format(pathlib.Path.home()),
    "-DWITH_CUDA=OFF",
    "-DWITH_FFMPEG=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",
    "-DBUILD_EXAMPLES=ON",
    "-DOPENCV_EXTRA_MODULES_PATH={}/opencv_contrib-{}/modules".format(BASE_DIR, OPENCV_VERSION),
    "-DPYTHON_EXECUTABLE={}/bin/python".format(python_path),
    "-DPYTHON3_LIBRARY={}/lib/libpython{}m.so".format(python_path, version),
    "-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),
    "-DPYTHON_INCLUDE_DIR={}/include/python{}m".format(python_path, version),
    "-DBUILD_EXAMPLES=OFF",
    "-DBUILD_TESTS=OFF",
    "-DBUILD_PERF_TESTS=OFF",
    _out=sys.stdout,
    )
    sh.make("-j4", _out=sys.stdout)
    sh.make.install(_out=sys.stdout)


    if __name__ == "__main__":
  3. @pohmelie pohmelie created this gist Apr 26, 2017.
    46 changes: 46 additions & 0 deletions build-opencv-for-pyenv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    import pathlib
    import sys

    import sh


    def clone_if_not_exists(name, url, **kwargs):
    if not pathlib.Path(name).exists():
    print("Cloning", url, "...")
    sh.git.clone(url, name, depth=1, **kwargs)


    def build_opencv():
    sh.pip.install("numpy")
    clone_if_not_exists("opencv", "https://github.com/PolarNick239/opencv.git", branch="stable_3.0.0")
    clone_if_not_exists("opencv_contrib", "https://github.com/PolarNick239/opencv_contrib.git", branch="stable_3.0.0")
    sh.rm("-rf", "opencv-build")
    sh.mkdir("opencv-build")
    sh.cd("opencv-build")
    python_path = pathlib.Path(sh.pyenv.which("python").stdout.decode()).parent.parent
    version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
    sh.cmake(
    "../opencv",
    "-DCMAKE_BUILD_TYPE=RELEASE",
    "-DCMAKE_INSTALL_PREFIX={}/usr/local".format(python_path),
    "-DWITH_CUDA=OFF",
    "-DWITH_FFMPEG=OFF",
    "-DINSTALL_C_EXAMPLES=OFF",
    "-DBUILD_opencv_legacy=OFF",
    "-DBUILD_NEW_PYTHON_SUPPORT=ON",
    "-DBUILD_opencv_python3=ON",
    "-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules",
    "-DBUILD_EXAMPLES=ON",
    "-DPYTHON_EXECUTABLE={}/bin/python".format(python_path),
    "-DPYTHON3_LIBRARY={}/lib/libpython{}m.so".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),
    "-DPYTHON_INCLUDE_DIR={}/include/python{}m".format(python_path, version),
    _out=sys.stdout,
    )
    sh.make("-j4", _out=sys.stdout)
    sh.make.install(_out=sys.stdout)


    if __name__ == "__main__":
    build_opencv()