家庭教師のトライをFlashで試してみた

2008 年 10 月 14 日 by 正宗
こんな感じ

こんな感じ

SPARK Projectに顔認識用のライブラリがあるので家庭教師のトライにトライしてみました(ややこしい)。

画面に直接マウスで落書きすると、顔の動きに落書きが追随します。

サンプルはこちらのページにあります

動作にはWEBカメラが必要。あとめちゃくちゃ重いです。

ライブラリを普通に使ってるだけで特別なことしてないのがバレバレで恥ずかしいですが以下はコードです。重くて泣きたくなる場合は37行目のコメントアウトを外して代わりに38行目をコメントアウトしてみて下さい。

ドキュメントクラス-Main.as

顔認識は全部ここに突っ込んだ

  1. package {
  2. import flash.display.Sprite;
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Graphics;
  6. import flash.geom.Matrix;
  7. import flash.geom.Rectangle;
  8. //
  9. import flash.events.Event;
  10. //
  11. import flash.media.Camera;
  12. import flash.media.Video;
  13. import flash.events.ActivityEvent;
  14. //
  15. import jp.maaash.ObjectDetection.ObjectDetector;
  16. import jp.maaash.ObjectDetection.ObjectDetectorOptions;
  17. import jp.maaash.ObjectDetection.ObjectDetectorEvent;
  18.  
  19. public class Main extends Sprite {
  20. private const VIDEO_SCALE:Number = .8;
  21. private const DEFAULT_FACE_RECT:Number = 50;
  22. private var camera:Camera;
  23. private var video:Video;
  24. private var detector:ObjectDetector;
  25. private var options:ObjectDetectorOptions;
  26.  
  27. private var raku:Raku;
  28. function Main():void {
  29. init();
  30. initDetector();
  31. }
  32. private function init():void {
  33. camera = Camera.getCamera();
  34. if (camera != null) {
  35. video = new Video(stage.stageWidth, stage.stageHeight);
  36. video.attachCamera(camera);
  37. //camera.addEventListener(ActivityEvent.ACTIVITY, onCameraActivityHandler);
  38. addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
  39. addChild(video);
  40. }
  41. var target:Sprite = new Sprite();
  42. var g:Graphics = target.graphics;
  43. g.beginFill(0x000000, 0);
  44. g.drawRect(0, 0, stage.stageHeight*2, stage.stageWidth*2)
  45. g.endFill();
  46. addChild(target)
  47. raku = new Raku(target);
  48. addChild(raku);
  49. }
  50. private function initDetector():void {
  51. detector = new ObjectDetector;
  52. detector.options = getDetectorOptions();
  53. detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, onDetectorCompleteHandler);
  54. detector.loadHaarCascades( '/samples/kao/face.zip' );
  55. }
  56. private function onCameraActivityHandler(e:ActivityEvent):void {
  57. if (e.activating) {
  58. addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
  59. } else {
  60. removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
  61. }
  62. }
  63. private function onEnterFrameHandler(e:Event):void {
  64. var bitmap:Bitmap = new Bitmap(new BitmapData(video.width, video.height, false));
  65. var matrix:Matrix = new Matrix();
  66. matrix.scale(VIDEO_SCALE, VIDEO_SCALE);
  67. bitmap.bitmapData.draw(video,matrix);
  68. detector.detect(bitmap);
  69. }
  70. private function onDetectorCompleteHandler(e:ObjectDetectorEvent):void {
  71. if ( e.rects ) {
  72. e.rects.forEach( function( r :Rectangle, idx :int, arr :Array ) :void {
  73. raku.moveRaku( r.x/VIDEO_SCALE, r.y/VIDEO_SCALE, r.width/VIDEO_SCALE, r.height/VIDEO_SCALE );
  74. });
  75. }
  76. }
  77. private function getDetectorOptions():ObjectDetectorOptions {
  78. options = new ObjectDetectorOptions;
  79. options.min_size = 50;
  80. options.startx = ObjectDetectorOptions.INVALID_POS;
  81. options.starty = ObjectDetectorOptions.INVALID_POS;
  82. options.endx = ObjectDetectorOptions.INVALID_POS;
  83. options.endy = ObjectDetectorOptions.INVALID_POS;
  84. return options;
  85. }
  86. }
  87. }

落書きするクラス-Raku.as

顔認識で得た矩形に合わせて拡大縮小してるだけ

  1. package {
  2.  
  3. import flash.display.Sprite;
  4. import flash.display.Graphics;
  5. import flash.events.MouseEvent;
  6. import flash.events.Event;
  7. public class Raku extends Sprite {
  8. private const DEFAULT_FACE_WIDTH:Number = 50;
  9. private const DEFAULT_FACE_HEIGHT:Number = 50;
  10.  
  11. private var this_scale_x:Number = 1;
  12. private var this_scale_y:Number = 1;
  13. private var g:Graphics;
  14. private var target:Sprite;
  15. function Raku(mc:Sprite):void {
  16. target = mc;
  17. init();
  18. }
  19. private function init():void {
  20. target.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
  21. Sprite(target).stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
  22. g = graphics;
  23. g.lineStyle(2, 0x000000);
  24. }
  25. private function onMouseDownHandler(e:MouseEvent):void {
  26. target.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
  27. g.moveTo((e.stageX-x)/this_scale_x, (e.stageY-y)/this_scale_y);
  28. }
  29. private function onMouseUpHandler(e:MouseEvent):void {
  30. target.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
  31. }
  32. private function onMouseMoveHandler(e:MouseEvent):void {
  33. g.lineTo((e.stageX-x)/this_scale_x, (e.stageY-y)/this_scale_y);
  34. }
  35. public function moveRaku(_x:Number, _y:Number, _width:Number, _height:Number):void{
  36. this.x = _x;
  37. this.y = _y;
  38. this_scale_x = _width/DEFAULT_FACE_WIDTH;
  39. this_scale_y = _height/DEFAULT_FACE_HEIGHT;
  40. this.scaleX = this_scale_x
  41. this.scaleY = this_scale_y
  42. }
  43. }
  44. }

タグ: ,

Related posts

コメントをどうぞ