In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

[facekit] detect face -> draw green ball on the nose

sample face recognition application "draw green ball on the nose"
Get Adobe Flash player
by yosukema 11 Jan 2009
// sample face recognition application "draw green ball on the nose"
package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.system.Security;
    import flash.text.TextField;
    import flash.utils.Dictionary;

    [SWF(width=320, height=263)]
    public class facekitsample extends Sprite
    {
        private var _loader:Loader;
        private var _label:TextField;
        private var _ball:Sprite;

        public function facekitsample()
        {
            // load facekit
            Security.allowDomain("facekit.net");
            Security.loadPolicyFile("http://facekit.net/flash/crossdomain.xml");
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoadComplete);
            _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
            _loader.load(new URLRequest("http://facekit.net/flash/facekit_module"));
            addChild(_loader);

            // status label
            _label = new TextField();
            addChild(_label);

            // ball
            _ball = new Sprite();
            _ball.graphics.beginFill(0x00ff00);
            _ball.graphics.drawCircle(0, 0, 20);
            _ball.graphics.endFill();
            _ball.visible = false;
            addChild(_ball);
        }
        
        private function onProgress(e:ProgressEvent):void
        {
            _label.text = "Loading... " + int(100.0 * e.bytesLoaded / e.bytesTotal) + "%";
        }

        private function onLoadComplete(e:Event):void
        {
            var cfacekit:Class = _loader.contentLoaderInfo.applicationDomain.getDefinition("facekit") as Class;
            (_loader.content as cfacekit).withoutgraphics = true;
            (_loader.content as cfacekit).setCallback(callback);
        }

        private function callback(d:Dictionary):void
        {
            _label.text = d["state"];
            if (d["state"] == "detected") {
            	_ball.visible = true;
                _ball.x = d["x"] + d["width"] / 2 + d["width"] / 2 * Math.sin(d["xdir"] / 180.0 * Math.PI);
                _ball.y = d["y"] + d["height"] / 2;
                _ball.width = d["width"] / 3;
                _ball.height = _ball.width;
            }
        }
    }

}