追記(2015/07/24): HomeBrewにフォーミュラhomebrew/science/opencv3が追加されて、特に苦労することなくインストールできるようになっていました。
OpenCV3 ベータ版が昨年11月に公開されました。
Python3でOpenCV3のプログラムを書いてみたいと思い、インストールしようとしたところ、色々、障害があり、それらを解決せずに逃げてなんとかインストールできました。
以下、備忘録です。
1. pyenvを諦める。
具体的には.bash_profileでpyenvのパスを通すスクリプトをコメントアウト。
2. brew install python3
3. pip3 install numpy
brew install numpyが失敗するのでpip3でインストールしておく。
4. brew tap homebrew/science
5. brew edit opencv
opencvのFormulaを変更する。
class Opencv < Formula
homepage "http://opencv.org/"
head "https://github.com/Itseez/opencv.git"
stable do
url "https://github.com/Itseez/opencv/archive/2.4.10.1.tar.gz"
sha256 "1be191790a0e279c085ddce62f44b63197f2801e3eb66b5dcb5e19c52a8c7639"
patch :DATA
end
bottle do
root_url "https://downloads.sf.net/project/machomebrew/Bottles/science"
sha1 "ccc506ef6cf339048d21c4ef3d8995c76706c9d9" => :yosemite
sha1 "fc69f870b36504f271dfe42deb5cb27c49bf21cd" => :mavericks
sha1 "1726a921ced13ebe7f2df0f18f64997091070f71" => :mountain_lion
end
devel do
url "https://github.com/Itseez/opencv/archive/3.0.0-beta.tar.gz"
sha1 "560895197d1a61ed88fab9ec791328c4c57c0179"
version "3.0.0-beta"
end
option "32-bit"
option "with-java", "Build with Java support"
option "with-qt", "Build the Qt4 backend to HighGUI"
option "with-tbb", "Enable parallel code in OpenCV using Intel TBB"
option "without-tests", "Build without accuracy & performance tests"
option "without-opencl", "Disable GPU code in OpenCV using OpenCL"
option "with-cuda", "Build with CUDA support"
option "with-quicktime", "Use QuickTime for Video I/O insted of QTKit"
option "with-opengl", "Build with OpenGL support"
option "without-brewed-numpy", "Build without Homebrew-packaged NumPy"
option :cxx11
depends_on :ant if build.with? "java"
depends_on "cmake" => :build
depends_on "eigen" => :recommended
depends_on "gstreamer" => :optional
depends_on "gst-plugins-good" if build.with? "gstreamer"
depends_on "jasper" => :optional
depends_on :java => :optional
depends_on "jpeg"
depends_on "libpng"
depends_on "libtiff"
depends_on "libdc1394" => :optional
depends_on "openexr" => :recommended
depends_on "openni" => :optional
depends_on "pkg-config" => :build
depends_on "qt" => :optional
depends_on "tbb" => :optional
depends_on :python => :recommended
depends_on "homebrew/python/numpy" if build.with? "brewed-numpy"
depends_on "ffmpeg" => :optional
def arg_switch(opt)
(build.with? opt) ? "ON" : "OFF"
end
def install
ENV.cxx11 if build.cxx11?
jpeg = Formula["jpeg"]
dylib = OS.mac? ? "dylib" : "so"
py_ver = build.stable? ? "" : "3"
args = std_cmake_args + %W[
-DCMAKE_OSX_DEPLOYMENT_TARGET=
-DBUILD_ZLIB=OFF
-DBUILD_TIFF=OFF
-DBUILD_PNG=OFF
-DBUILD_OPENEXR=OFF
-DBUILD_JASPER=OFF
-DBUILD_JPEG=OFF
-DJPEG_INCLUDE_DIR=#{jpeg.opt_include}
-DJPEG_LIBRARY=#{jpeg.opt_lib}/libjpeg.#{dylib}
]
args << "-DBUILD_TESTS=OFF" << "-DBUILD_PERF_TESTS=OFF" if build.without? "tests"
args << "-DBUILD_opencv_python#{py_ver}=" + arg_switch("python")
args << "-DBUILD_opencv_java=" + arg_switch("java")
args << "-DWITH_OPENEXR=" + arg_switch("openexr")
args << "-DWITH_EIGEN=" + arg_switch("eigen")
args << "-DWITH_TBB=" + arg_switch("tbb")
args << "-DWITH_FFMPEG=" + arg_switch("ffmpeg")
args << "-DWITH_QUICKTIME=" + arg_switch("quicktime")
args << "-DWITH_1394=" + arg_switch("libdc1394")
args << "-DWITH_OPENGL=" + arg_switch("opengl")
args << "-DWITH_JASPER=" + arg_switch("jasper")
args << "-DWITH_QT=" + arg_switch("qt")
args << "-DWITH_GSTREAMER=" + arg_switch("gstreamer")
if build.with? "python"
py_prefix = `/usr/local/bin/python#{py_ver}-config --prefix`.chomp
py_lib = OS.linux? ? `/usr/local/bin/python#{py_ver}-config --configdir`.chomp : "#{py_prefix}/lib"
args << "-DPYTHON#{py_ver}_LIBRARY=#{py_lib}/libpython3.4m.#{dylib}"
args << "-DPYTHON#{py_ver}_INCLUDE_DIR=#{py_prefix}/include/python3.4m"
end
if build.with? "cuda"
ENV["CUDA_NVCC_FLAGS"] = "-Xcompiler -stdlib=libstdc++; -Xlinker -stdlib=libstdc++"
inreplace "cmake/FindCUDA.cmake",
"list(APPEND CUDA_LIBRARIES -Wl,-rpath \"-Wl,${_cuda_path_to_cudart}\")",
"#list(APPEND CUDA"
args << "-DWITH_CUDA=ON"
args << "-DCMAKE_CXX_FLAGS=-stdlib=libstdc++"
else
args << "-DWITH_CUDA=OFF"
end
args << "-DWITH_OPENCL=OFF" if build.without?("opencl") || MacOS.version < :lion
if build.with? "openni"
args << "-DWITH_OPENNI=ON"
inreplace "cmake/OpenCVFindOpenNI.cmake" do |s|
s.gsub! "/usr/include/ni", "#{Formula["openni"].opt_include}/ni"
s.gsub! "/usr/lib", "#{Formula["openni"].opt_lib}"
end
end
if build.include? "32-bit"
args << "-DCMAKE_OSX_ARCHITECTURES=i386"
args << "-DOPENCV_EXTRA_C_FLAGS='-arch i386 -m32'"
args << "-DOPENCV_EXTRA_CXX_FLAGS='-arch i386 -m32'"
end
if ENV.compiler == :clang && !build.bottle?
args << "-DENABLE_SSSE3=ON" if Hardware::CPU.ssse3?
args << "-DENABLE_SSE41=ON" if Hardware::CPU.sse4?
args << "-DENABLE_SSE42=ON" if Hardware::CPU.sse4_2?
args << "-DENABLE_AVX=ON" if Hardware::CPU.avx?
end
mkdir "macbuild" do
system "cmake", "..", *args
system "make"
system "make", "install"
end
end
test do
Language::Python.each_python(build) do |python, _version|
system python, "-c", "import cv2"
end
end
end
__END__
- 変更場所は、build.stableじゃない時のPythonのバージョンを3に
- python-configの代わりに/usr/local/bin/python3-configを使用
- PYTHON3_LIBRARYとPYTHON3_INCLUDE_DIRのパスをそれなりに合わせる
6. brew install opencv --devel --without-brewed-numpy
かなりやっつけ仕事ですが、これで、/usr/local/bin/python3からimport cv2ができるようになりました。