Hatena::ブログ(Diary)

kazutoyo@hatena

ページビュー
6216

2010-05-07 KUWAGATize on Flash的なことやってみた。

| 00:43

クワガタイズ出来たよ|イエイリカズマ Official Blog 〜生け贄スタイルの理論と実践〜

今さらですが、Flashで作ってみました。

KUWAGATize on Flash

f:id:kazutoyo_tk:20100508003228j:image

OpenCVAS3に移植したMarilenaを使っています。

以下コード

package 
{
	import adobe.utils.CustomActions;
	import flash.display.*;
	import flash.events.ActivityEvent;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.Timer;
	import jp.maaash.ObjectDetection.*;
	
	/**
	 * ...
	 * @author kazutoyo
	 */
	public class Main extends Sprite 
	{
		[Embed(source = 'kuwa0.png')]
		private const Kuwa0:Class;
		private const CAMERA_WIDTH:Number = 640;
		private const CAMERA_HEIGHT:Number = 480;
		private const VIDEO_SCALE:Number = 0.5;
		private const DEFAULT_FACE_RECT:Number = 40;
		private const DEFAULT_FACE_WIDTH:Number = 40;
		private const DEFAULT_FACE_HEIGHT:Number = 40;
		private const DEFAULT_IMG_WIDTH:Number = 165;
		private const DEFAULT_IMG_HEIGHT:Number = 164;
		private var img:Bitmap;
		private var camera:Camera;
		private var video:Video;
		private var detector:ObjectDetector;
		private var options:ObjectDetectorOptions;
		private var timer:Timer = new Timer(500, 0);
		private var v_bmd:BitmapData;
		private var matrix:Matrix = new Matrix();
		private var v_bmp:Bitmap = new Bitmap(v_bmd);
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			//Stage
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			//クワガタさんを呼び出す
			img = new Kuwa0();
			addChild(img);
			
			//カメラの初期化
			this.setupCamera();
			//Detector
			this.initDetector();
			
			//matrix
			matrix.scale(VIDEO_SCALE, VIDEO_SCALE);
			v_bmd = new BitmapData(CAMERA_WIDTH * VIDEO_SCALE, CAMERA_HEIGHT * VIDEO_SCALE);
		}
		
		private function setupCamera():void {
			camera = Camera.getCamera();
            if(camera == null){
                trace("カメラがありません");
                return;
            }else{
				camera.setMode(CAMERA_WIDTH, CAMERA_HEIGHT, 15);
				video = new Video(camera.width, camera.height);
				video.attachCamera(camera);
				timer.addEventListener(TimerEvent.TIMER, onTimer);
				timer.start();
				addChildAt(video,0);
			}
        }
		
		private function initDetector():void {
			detector = new ObjectDetector();
			detector.options = this.getDetectorOptions();
			detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, onDetectorComplete);
			detector.loadHaarCascades('face.zip');
		}
		
		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 onDetectorComplete(e:ObjectDetectorEvent):void {
			if (e.rects) {
				e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
					img.width = ((r.width / DEFAULT_FACE_WIDTH) * DEFAULT_IMG_WIDTH)/VIDEO_SCALE;
					img.height = ((r.height / DEFAULT_FACE_HEIGHT) * DEFAULT_IMG_HEIGHT)/VIDEO_SCALE;
					img.x = (r.x/VIDEO_SCALE) - (img.width*(70/165));
					img.y = (r.y/VIDEO_SCALE) - (img.height * (90 / 164));
				});
			}
		}
		
		private function onTimer(e:TimerEvent):void {
			timer.stop();
			v_bmd.draw(video, matrix);
			v_bmp.bitmapData = v_bmd;
			detector.detect(v_bmp);
			timer.start();
		}
		
	}
	
}

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証