読者です 読者をやめる 読者になる 読者になる

debianでWebカメラを使って撮影をしたかった

ThinkPad X61Webカメラを繋げてみたのでRubyで使える様にチャレンジした内容のメモです。
環境はdebianの7.6で実行して失敗しています。

ググってもビルドの記事ばっかりだったので、apt-getでのインストールにチャレンジしましたが、wheezy-backportsではOpenCV2.3のパッケージが入り、ruby-opencvのgemに必要なopencv_photoが入っていないためruby-opencvはインストールできませんでした。

取りあえず作業メモとして残しておきます。
OpenCVをインストールするためにパッケージを探します。

# apt-cache search opencv
libcv-dev - Translation package for libcv-dev
libcv2.3 - computer vision library - libcv* translation package
libcvaux-dev - Translation package for libcvaux-dev
libcvaux2.3 - computer vision library - libcvaux translation package
libhighgui-dev - Translation package for libhighgui-dev
libhighgui2.3 - computer vision library - libhighgui translation package
libopencv-calib3d-dev - development files for libopencv-calib3d
libopencv-calib3d2.3 - computer vision Camera Calibration library
libopencv-contrib-dev - development files for libopencv-contrib
libopencv-contrib2.3 - computer vision contrib library
libopencv-core-dev - development files for libopencv-core
libopencv-core2.3 - computer vision core library
libopencv-dev - development files for opencv
libopencv-features2d-dev - development files for libopencv-features2d
libopencv-features2d2.3 - computer vision Feature Detection and Descriptor Extraction library
libopencv-flann-dev - development files for libopencv-flann
libopencv-flann2.3 - computer vision Clustering and Search in Multi-Dimensional spaces library
libopencv-gpu-dev - development files for libopencv-gpu
libopencv-gpu2.3 - computer vision GPU Processing library
libopencv-highgui-dev - development files for libopencv-highgui
libopencv-highgui2.3 - computer vision High-level GUI and Media I/O library
libopencv-imgproc-dev - development files for libopencv-imgproc
libopencv-imgproc2.3 - computer vision Image Processing library
libopencv-legacy-dev - development files for libopencv-legacy
libopencv-legacy2.3 - computer vision legacy library
libopencv-ml-dev - development files for libopencv-ml
libopencv-ml2.3 - computer vision Machine Learning library
libopencv-objdetect-dev - development files for libopencv-objdetect
libopencv-objdetect2.3 - computer vision Object Detection library
libopencv-video-dev - development files for libopencv-video
libopencv-video2.3 - computer vision Video analysis library
opencv-doc - OpenCV documentation and examples
python-opencv - Python bindings for the computer vision library

libopencv-core2.3っぽいかなと思ったので。

# apt-get install libopencv-core2.3

続いてRuby用のラッパーをインストールします。

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
        from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from extconf.rb:12:in `
' Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection. Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

mkmfがないと言われたので探します。

# apt-cache search mkmf
ruby-dev - Header files for compiling extension modules for Ruby (default version)
ruby-mkrf - Ruby library generating Rakefiles to build C extensions to Ruby
ruby1.8-dev - Header files for compiling extension modules for the Ruby 1.8
ruby1.9.1-dev - Header files for compiling extension modules for the Ruby 1.9.1

ruby-devに入ってるっぽい雰囲気ですね。

# apt-get install ruby-dev

再チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... no
checking for main() in -lopencv_calib3d... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
extconf.rb:58:in `block in <main>': opencv_calib3d not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

search opencvでlibopencv-calib3d2.3もあったので入れてみる。

# apt-get install libopencv-calib3d2.3

再チャレンジ。

# gem install ruby-opencv

同じエラーでダメでした。うーん。ディベロップパッケージも入れないとだめなのかなー。

# apt-get install libopencv-calib3d-dev

再々チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... yes
checking for main() in -lopencv_calib3d... yes
checking for main() in -lopencv_contrib... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
        --with-opencv_contriblib
        --without-opencv_contriblib
extconf.rb:58:in `block in <main>': opencv_contrib not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

エラーがちょっと変わった。lopencv_contribがないらしい。
これも似た名前のパッケージがあったので突っ込んでみる。

# apt-get install libopencv-contrib2.3

再々々チャレンジ

# gem install ruby-opencv

同じエラーでだめ。libopencv-contrib-devも入れないとだめか。

# apt-get install libopencv-contrib-dev

再々々々チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... yes
checking for main() in -lopencv_calib3d... yes
checking for main() in -lopencv_contrib... yes
checking for main() in -lopencv_core... yes
checking for main() in -lopencv_features2d... yes
checking for main() in -lopencv_flann... yes
checking for main() in -lopencv_highgui... yes
checking for main() in -lopencv_imgproc... yes
checking for main() in -lopencv_legacy... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
        --with-opencv_contriblib
        --without-opencv_contriblib
        --with-opencv_corelib
        --without-opencv_corelib
        --with-opencv_features2dlib
        --without-opencv_features2dlib
        --with-opencv_flannlib
        --without-opencv_flannlib
        --with-opencv_highguilib
        --without-opencv_highguilib
        --with-opencv_imgproclib
        --without-opencv_imgproclib
        --with-opencv_legacylib
        --without-opencv_legacylib
extconf.rb:58:in `block in <main>': opencv_legacy not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

lopencv_legacyがないとな。
libopencv-legacy2.3を入れよう、今までのパターンからするとlibopencv-legacy-devも必要そうだから入れておく。

# apt-get install libopencv-legacy2.3 libopencv-legacy-dev

