Skip to content

Instantly share code, notes, and snippets.

@kei-s
Created June 10, 2013 06:07
Show Gist options
  • Save kei-s/5746820 to your computer and use it in GitHub Desktop.
Save kei-s/5746820 to your computer and use it in GitHub Desktop.
OpenCV Face Detect sample.
require 'bundler/setup'
require 'opencv'
include OpenCV
if ARGV.length < 2
puts "Usage: ruby #{__FILE__} source dest"
exit
end
FACE_DETECTORS = %w(
haarcascade_frontalface_default.xml
haarcascade_frontalface_alt.xml
haarcascade_frontalface_alt2.xml
haarcascade_frontalface_alt_tree.xml
haarcascade_profileface.xml
)
EYE_DETECTORS = %w(
haarcascade_eye.xml
haarcascade_eye_tree_eyeglasses.xml
haarcascade_lefteye_2splits.xml
haarcascade_righteye_2splits.xml
haarcascade_mcs_eyepair_small.xml
haarcascade_mcs_eyepair_big.xml
haarcascade_mcs_righteye.xml
haarcascade_mcs_lefteye.xml
)
FACE_PART_DETECTORS = %w(
haarcascade_mcs_leftear.xml
haarcascade_mcs_rightear.xml
haarcascade_mcs_mouth.xml
haarcascade_mcs_nose.xml
)
SMILE_DETECTORS = %w(
haarcascade_smile.xml
)
BODY_DETECTORS = %w(
haarcascade_fullbody.xml
haarcascade_upperbody.xml
haarcascade_lowerbody.xml
haarcascade_mcs_upperbody.xml
)
detectors = EYE_DETECTORS.map do |data|
file = "/usr/local/share/OpenCV/haarcascades/#{data}"
CvHaarClassifierCascade::load(file)
end
image = CvMat.load(ARGV[0])
gray = OpenCV.BGR2GRAY(image)
gray.equalize_hist
detectors.each do |detector|
puts "Detecting: #{detector}"
detector.detect_objects(gray).each do |region|
puts "Detect: #{region}"
color = CvColor::Blue
image.rectangle! region.top_left, region.bottom_right, :color => color
end
end
image.save_image(ARGV[1])
`open #{ARGV[1]}`
#window = GUI::Window.new('Face detection')
#window.show(image)
#GUI::wait_key
@kei-s
Copy link
Author

kei-s commented Jun 10, 2013

$ brew tap homebrew/science && brew install opencv

gem "ruby-opencv"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment