LoginSignup
107
105

More than 5 years have passed since last update.

Node.jsとOpenCVで顔認識してみます。

Posted at

やることとバージョン

OpenCVが気になっていたのでNode.jsから使えるライブラリを探して試してみました。

ライブラリはこちらを使ってみます。
https://github.com/peterbraden/node-opencv

  • Node.js 0.12.0
  • Mac OS 10.10.2 Yosemite
  • Homebrew 0.9.5
  • OpenCV 2.4
  • node-opencv 3.0

OpenCV

OpenCVはインテルが開発した画像処理や画像解析等のライブラリです。

準備(とハマったところ)

opencvインストールで戸惑う。

npm install opencvだけやろうとするとエラーめっちゃ出ました苦笑

$ npm install opencv
\
> opencv@3.0.0 install /Users/sugawara_nobisuke/n0bisuke/lab/opencv/node_modules/opencv
> node-pre-gyp install --fallback-to-build

child_process: customFds option is deprecated, use stdio instead.
child_process: customFds option is deprecated, use stdio instead.
/bin/sh: pkg-config: command not found
gyp: Call to 'pkg-config --cflags opencv' returned exit status 127. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/sugawara_nobisuke/.nvm/versions/v0.12.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:343:16)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Darwin 14.1.0
・
・
・

どうやらpkg-configが無いよって怒られてるみたい

$ brew install pkg-config

まだエラーでてます。opencvがそもそも入ってなかった疑惑

$ brew install opencv
Error: No available formula for opencv
Searching formulae...
Searching taps...
homebrew/science/opencv

こうすると良いみたい。
http://qiita.com/laprasDrum/items/59092e96608dd106be63

$ brew tap homebrew/science
$ brew install opencv
・
・
・
==> python setup.py build --fcompiler=gnu95 install --prefix=/usr/local/Cellar/numpy
?  /usr/local/Cellar/numpy/1.9.1: 693 files, 14M, built in 112 seconds
==> Installing opencv
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/science/opencv-
######################################################################## 100.0%
==> Pouring opencv-2.4.10.1.yosemite.bottle.tar.gz
?  /usr/local/Cellar/opencv/2.4.10.1: 219 files, 39M

上手くいったみたいです。
これでMac(Yosemite)にopencv2.4がインストールされたみたいです。

もう一度node-opencvを入れてみます。

$ npm install opencv
-
> opencv@3.0.0 install /Users/sugawara_nobisuke/n0bisuke/lab/opencv/node_modules/opencv
> node-pre-gyp install --fallback-to-build

child_process: customFds option is deprecated, use stdio instead.
child_process: customFds option is deprecated, use stdio instead.
  CXX(target) Release/obj.target/opencv/src/init.o
  CXX(target) Release/obj.target/opencv/src/Matrix.o
  CXX(target) Release/obj.target/opencv/src/OpenCV.o
  CXX(target) Release/obj.target/opencv/src/CascadeClassifierWrap.o
  CXX(target) Release/obj.target/opencv/src/Contours.o
  CXX(target) Release/obj.target/opencv/src/Point.o
  CXX(target) Release/obj.target/opencv/src/VideoCaptureWrap.o
  CXX(target) Release/obj.target/opencv/src/CamShift.o
  CXX(target) Release/obj.target/opencv/src/HighGUI.o
  CXX(target) Release/obj.target/opencv/src/FaceRecognizer.o
  CXX(target) Release/obj.target/opencv/src/BackgroundSubtractor.o
  CXX(target) Release/obj.target/opencv/src/Constants.o
  CXX(target) Release/obj.target/opencv/src/Calib3D.o
  CXX(target) Release/obj.target/opencv/src/ImgProc.o
  SOLINK_MODULE(target) Release/opencv.node
  SOLINK_MODULE(target) Release/opencv.node: Finished
  COPY /Users/sugawara_nobisuke/n0bisuke/lab/opencv/node_modules/opencv/build/opencv/v3.0.0/Release/node-v14-darwin-x64/opencv.node
  TOUCH Release/obj.target/action_after_build.stamp
opencv@3.0.0 node_modules/opencv
├── buffers@0.1.1
├── nan@1.4.3
└── node-pre-gyp@0.5.31

動かしてみる

まずは画像を用意します。 LIGの入社式記事から拝借。

n0bisuke.jpgとして保存してapp.jsと同じ階層に設置しました。

app.js
var cv = require('opencv');

cv.readImage("./n0bisuke.jpg", function(err, im){
  im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
    for (var i=0;i<faces.length; i++){
      var x = faces[i]
      im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
    }
    im.save('./out.jpg');
  });
})

実行してみます。

$ node app.js

out.jpgが出力されているので確認してみます。

顔に印がついてますね!

どうやら顔認識ができたみたいです。

107
105
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
107
105