再々々々々チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... yes
checking for main() in -lopencv_calib3d... yes
checking for main() in -lopencv_contrib... yes
checking for main() in -lopencv_core... yes
checking for main() in -lopencv_features2d... yes
checking for main() in -lopencv_flann... yes
checking for main() in -lopencv_highgui... yes
checking for main() in -lopencv_imgproc... yes
checking for main() in -lopencv_legacy... yes
checking for main() in -lopencv_ml... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
        --with-opencv_contriblib
        --without-opencv_contriblib
        --with-opencv_corelib
        --without-opencv_corelib
        --with-opencv_features2dlib
        --without-opencv_features2dlib
        --with-opencv_flannlib
        --without-opencv_flannlib
        --with-opencv_highguilib
        --without-opencv_highguilib
        --with-opencv_imgproclib
        --without-opencv_imgproclib
        --with-opencv_legacylib
        --without-opencv_legacylib
        --with-opencv_mllib
        --without-opencv_mllib
extconf.rb:58:in `block in <main>': opencv_ml not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

ご注文はlopencv_mlですか?

# apt-get install libopencv-ml2.3 libopencv-ml-dev

再々々々々々チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... yes
checking for main() in -lopencv_calib3d... yes
checking for main() in -lopencv_contrib... yes
checking for main() in -lopencv_core... yes
checking for main() in -lopencv_features2d... yes
checking for main() in -lopencv_flann... yes
checking for main() in -lopencv_highgui... yes
checking for main() in -lopencv_imgproc... yes
checking for main() in -lopencv_legacy... yes
checking for main() in -lopencv_ml... yes
checking for main() in -lopencv_objdetect... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
        --with-opencv_contriblib
        --without-opencv_contriblib
        --with-opencv_corelib
        --without-opencv_corelib
        --with-opencv_features2dlib
        --without-opencv_features2dlib
        --with-opencv_flannlib
        --without-opencv_flannlib
        --with-opencv_highguilib
        --without-opencv_highguilib
        --with-opencv_imgproclib
        --without-opencv_imgproclib
        --with-opencv_legacylib
        --without-opencv_legacylib
        --with-opencv_mllib
        --without-opencv_mllib
        --with-opencv_objdetectlib
        --without-opencv_objdetectlib
extconf.rb:58:in `block in <main>': opencv_objdetect not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

lopencv_objdetectはいりまーす。

# apt-get install libopencv-objdetect2.3 libopencv-objdetect-dev

再々々々々々々チャレンジ

# gem install ruby-opencv
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-opencv:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
>> Check the required libraries...
checking for main() in -lstdc++... yes
checking for main() in -lopencv_calib3d... yes
checking for main() in -lopencv_contrib... yes
checking for main() in -lopencv_core... yes
checking for main() in -lopencv_features2d... yes
checking for main() in -lopencv_flann... yes
checking for main() in -lopencv_highgui... yes
checking for main() in -lopencv_imgproc... yes
checking for main() in -lopencv_legacy... yes
checking for main() in -lopencv_ml... yes
checking for main() in -lopencv_objdetect... yes
checking for main() in -lopencv_video... yes
checking for main() in -lopencv_photo... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby1.9.1
        --with-opencv-dir
        --without-opencv-dir
        --with-opencv-include
        --without-opencv-include=${opencv-dir}/include
        --with-opencv-lib
        --without-opencv-lib=${opencv-dir}/lib
        --with-libxml2-dir
        --without-libxml2-dir
        --with-libxml2-include
        --without-libxml2-include=${libxml2-dir}/include
        --with-libxml2-lib
        --without-libxml2-lib=${libxml2-dir}/lib
        --with-stdc++lib
        --without-stdc++lib
        --with-opencv_calib3dlib
        --without-opencv_calib3dlib
        --with-opencv_contriblib
        --without-opencv_contriblib
        --with-opencv_corelib
        --without-opencv_corelib
        --with-opencv_features2dlib
        --without-opencv_features2dlib
        --with-opencv_flannlib
        --without-opencv_flannlib
        --with-opencv_highguilib
        --without-opencv_highguilib
        --with-opencv_imgproclib
        --without-opencv_imgproclib
        --with-opencv_legacylib
        --without-opencv_legacylib
        --with-opencv_mllib
        --without-opencv_mllib
        --with-opencv_objdetectlib
        --without-opencv_objdetectlib
        --with-opencv_videolib
        --without-opencv_videolib
        --with-opencv_photolib
        --without-opencv_photolib
extconf.rb:58:in `block in <main>': opencv_photo not found. (RuntimeError)
        from extconf.rb:58:in `each'
        from extconf.rb:58:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/ruby-opencv-0.0.13/ext/opencv/gem_make.out

lopencv_photoだな…

発展の激しい「computational photography」分野のアルゴリズムをサポートするためのopencv_photoモジュールを追加

【ニュース】OpenCV2.4がリリース | DERiVE コンピュータビジョン ブログ & メルマガ

なるほど、なんでみんなソースからビルドしてるのか、薄皮を剥ぐように少しずつ真実が見えてきた。
aptに登録されている2.3系のパッケージではopencv_photoが使えないからソースから2.4以降をビルドしないといけなかったのね。

後始末として、インストールした各種パッケージをアンインストールします。
メモ残して置いて良かった…。

# apt-get remove ruby-dev libopencv-core2.3 ruby-dev libopencv-calib3d2.3 libopencv-calib3d-dev libopencv-contrib2.3 libopencv-contrib-dev libopencv-legacy2.3 libopencv-legacy-dev libopencv-ml2.3 libopencv-ml-dev libopencv-objdetect2.3 libopencv-objdetect-dev --purge
# apt-get autoremove --purge