LoginSignup
12
12

More than 5 years have passed since last update.

UbuntuでOpenCV3&Python3の環境構築

Posted at

Ubuntuで環境構築

標準のパッケージではPython2用しかないので、自前で用意します。
cmakeのPython3.4の検出処理に問題があるようで、なかなかうまくいかなかったので、備忘録的にメモしておきます。

対象となる環境

  • cmakeでpython3のライブラリのパスを取得できなくて、ターゲットのopencv_python3がunavailableになる
  • (たぶんubuntu14.04ならpython3を検出できないと思う)

windows環境では比較的簡単にできたのに、Ubuntuでこんなに苦労するとは思いもしなかった。

下準備

必要なパッケージをインストール

参考にしたサイトInstaller Opencv 3 sur Ubuntu 14.04 Trusty (英語じゃないけどそれなりに分かります)

sudo apt install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev \
libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev \
libv4l-dev python3-dev python3-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev \
libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils python3.4-dev

pyconfig.hをコピー

必要かどうか分からない
bash
sudo cp /usr/include/python3.4m/pyconfig.h /usr/include/python3.4m/pyconfig.h_old
sudo cp /usr/include/i386-linux-gnu/python3.4m/pyconfig.h /usr/include/python3.4m/

cmakeのファイルを編集

cmakeがpythonのバージョンで3.4があることを知らないので、教えてあげます。
FindPython{Interp,Libs}: Search for Python 3.4.
編集対象は、
* Modules/FindPythonInterp.cmake
* Modules/FindPythonLibs.cmake
の二つです。

ソースコードを取得

git clone https://github.com/Itseez/opencv.git

cmake

cmake-guiでいろいろオプションを設定します
私の環境では、ファイルのハッシュが違うとかで、エラーになるので、WITH_IPP=OFFにする必要がありました。

Pythonのライブラリを検出させる

  • pythonが見つからない状態(cmakeのログ)
Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.6", minimum required is "2.7") 
Could NOT find PythonLibs (missing:  PYTHON_INCLUDE_DIRS) (found suitable exact version "2.7.6")
Found PythonInterp: /usr/bin/python3.4 (found suitable version "3.4", minimum required is "3.4") 
Could NOT find PythonLibs (missing:  PYTHON_LIBRARIES) (Required is exact version "3.4.0")
  • Python、Python2、Python3のパスのエントリーを選択して、Remove Entoryをクリックで削除
  • 再度Configureを押す
  • pythonが見つかる状態(cmakeのログ)
Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.6", minimum required is "2.7") 
Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable exact version "2.7.6") 
Found PythonInterp: /usr/bin/python3.4 (found suitable version "3.4", minimum required is "3.4") 
Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (found suitable exact version "3.4.0")
  • そのままGenerateでMakefileを作成 (Configureを押すとPython、Python2、Python3のパスのエントリーが復活するので、この状態で再びConfigureを押してしまうと見つからない状態に逆戻りしてしまう)

make

Makefileさえできてしまえば、後は問題ないはず

make -j4
sudo make install
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12