16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

さんじゃらっと ruby + OpenCV で顔認識してトリミングするお

Last updated at Posted at 2015-05-02

こんにちは

前回のさんじゃらっと OSX + ruby でOpenCVの続きです。

ruby-opencvを使って画像から顔を認識して顔の部分だけを切り取ります。

スクリプトはこちらです。

face_detection.rb
require 'opencv'
include OpenCV

if ARGV.length < 1
  puts "Usage: ruby #{__FILE__} source dest"
  exit
end

data = './haarcascade_frontalface_alt.xml'
detector = CvHaarClassifierCascade::load(data)
image = IplImage.load(ARGV[0])

detector.detect_objects(image).each do |region|
  color = CvColor::Blue
  image.rectangle! region.top_left, region.bottom_right, :color => color #ここで顔に枠をつけるよ
  image.set_roi(region) #ここでさらに顔の部分だけを切り取り
end

#image.save_image(ARGV[1])
window = GUI::Window.new('Face detection')
window.show(image)
GUI::wait_key

ただ、これを実行すると現時点では
error: (-2) The node does not represent a user object (unknown type?) in function cvRead (OpenCV::CvStsError)
こんなエラーが出ました。

cascade用のhaar-like-fatureのxmlの型が古いようです。
そこで最新のxmlをどこかからwgetとかしてきて使いました

例えばこんなん
wget https://raw.githubusercontent.com/bwkeller/warai/master/haarcascade_frontalface_alt.xml

ほんで、
ruby face_detection face.jpg
とかやると

lena.jpg

out.jpg

こうなりました!

Stay Gold!

16
16
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
16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?