-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebcamFaceDetector.as
276 lines (228 loc) · 7.19 KB
/
WebcamFaceDetector.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* ==== Welcome to http://www.ubekar.com ====*/
/* ==== Program: JamesChan ====*/
/* ==== Email: [email protected] ====*/
package {
import flash.media.Video;
import flash.media.Camera;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Graphics;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.Video;
import flash.media.Camera;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Graphics;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.*;
import flash.events.Event;
import com.adobe.images.JPGEncoder;
import com.adobe.net.*;
import flash.media.*;
import flash.display.*;
import flash.net.*;
import fl.controls.*;
import flash.events.*;
import flash.geom.*;
import gs.easing.Cubic;
import gs.TweenLite;
import jp.maaash.ObjectDetection.ObjectDetector;
import jp.maaash.ObjectDetection.ObjectDetectorEvent;
import jp.maaash.ObjectDetection.ObjectDetectorOptions;
public class WebcamFaceDetector extends Sprite {
//How long a rectangle will remain visible after no faces are found
private const __noFaceTimeout : int = 500;
//how often to analyze the webcam image
private const __faceDetectInterval : int = 50;
//color of the rectangle
private const __rectColor : int = 0xff0000;
private var _detector :ObjectDetector;
private var _options :ObjectDetectorOptions;
private var _bmpTarget :Bitmap;
private var _detectionTimer : Timer;
private var _rects:Array;
private var _pics:Array;
private var _video : Video;
private var _noFaceTimer : Timer;
public var cameraContainer : Sprite;
public var tu:Loader;
public var bp:Bitmap;
public var picture:String="http://localhost/shoes/try2.php";
public var camera : Camera;
public function WebcamFaceDetector() {
//Timer for rectangles not being found
_noFaceTimer = new Timer( __noFaceTimeout );
_noFaceTimer.addEventListener( TimerEvent.TIMER , _noFaceTimer_timer);
//Array of reusable rectangles
_rects = new Array( );
_pics=new Array();
//timer for how often to detect
_detectionTimer = new Timer( __faceDetectInterval );
_detectionTimer.addEventListener( TimerEvent.TIMER , _detectionTimer_timer);
_detectionTimer.start();
//initalize detector
_initDetector();
//set up camera
_setupCamera();
//hook up detection complete
_detector.addEventListener( ObjectDetectorEvent.DETECTION_COMPLETE , _detection_complete );
}
private function _set_picture():Sprite{
var _pic_sprite:Sprite=new Sprite();
var lu:URLRequest=new URLRequest(picture);
tu=new Loader();
tu.load(lu);
tu.contentLoaderInfo.addEventListener(Event.COMPLETE, picHandler);
_pic_sprite.addChild(tu);
tu.x=10;
tu.y=30;
return _pic_sprite;
}
private function picHandler(evt:Event) {
bp=(tu.content as Bitmap);
bp.x=0;
bp.y=0;
bp.height=60;
bp.width=300;
}
private function _setupCamera() : void{
//var camera : Camera;
var index:int = 0;
for ( var i : int = 0 ; i < Camera.names.length ; i ++ ) {
if ( Camera.names[ i ] == "USB Video Class Video" ) {
index = i;
}
}
camera = Camera.getCamera( String( index ) );
camera.setMode(390, 300, 32);
if (camera != null) {
_video = new Video( camera.width , camera.height );
_video.attachCamera( camera );
addChild( _video );
} else {
trace( "You need a camera." );
}
}
/**
* Called when No faces are found after __noFaceTimeout time
*/
private function _noFaceTimer_timer (event : TimerEvent) : void {
_noFaceTimer.stop();
for (var i : int = 0; i < _rects.length; i++) {
TweenLite.to( _rects[i] , .5, {
alpha:0,
x:_rects[i].x + _video.x,
y:_rects[i].y,
ease:Cubic.easeOut
} );
}
}
/**
* Creates a rectangle
*/
private function _createRect() : Sprite{
var rectContainer : Sprite = new Sprite();
rectContainer.graphics.lineStyle( 2 , __rectColor , 1 );
rectContainer.graphics.beginFill(0x0FF000,0);
rectContainer.graphics.drawRect(0, 0, 100, 100);
return rectContainer;
}
/**
* Evalutates the webcam video for faces on a timer
*/
private function _detectionTimer_timer (event : TimerEvent) : void {
_bmpTarget = new Bitmap( new BitmapData( _video.width, _video.height, false ) );
_bmpTarget.bitmapData.draw( _video );
_detector.detect( _bmpTarget );
}
/**
* Fired when a detection is complete
*/
private function _detection_complete (event : ObjectDetectorEvent) : void {
//no faces found
if(event.rects.length == 0)
return;
//stop the no-face timer and start back up again
_noFaceTimer.stop( );
_noFaceTimer.start();
//loop through faces found
for (var i : int = 0; i < event.rects.length ; i++) {
//create rectangles if needed
if(_rects[i] == null) {
_rects[i] = _createRect();
addChild(_rects[i]);
}
//create pictures
if(_pics[i]==null) {
_pics[i]=_set_picture();
addChild(_pics[i]);
}
//Animate to new size
TweenLite.to( _rects[i] , .5, {
alpha:0,
x:event.rects[i].x*_video.scaleX + _video.x,
y:event.rects[i].y*_video.scaleY,
width:event.rects[i].width*_video.scaleX,
height:event.rects[i].height*_video.scaleY,
ease:Cubic.easeOut
});
// trace("x="+event.rects[i].x*_video.scaleX + _video.x);
// trace("y="+event.rects[i].y*_video.scaleY);
TweenLite.to(_pics[i], .5,{
alpha:1,
x:event.rects[i].x*_video.scaleX + _video.x-120,
y:event.rects[i].y*_video.scaleY+20,
width:event.rects[i].width*_video.scaleX+200,
height:event.rects[i].height*_video.scaleY+100,
ease:Cubic.easeOut
});
}
//hide the rest of the rectangles
if(event.rects.length < _rects.length){
for (var j : int = event.rects.length; j < _rects.length; j++) {
TweenLite.to( _rects[j] , .5, {
alpha:0,
x:_rects[j].x,
y:_rects[j].y,
ease:Cubic.easeOut
} );
TweenLite.to( _pics[j] , .5, {
alpha:0,
x:_pics[j].x,
y:_pics[j].y,
ease:Cubic.easeOut
} );
}
}
}
/**
* Initializes the detector
*/
private function _initDetector () : void {
_detector = new ObjectDetector;
_detector.options = getDetectorOptions( );
_detector.loadHaarCascades( "face.zip" );
}
/**
* Gets dector options
*/
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;
}
}
}