| 1 |
package |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.display.StageScaleMode; |
|---|
| 4 |
import flash.display.StageAlign; |
|---|
| 5 |
import flash.display.Sprite; |
|---|
| 6 |
import flash.display.Loader; |
|---|
| 7 |
import flash.display.Bitmap; |
|---|
| 8 |
import flash.display.BitmapData; |
|---|
| 9 |
import flash.display.Graphics; |
|---|
| 10 |
import flash.events.Event; |
|---|
| 11 |
import flash.geom.Rectangle; |
|---|
| 12 |
import flash.net.URLRequest; |
|---|
| 13 |
import flash.text.TextField; |
|---|
| 14 |
import jp.maaash.ObjectDetection.ObjectDetector; |
|---|
| 15 |
import jp.maaash.ObjectDetection.ObjectDetectorOptions; |
|---|
| 16 |
import jp.maaash.ObjectDetection.ObjectDetectorEvent; |
|---|
| 17 |
|
|---|
| 18 |
public class FaceDetector extends Sprite{ |
|---|
| 19 |
private var debug :Boolean = false; |
|---|
| 20 |
|
|---|
| 21 |
private var detector :ObjectDetector; |
|---|
| 22 |
private var options :ObjectDetectorOptions; |
|---|
| 23 |
private var faceImage :Loader; |
|---|
| 24 |
private var bmpTarget :Bitmap; |
|---|
| 25 |
|
|---|
| 26 |
private var view :Sprite; |
|---|
| 27 |
private var faceRectContainer :Sprite; |
|---|
| 28 |
private var tf :TextField; |
|---|
| 29 |
|
|---|
| 30 |
public function FaceDetector() { |
|---|
| 31 |
initUI(); |
|---|
| 32 |
initDetector(); |
|---|
| 33 |
faceImage.load( new URLRequest("013.jpg") ); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
private function initUI():void{ |
|---|
| 37 |
stage.scaleMode = StageScaleMode.NO_SCALE; |
|---|
| 38 |
stage.align = StageAlign.TOP_LEFT; |
|---|
| 39 |
|
|---|
| 40 |
view = new Sprite; |
|---|
| 41 |
addChild(view); |
|---|
| 42 |
|
|---|
| 43 |
faceImage = new Loader; |
|---|
| 44 |
faceImage.contentLoaderInfo.addEventListener( Event.COMPLETE, function(e :Event) :void { |
|---|
| 45 |
startDetection(); |
|---|
| 46 |
}); |
|---|
| 47 |
view.addChild( faceImage ); |
|---|
| 48 |
|
|---|
| 49 |
faceRectContainer = new Sprite; |
|---|
| 50 |
view.addChild( faceRectContainer ); |
|---|
| 51 |
|
|---|
| 52 |
tf = new TextField; |
|---|
| 53 |
tf.x = 256; |
|---|
| 54 |
tf.width = 600; |
|---|
| 55 |
tf.height = 300; |
|---|
| 56 |
tf.textColor = 0x000000; |
|---|
| 57 |
tf.multiline = true; |
|---|
| 58 |
view.addChild( tf ); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
private function initDetector():void{ |
|---|
| 62 |
detector = new ObjectDetector; |
|---|
| 63 |
detector.options = getDetectorOptions(); |
|---|
| 64 |
detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE,function( e :ObjectDetectorEvent ):void{ |
|---|
| 65 |
logger("[ObjectDetectorEvent.COMPLETE]"); |
|---|
| 66 |
tf.appendText( "\ntime: "+(new Date)+" "+e.type ); |
|---|
| 67 |
detector.removeEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, arguments.callee ); |
|---|
| 68 |
|
|---|
| 69 |
if( e.rects ){ |
|---|
| 70 |
var g :Graphics = faceRectContainer.graphics; |
|---|
| 71 |
g.clear(); |
|---|
| 72 |
g.lineStyle( 2 ); // black 2pix |
|---|
| 73 |
e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void { |
|---|
| 74 |
g.drawRect( r.x, r.y, r.width, r.height ); |
|---|
| 75 |
}); |
|---|
| 76 |
} |
|---|
| 77 |
}); |
|---|
| 78 |
var events :Array = [ ObjectDetectorEvent.DETECTION_START, ObjectDetectorEvent.HAARCASCADES_LOAD_COMPLETE, ObjectDetectorEvent.HAARCASCADES_LOADING ]; |
|---|
| 79 |
events.forEach( function(t :String, idx :int, arr :Array ) :void { |
|---|
| 80 |
detector.addEventListener( t, function(e :ObjectDetectorEvent) :void { |
|---|
| 81 |
tf.appendText( "\ntime: "+(new Date)+" "+e.type ); |
|---|
| 82 |
}); |
|---|
| 83 |
}); |
|---|
| 84 |
detector.loadHaarCascades( "face.zip" ); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
private function startDetection():void{ |
|---|
| 88 |
logger("[startDetection]"); |
|---|
| 89 |
|
|---|
| 90 |
bmpTarget = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) ) |
|---|
| 91 |
bmpTarget.bitmapData.draw( faceImage ); |
|---|
| 92 |
detector.detect( bmpTarget ); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
private function getDetectorOptions() :ObjectDetectorOptions { |
|---|
| 96 |
options = new ObjectDetectorOptions; |
|---|
| 97 |
options.min_size = 50; |
|---|
| 98 |
options.startx = ObjectDetectorOptions.INVALID_POS; |
|---|
| 99 |
options.starty = ObjectDetectorOptions.INVALID_POS; |
|---|
| 100 |
options.endx = ObjectDetectorOptions.INVALID_POS; |
|---|
| 101 |
options.endy = ObjectDetectorOptions.INVALID_POS; |
|---|
| 102 |
return options; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
private function logger(... args):void{ |
|---|
| 106 |
if(!debug){ return; } |
|---|
| 107 |
log(args); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|