MacOS X の Python 2.5 で OpenCV 頑張ってみる

うし〜

MacPortsPython 2.5 がある。MacOS X に最初から入ってる Python は 2.3.5。せっかくだから新しいバージョンにしたい!という訳で MacPorts で python25 をインストールした。

debian:~ paraches$ sudo port install python25
--->  Fetching python25
--->  Attempting to fetch Python-2.5.1.tar.bz2 from http://www.python.org//ftp/python/2.5.1/
--->  Verifying checksum(s) for python25
--->  Extracting python25
--->  Applying patches to python25
--->  Configuring python25
--->  Building python25 with target all
--->  Staging python25 into destroot
--->  Installing python25 2.5.1_1+darwin_8
--->  Activating python25 2.5.1_1+darwin_8
--->  Cleaning python25
debian:~ paraches$ 

インストールは問題なく終了!良かった良かった。
それじゃぁ早速 OpenCV のサンプルを試してみよう!

debian:~/myTest/opencv-1.0.0/samples/python paraches$ python2.5 lkdemo.py 
OpenCV Python version of lkdemo
Fatal Python error: Interpreter not initialized (version mismatch?)
Abort trap
debian:~/myTest/opencv-1.0.0/samples/python paraches$ 

あらら…エラー!
何やら python のバージョンが合わないって。python2.3 のライブラリ?を呼び出したりしちゃっているのかな? OpenCV を make したときは Python 2.3.5 しかインストールされてなかったから、Python 2.3 用に作られちゃってるんだろう…。んじゃ、OpenCV を make uninstall してもう一度 configure から作り直してみる…

debian:~/myTest/opencv-1.0.0 paraches$ ./configure --with-python CPPFLAGS="-I/opt/local/include" LDFLAGS="-L/opt/local/lib"
checking build system type... i386-apple-darwin8.10.1
checking host system type... i386-apple-darwin8.10.1
<省略>
checking for python... /usr/bin/python
checking for python version... 2.3
checking for python platform... darwin
checking for python script directory... ${prefix}/lib/python2.3/site-packages
checking for python extension module directory... ${exec_prefix}/lib/python2.3/site-packages
configure: PYTHON_CSPEC= -I/System/Library/Frameworks/Python.framework/Versions/2.3/include/python2.3
configure: PYTHON_LSPEC=-Wl,-F. -Wl,-F. -F/System/Library/Frameworks -L/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config -lpython2.3 -u __dummy -u _PyMac_Error -framework System Python.framework/Versions/2.3/Python -framework CoreServices -framework Foundation
checking Python.h usability... yes
checking Python.h presence... yes
checking for Python.h... yes
<省略>

けど、やっぱり Python 2.3 用になってる。たぶん OpenCV 作る時に /usr/bin/python に合わせて?作ってるんだろうな〜と考えて /usr/bin/python を /usr/local/bin/python2.5 へのリンクにした(元は /usr/bin/python2.3 へのリンクだった)。

debian:~ paraches$ ls -al /usr/bin | grep python
lrwxr-xr-x     1 root  wheel            9 Jul  9 20:17 python -> python2.3
lrwxr-xr-x     1 root  wheel           72 Jul  9 20:17 python2.3 -> ../../System/Library/Frameworks/Python.framework/Versions/2.3/bin/python
lrwxr-xr-x     1 root  wheel           10 Jul  9 20:17 pythonw -> pythonw2.3
-rwxr-xr-x     1 root  wheel        34216 Dec 26  2005 pythonw2.3
debian:~ paraches$ sudo rm /usr/bin/python
debian:~ paraches$ sudo ln -s /opt/local/bin/python2.5 /usr/bin/python
debian:~ paraches$ python
Python 2.5.1 (r251:54863, Jul  9 2007, 21:53:21) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
debian:~ paraches$ 

これで /usr/bin/python は python2.5 になった。まずは ./configure してみる…

debian:~/myTest/opencv-1.0.0 paraches$ ./configure --with-python CPPFLAGS="-I/opt/local/include" LDFLAGS="-L/opt/local/lib"
checking build system type... i386-apple-darwin8.10.1
checking host system type... i386-apple-darwin8.10.1
<省略>
checking for python... /usr/bin/python
checking for python version... 2.5
checking for python platform... darwin
checking for python script directory... ${prefix}/lib/python2.5/site-packages
checking for python extension module directory... ${exec_prefix}/lib/python2.5/site-packages
configure: PYTHON_CSPEC= -I/opt/local/include/python2.5
configure: PYTHON_LSPEC=-L/opt/local/lib -L/opt/local/lib/python2.5/config -ldl -lpython2.5 -u _PyMac_Error
checking Python.h usability... yes
checking Python.h presence... yes
checking for Python.h... yes
<省略>

ちゃんと python2.5 でできてるみたい。それじゃ OpenCV を make だ!

debian:~/myTest/opencv-1.0.0 paraches$ make
make  all-recursive
<省略>
/usr/bin/ld: Undefined symbols:
_environ
collect2: ld returned 1 exit status
make[4]: *** [_cv.la] Error 1
make[3]: *** [all-recursive] Error 1
make[2]: *** [all-recursive] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
debian:~/myTest/opencv-1.0.0 paraches$ 

今度はエラー! _environ が定義されてない??? リンクで失敗してるってこと? もうわからない…

