Skip to content

Instantly share code, notes, and snippets.

@alskipp
Forked from mattetti/face_detector.rb
Created December 9, 2011 23:00
Show Gist options
  • Save alskipp/1453717 to your computer and use it in GitHub Desktop.
Save alskipp/1453717 to your computer and use it in GitHub Desktop.
Macruby Face Detection in Mac OS X Lion. Usage: $ macruby face_detector.rb or $ macruby face_detector.rb image_url
framework 'Cocoa'
class FaceDetectionDelegate
attr_accessor :window
def initWithURL(url)
case url
when String
@photo_url = NSURL.URLWithString(url)
when NSURL
@photo_url = url
else
raise "The FaceDetectionDelegate class was initiated with an unknown type object"
end
self
end
def applicationDidFinishLaunching(aNotification)
window.delegate = self
puts "Fetching and loading the image #{@photo_url.absoluteString}"
source = CGImageSourceCreateWithURL @photo_url, nil
@cg_image = CGImageSourceCreateImageAtIndex source, 0, nil
puts "image size: w=#{CGImageGetWidth(@cg_image)}, h=#{CGImageGetHeight(@cg_image)}"
view = NSView.alloc.initWithFrame CGRectMake(0.0, 0.0, CGImageGetWidth(@cg_image), CGImageGetHeight(@cg_image))
view.wantsLayer = true
calayer = CALayer.layer
calayer.frame = view.frame
calayer.contents = @cg_image
view.layer.addSublayer calayer
window.setFrame([0.0, 0.0, (CGImageGetWidth(@cg_image)+20), (CGImageGetHeight(@cg_image)+20)], display:true, animate:true)
window.center
window.contentView.wantsLayer = true
window.contentView.layer.affineTransform = CGAffineTransformMakeScale(1, 1)
window.contentView.addSubview(view)
detect_faces
window.orderFrontRegardless
end
def detect_faces
ciImage = CIImage.imageWithCGImage @cg_image
detectorOptions = {CIDetectorAccuracy: CIDetectorAccuracyHigh }
detector = CIDetector.detectorOfType "CIDetectorTypeFace", context:nil, options:detectorOptions
features = detector.featuresInImage(ciImage)
features.each do |feature|
face = CALayer.layer
face.frame = feature.bounds
face.backgroundColor = CGColorCreateGenericRGB(1, 1, 0, 0.4)
window.contentView.layer.addSublayer face
if(feature.hasLeftEyePosition)
left_eye = CALayer.layer
left_eye.frame = CGRectMake(0, 0, 15, 15)
left_eye.backgroundColor = CGColorCreateGenericRGB(0, 0, 1, 0.2)
left_eye.position = feature.leftEyePosition
window.contentView.layer.addSublayer left_eye
end
if(feature.hasRightEyePosition)
right_eye = CALayer.layer
right_eye.frame = CGRectMake(0, 0, 15, 15)
right_eye.backgroundColor = CGColorCreateGenericRGB(1, 0, 0, 0.2)
right_eye.position = feature.rightEyePosition
window.contentView.layer.addSublayer right_eye
end
if(feature.hasMouthPosition)
mouth = CALayer.layer
mouth.frame = CGRectMake(0, 0, 40, 15)
mouth.backgroundColor = CGColorCreateGenericRGB(0, 1, 0, 0.2)
mouth.position = feature.mouthPosition
window.contentView.layer.addSublayer mouth
end
end
end
def windowWillClose(sender); exit(1); end
end
# Create the Application
application = NSApplication.sharedApplication
NSApplication.sharedApplication.activationPolicy = NSApplicationActivationPolicyRegular
application.delegate = FaceDetectionDelegate.alloc.initWithURL(ARGV.shift || "http://merbist.com/wp-content/uploads/2010/03/matz_koichi_matt_aimonetti_sansonetti_jimmy.jpg")
# create the Application Window
frame = [0.0, 0.0, 330, 250]
window = NSWindow.alloc.initWithContentRect frame,
styleMask: NSTitledWindowMask | NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false
application.delegate.window = window
window.orderOut(nil)
window.display
puts "Starting the app..."
application.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment