Skip to content

Instantly share code, notes, and snippets.

@pohmelie
Created April 26, 2017 14:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pohmelie/906a79f8ad665d0591186549e191ae9f to your computer and use it in GitHub Desktop.
Save pohmelie/906a79f8ad665d0591186549e191ae9f to your computer and use it in GitHub Desktop.
Build opencv for ubuntu 16.04 with pyenv
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()
@ZoomQuiet
Copy link

ERROR:

...

-- Checking for module 'libv4l1'
-- Found libv4l1, version 1.14.2
-- Checking for module 'libv4l2'
-- Found libv4l2, version 1.14.2
-- Looking for linux/videodev.h
-- Looking for linux/videodev.h - not found
-- Looking for linux/videodev2.h
-- Looking for linux/videodev2.h - found
-- Looking for sys/videoio.h
-- Looking for sys/videoio.h - not found
-- Checking for module 'libgphoto2'
-- Found libgphoto2, version 2.5.16
-- ICV: Downloading ippicv_linux_20141027.tgz...
-- Configuring incomplete, errors occurred!
See also "/opt/src/opencv-build/CMakeFiles/CMakeOutput.log".
See also "/opt/src/opencv-build/CMakeFiles/CMakeError.log".
Traceback (most recent call last):
File "build-opencv-for-pyenv.py", line 46, in
build_opencv()
File "build-opencv-for-pyenv.py", line 39, in build_opencv
_out=sys.stdout,
File "/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/sh.py", line 1427, in call
return RunningCommand(cmd, call_args, stdin, stdout, stderr)
File "/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/sh.py", line 774, in init
self.wait()
File "/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/sh.py", line 792, in wait
self.handle_command_exit_code(exit_code)
File "/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/sh.py", line 815, in handle_command_exit_code
raise exc
sh.ErrorReturnCode_1:

RAN: /usr/bin/cmake ../opencv -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/home/usee/.pyenv/versions/UC2715/usr/local -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=/home/usee/.pyenv/versions/UC2715/bin/python -DPYTHON3_LIBRARY=/home/usee/.pyenv/versions/UC2715/lib/libpython2.7m.so -DPYTHON3_PACKAGES_PATH=/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/ -DPYTHON3_NUMPY_INCLUDE_DIRS=/home/usee/.pyenv/versions/UC2715/lib/python2.7/site-packages/numpy/core/include -DPYTHON_INCLUDE_DIR=/home/usee/.pyenv/versions/UC2715/include/python2.7m

STDOUT:

STDERR:
CMake Deprecation Warning at CMakeLists.txt:69 (cmake_policy):
The OLD behavior for policy CMP0022 will be removed from a future version
of CMake.

The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term circumstances. Projects should be ported to the NEW
behavior and not rely on setting a policy to OLD.

CMake Deprecation Warning at CMakeLists.txt:74 (cmake_policy):
The OLD behavior for policy CMP0026 will be removed from a future version
of CMake.

The cmake-policies(7) manual explains that the OLD behaviors of all
policies are deprecated and that a policy should be set to OLD only under
specific short-term cir... (1721 more, please see e.stderr)

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