Skip to content

Instantly share code, notes, and snippets.

@siuying
Forked from mattetti/face_detector.rb
Created December 1, 2011 16:17
Show Gist options
  • Save siuying/1417905 to your computer and use it in GitHub Desktop.
Save siuying/1417905 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 NSColor
def toCGColor
color_RGB = colorUsingColorSpaceName(NSCalibratedRGBColorSpace)
## approach #1
# components = Array.new(4){Pointer.new(:double)}
# color_RGB.getRed(components[0],
# green: components[1],
# blue: components[2],
# alpha:components[3])
# components.collect!{|x| x[0] }
# approach #2
components = [redComponent, greenComponent, blueComponent, alphaComponent]
color_space = CGColorSpaceCreateWithName(KCGColorSpaceGenericRGB)
color = CGColorCreate(color_space, components)
CGColorSpaceRelease(color_space)
color
end
end
# NSVIew set background color & and set center
class NSView
# set background like on IOS
def background_color=(color)
viewLayer = CALayer.layer
viewLayer.backgroundColor = color.toCGColor
self.wantsLayer = true # // view's backing store is using a Core Animation Layer
self.layer = viewLayer
end
# helper to set nsview center like on IOS
def center= (point)
self.frameOrigin = [point.x-(self.frame.size.width/2), point.y-(self.frame.size.height/2)]
self.needsDisplay = true
end
end
# NSImage to CGImage
class NSImage
def to_CGImage
source = CGImageSourceCreateWithData(self.TIFFRepresentation, nil)
maskRef = CGImageSourceCreateImageAtIndex(source, 0, nil)
end
end
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}"
image = NSImage.alloc.initWithContentsOfURL @photo_url
# Helpers to set the NSImageView size
bitmap = NSBitmapImageRep.imageRepWithData image.TIFFRepresentation
puts "image size: w=#{bitmap.pixelsWide}, h=#{bitmap.pixelsHigh}" if bitmap
imageView = NSImageView.alloc.init
imageView.image = image
imageView.wantsLayer = true
imageView.imageScaling = NSImageScaleProportionallyUpOrDown
imageView.layer.affineTransform = CGAffineTransformMakeScale(-1, 1)
imageView.imageFrameStyle = NSImageFramePhoto
window.setFrame([0.0, 0.0, (bitmap.pixelsWide+20), (bitmap.pixelsHigh+20)], display:true, animate:true)
window.center
window.contentView.wantsLayer = true
window.contentView.layer.affineTransform = CGAffineTransformMakeScale(1, 1)
imageView.frame = CGRectMake(0.0, 0.0, bitmap.pixelsWide, bitmap.pixelsHigh)
window.contentView.addSubview(imageView)
detect_faces
window.orderFrontRegardless
end
def detect_faces
ciImage = CIImage.imageWithCGImage window.contentView.subviews.last.image.to_CGImage
mustacheImage = NSImage.alloc.initWithContentsOfFile "mustache_03.png"
detectorOptions = {CIDetectorAccuracy: CIDetectorAccuracyHigh }
detector = CIDetector.detectorOfType "CIDetectorTypeFace", context:nil, options:detectorOptions
features = detector.featuresInImage(ciImage)
features.each do |feature|
if(feature.hasMouthPosition)
imageView = NSImageView.alloc.init
imageView.image = mustacheImage
imageView.wantsLayer = true
imageView.imageScaling = NSImageScaleProportionallyUpOrDown
imageView.layer.affineTransform = CGAffineTransformMakeScale(-1, 1)
imageView.imageFrameStyle = NSImageFrameNone
imageView.frame = CGRectMake(0.0, 0.0, 40, 15)
imageView.center = feature.mouthPosition
window.contentView.addSubview(imageView)
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