ちょっとググってみた。何やら MacPorts の Python25 は大変みたいだ。とりあえず port の詳細はコレ。で、それについてこんな話も。なるほどね〜大切なものが抜けた状態でビルドされているのね。ffmpeg をバリアント無しでビルドして全然動画フォーマットの変換ができないような状態ね。んじゃ、素直に MacPorts で入れた python25 は削除!

debian:~/myTest paraches$ sudo port uninstall python25
--->  Deactivating python25 2.5.1_1+darwin_8
--->  Uninstalling python25 2.5.1_1+darwin_8
debian:~/myTest paraches$     

で、次は PyJUG から Python 2.5 の dmg をダウンロードしてインストール。インストール自体はパッケージをダブルクリックでインストーラが起動するので簡単。特に設定すること等はない。
で、/usr/bin/python を /usr/local/bin/python2.5 へのリンクに変更して

debian:~ paraches$ sudo rm /usr/bin/python
debian:~ paraches$ sudo ln -s /usr/local/bin/python2.5 /usr/bin/python

まずは ./configure を…

debian:~/myTest/opencv-1.0.0 paraches$ ./configure --with-python CPPFLAGS="-I/opt/local/include" LDFLAGS="-L/opt/local/lib"
checking build system type... i386-apple-darwin8.10.1
checking host system type... i386-apple-darwin8.10.1
checking target system type... i386-apple-darwin8.10.1
<省略>
configure: Checking for necessary tools to build python wrappers
checking for python... /usr/bin/python
checking for python version... 2.5
checking for python platform... darwin
checking for python script directory... ${prefix}/lib/python2.5/site-packages
checking for python extension module directory... ${exec_prefix}/lib/python2.5/site-packages
configure: PYTHON_CSPEC= -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5
configure: PYTHON_LSPEC=-arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g -F/Library/Frameworks -L/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/config -lpython2.5 -u _PyMac_Error Python.framework/Versions/2.5/Python
checking Python.h usability... yes
checking Python.h presence... yes
checking for Python.h... yes
<省略>

ちゃんと python2.5って出てくるね。ここまではさっきも大丈夫だった。
次は OpenCV を make。

debian:~/myTest/opencv-1.0.0 paraches$ make
make  all-recursive
Making all in cxcore
<省略>
make[2]: Nothing to be done for `all'.
Making all in docs
make[2]: Nothing to be done for `all'.
make[2]: Nothing to be done for `all-am'.
debian:~/myTest/opencv-1.0.0 paraches$ 

ワーニングの数が半端じゃない気もするけど、make は終了。ついにここまで来た〜!
次は make install

debian:~/myTest/opencv-1.0.0 paraches$ sudo make install
Making install in cxcore
Making install in src
<省略>
make[2]: Nothing to be done for `install-exec-am'.
test -z "/usr/local/include/opencv" || /bin/sh ./autotools/mkinstalldirs "/usr/local/include/opencv"
test -z "/usr/local/lib/pkgconfig" || /bin/sh ./autotools/mkinstalldirs "/usr/local/lib/pkgconfig"
 /usr/bin/install -c -m 644 'opencv.pc' '/usr/local/lib/pkgconfig/opencv.pc'
debian:~/myTest/opencv-1.0.0 paraches$ 

これも問題なく終了!
出来上がったもの?を見てみよう。

debian:~/myTest/opencv-1.0.0 paraches$ ls /usr/local/lib
libcv.1.0.0.dylib       libcvhaartraining.a     libhighgui.la
libcv.1.dylib           libcxcore.1.0.0.dylib   libml.1.0.0.dylib
libcv.dylib             libcxcore.1.dylib       libml.1.dylib
libcv.la                libcxcore.dylib         libml.dylib
libcvaux.1.0.0.dylib    libcxcore.la            libml.la
libcvaux.1.dylib        libhighgui.1.0.0.dylib  pkgconfig
libcvaux.dylib          libhighgui.1.dylib      python2.3
libcvaux.la             libhighgui.dylib        python2.5
debian:~/myTest/opencv-1.0.0 paraches$ ls /usr/local/lib/python2.5
site-packages
debian:~/myTest/opencv-1.0.0 paraches$ 

PYTHONPATH に設定しなきゃいけないのは /usr/local/lib/python2.5/site-packages かな。
PYTHONPATH は .bashrc で設定。

PYTHONPATH=/usr/local/lib/python2.5/site-packages/;export PYTHONPATH

さて、実行してみると…

debian:~/myTest/opencv-1.0.0/samples/python paraches$ python lkdemo.py 
OpenCV Python version of lkdemo
Hot keys: 
        ESC - quit the program
        r - auto-initialize tracking
        c - delete all the points
        n - switch the "night" mode on/off
To add/remove a feature point click it

Failed to tranform process type: -50
debian:~/myTest/opencv-1.0.0/samples/python paraches$ 


やった! ついに Python 2.5 で OpenCV が使えるようになった!
ん? Dock に Python のアイコンが表示されてる。ロケットに Python 2.5 のインストーラに描いてあった蛇?かな。

けど、なんかエラーメッセージがターミナルに表示される。

Failed to tranform process type: -50

あれやこれややってみたんだけど、どうも highgui.cvNamedWindow() でこのメッセージが表示されているみたいだ。
で、そこから _highgui.cpp まではみてみたけどその先の SWIG が絡んでくるともう解らない。SWIGってラッパー? 包んでる先の C のコード?ってどこ??? う〜ん…