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