PasteAll.org
// License: GPL - http://www.gnu.org/licenses/#GPL

// If you use the code, you can say thank you by linking to the new free math project www.echteinfach.tv from one of your websites, cheers


package {

import Main.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.text.*;
import flash.utils.*;
import jp.maaash.ObjectDetection.*;
import org.papervision3d.cameras.*;
import org.papervision3d.events.*;
import org.papervision3d.materials.*;
import org.papervision3d.materials.utils.*;
import org.papervision3d.objects.*;
import org.papervision3d.objects.parsers.*;
import org.papervision3d.render.*;
import org.papervision3d.scenes.*;
import org.papervision3d.view.*;

public class Main extends Sprite
{
private var HeadCenterX:Number;
private var HeadCenterY:Number;
private var video:Video;
private var scene:Scene3D;
private var bre:BasicRenderEngine;
private var detectionMap:BitmapData;
private var sceneWidth:int;
private var faceRectContainer:Sprite;
private var mp:MaterialsList;
private var drawMatrix:Matrix;
private var options:ObjectDetectorOptions;
private var movingForward:Boolean = true;
private var bmpdFromVideo:BitmapData;
private var intervallToFetch:int;
private var scaleFactor:int = 4;
private var vp:Viewport3D;
private var screenButton:HoldButton;
private var collada1:Collada;
private var collada2:Collada;
private var collada3:Collada;
private var detector:ObjectDetector;
private var graph:Graphics;
private var cam:Camera3D;
private var sceneHeight:int;
private var mat:BitmapFileMaterial;
private var bmpFromVideo:Bitmap;
private var showRectangle:Boolean;
private var webcam:cameraClass;

public function Main() : void
{
var _loc_1:TextField = null;
this.sceneWidth = 640;
this.sceneHeight = 480;
this.intervallToFetch = 67;
this.faceRectContainer = new Sprite();
this.showRectangle = false;
this.webcam = new cameraClass(this.sceneWidth, this.sceneHeight);
this.video = this.webcam.camVideo;
if (this.video == null)
{
trace("NO WEBCAM!!");
_loc_1 = new TextField();
_loc_1.text = "Please CONNECT your WEBCAM and TRY AGAIN";
_loc_1.width = 350;
_loc_1.height = 100;
_loc_1.x = 180;
_loc_1.y = 220;
_loc_1.selectable = false;
addChild(_loc_1);
}
else
{
this.video.addEventListener(Event.ACTIVATE, this.initWebcam);
var _loc_2:int = 0;
this.video.y = 0;
this.video.x = _loc_2;
addChild(this.video);
this.bmpdFromVideo = new BitmapData(this.video.width, this.video.height, false);
this.detectionMap = new BitmapData(640 / this.scaleFactor, 480 / this.scaleFactor, false, 0);
this.drawMatrix = new Matrix(1 / this.scaleFactor, 0, 0, 1 / this.scaleFactor);
this.initDetector();
addChild(this.faceRectContainer);
this.setupPV3D();
this.addCollada();
addEventListener(Event.ENTER_FRAME, this.loop);
root.addEventListener(MouseEvent.CLICK, this.mouseClicked);
this.collada1.visible = true;
this.collada2.visible = false;
this.collada3.visible = false;
this.screenButton = new HoldButton();
}
return;
}

private function initDetector() : void
{
this.detector = new ObjectDetector();
var _loc_1:* = new ObjectDetectorOptions();
_loc_1.min_size = 30;
this.detector.options = _loc_1;
this.detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, this.detectionHandler);
return;
}

private function getAndDetectCamImage(event:Event) : void
{
this.bmpdFromVideo.draw(this.video);
this.detectionMap.draw(this.bmpdFromVideo, this.drawMatrix, null, "normal", null, true);
this.detector.detect(this.detectionMap);
return;
}

private function startTimer() : void
{
var _loc_1:* = new Timer(this.intervallToFetch);
_loc_1.addEventListener("timer", this.getAndDetectCamImage);
_loc_1.start();
return;
}

private function detectionHandler(event:ObjectDetectorEvent) : void
{
if (this.collada1.z > 822000)
{
this.movingForward = false;
}
if (this.collada1.z < 15000)
{
this.movingForward = true;
}
this.graph = this.faceRectContainer.graphics;
this.graph.clear();
if (this.collada1.visible == true)
{
this.moveObjectByDetectedData(event, this.collada1);
}
else if (this.collada2.visible == true)
{
this.moveObjectByDetectedData(event, this.collada2);
}
else if (this.collada3.visible == true)
{
this.moveObjectByDetectedData(event, this.collada3);
}
return;
}

private function setupPV3D() : void
{
this.scene = new Scene3D();
this.cam = new Camera3D();
this.cam.z = -400;
this.vp = new Viewport3D();
this.bre = new BasicRenderEngine();
addChild(this.vp);
return;
}

private function bitmapLoadedAssignTransparency(event:Event) : void
{
this.mat.bitmap.colorTransform(this.mat.bitmap.rect, new ColorTransform(1, 1, 1, 0.5));
return;
}

private function loop(event:Event) : void
{
this.bre.renderScene(this.scene, this.cam, this.vp);
return;
}

private function initWebcam(event:Event) : void
{
trace("#INIT\nVideo started:", event, "\n");
this.video.removeEventListener(Event.ACTIVATE, init);
this.webcam.camera.addEventListener(ActivityEvent.ACTIVITY, this.activated);
var _loc_2:* = this.video.transform.matrix;
_loc_2.a = -1;
_loc_2.tx = this.video.width + this.video.x;
this.video.transform.matrix = _loc_2;
return;
}

private function mouseClicked(event:Event) : void
{
if (this.collada1.visible == true)
{
this.collada2.x = this.collada2.x;
this.collada2.y = this.collada2.y;
this.collada2.z = this.collada2.z;
this.collada1.visible = false;
this.collada2.visible = true;
}
else if (this.collada2.visible == true)
{
this.collada1.x = this.collada2.x;
this.collada1.y = this.collada2.y;
this.collada1.z = this.collada2.z;
this.collada2.visible = false;
this.collada3.visible = true;
}
else if (this.collada3.visible == true)
{
this.collada3.x = this.collada2.x;
this.collada3.y = this.collada2.y;
this.collada3.z = this.collada2.z;
this.collada3.visible = false;
this.collada1.visible = true;
}
return;
}

private function handleColladaComplete(event:FileLoadEvent) : void
{
this.scene.addChild(this.collada1);
this.collada1.scale = 0.65;
this.scene.addChild(this.collada2);
this.collada2.scale = 0.65;
this.scene.addChild(this.collada3);
this.collada3.scale = 0.7;
addChild(this.screenButton);
trace("#Materials of Model: " + this.collada1.materials.materialsByName);
var _loc_2:* = this.getColladaDisplayObj(this.collada1);
return;
}

private function activated(event:Event) : void
{
if (event.type == "activity")
{
this.webcam.camera.removeEventListener(ActivityEvent.ACTIVITY, this.activated);
this.startTimer();
}
return;
}

private function moveObjectByDetectedData(event:ObjectDetectorEvent, root:Collada) : void
{
var count:int;
var e:* = event;
var collObj:* = root;
if (e.rects)
{
this.graph.lineStyle(2, 128);
count;
e.rects.forEach(function (root:Rectangle, root:int, root:Array) : void
{
if (showRectangle)
{
graph.drawRect(root.x * scaleFactor, root.y * scaleFactor, root.width * scaleFactor, root.height * scaleFactor);
}
HeadCenterX = 640 - (root.width / 2 + root.x) * scaleFactor;
HeadCenterY = (root.height / 2 + root.y) * scaleFactor;
collObj.x = (-sceneWidth) / 2 + HeadCenterX;
collObj.y = sceneHeight / 2 - HeadCenterY - 0;
collObj.rotationY = 0 + (70 - HeadCenterX / stage.width * 140);
return;
}
);
}
return;
}

private function addCollada() : void
{
this.mat = new BitmapFileMaterial("models/images/texture0.jpg");
this.mat.addEventListener(Event.COMPLETE, this.bitmapLoadedAssignTransparency);
this.mp = new MaterialsList();
this.mp.addMaterial(this.mat, "material_0_8");
this.collada1 = new Collada("models/glasses-kai.dae", null, 1, true);
this.collada2 = new Collada("models/glasses-kai-black.dae", null, 1, true);
this.collada3 = new Collada("models/glasses-kai-round.dae", null, 1, true);
this.collada1.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, this.handleColladaComplete);
return;
}

public function getColladaDisplayObj(activity:DisplayObject3D) : DisplayObject3D
{
var returnObj:DisplayObject3D;
var childname:String;
var tempCO:* = activity;
try
{
childname = String(tempCO.childrenList()).substring(0, String(tempCO.childrenList()).indexOf(": "));
trace("childname: " + childname);
returnObj = tempCO.getChildByName(childname);
trace("found colladaNode: " + childname);
}
catch (errObject:Error)
{
trace(errObject.message);
}
return returnObj;
}

}
}

  1. // License: GPL - http://www.gnu.org/licenses/#GPL
  2.  
  3. // If you use the code, you can say thank you by linking to the new free math project www.echteinfach.tv from one of your websites, cheers
  4.  
  5.  
  6. package  {
  7.  
  8.     import Main.*;
  9.     import flash.display.*;
  10.     import flash.events.*;
  11.     import flash.geom.*;
  12.     import flash.media.*;
  13.     import flash.text.*;
  14.     import flash.utils.*;
  15.     import jp.maaash.ObjectDetection.*;
  16.     import org.papervision3d.cameras.*;
  17.     import org.papervision3d.events.*;
  18.     import org.papervision3d.materials.*;
  19.     import org.papervision3d.materials.utils.*;
  20.     import org.papervision3d.objects.*;
  21.     import org.papervision3d.objects.parsers.*;
  22.     import org.papervision3d.render.*;
  23.     import org.papervision3d.scenes.*;
  24.     import org.papervision3d.view.*;
  25.  
  26.     public class Main extends Sprite
  27.     {
  28.         private var HeadCenterX:Number;
  29.         private var HeadCenterY:Number;
  30.         private var video:Video;
  31.         private var scene:Scene3D;
  32.         private var bre:BasicRenderEngine;
  33.         private var detectionMap:BitmapData;
  34.         private var sceneWidth:int;
  35.         private var faceRectContainer:Sprite;
  36.         private var mp:MaterialsList;
  37.         private var drawMatrix:Matrix;
  38.         private var options:ObjectDetectorOptions;
  39.         private var movingForward:Boolean = true;
  40.         private var bmpdFromVideo:BitmapData;
  41.         private var intervallToFetch:int;
  42.         private var scaleFactor:int = 4;
  43.         private var vp:Viewport3D;
  44.         private var screenButton:HoldButton;
  45.         private var collada1:Collada;
  46.         private var collada2:Collada;
  47.         private var collada3:Collada;
  48.         private var detector:ObjectDetector;
  49.         private var graph:Graphics;
  50.         private var cam:Camera3D;
  51.         private var sceneHeight:int;
  52.         private var mat:BitmapFileMaterial;
  53.         private var bmpFromVideo:Bitmap;
  54.         private var showRectangle:Boolean;
  55.         private var webcam:cameraClass;
  56.  
  57.         public function Main() : void
  58.         {
  59.             var _loc_1:TextField = null;
  60.             this.sceneWidth = 640;
  61.             this.sceneHeight = 480;
  62.             this.intervallToFetch = 67;
  63.             this.faceRectContainer = new Sprite();
  64.             this.showRectangle = false;
  65.             this.webcam = new cameraClass(this.sceneWidth, this.sceneHeight);
  66.             this.video = this.webcam.camVideo;
  67.             if (this.video == null)
  68.             {
  69.                 trace("NO WEBCAM!!");
  70.                 _loc_1 = new TextField();
  71.                 _loc_1.text = "Please CONNECT your WEBCAM and TRY AGAIN";
  72.                 _loc_1.width = 350;
  73.                 _loc_1.height = 100;
  74.                 _loc_1.x = 180;
  75.                 _loc_1.y = 220;
  76.                 _loc_1.selectable = false;
  77.                 addChild(_loc_1);
  78.             }
  79.             else
  80.             {
  81.                 this.video.addEventListener(Event.ACTIVATE, this.initWebcam);
  82.                 var _loc_2:int = 0;
  83.                 this.video.y = 0;
  84.                 this.video.x = _loc_2;
  85.                 addChild(this.video);
  86.                 this.bmpdFromVideo = new BitmapData(this.video.width, this.video.height, false);
  87.                 this.detectionMap = new BitmapData(640 / this.scaleFactor, 480 / this.scaleFactor, false, 0);
  88.                 this.drawMatrix = new Matrix(1 / this.scaleFactor, 0, 0, 1 / this.scaleFactor);
  89.                 this.initDetector();
  90.                 addChild(this.faceRectContainer);
  91.                 this.setupPV3D();
  92.                 this.addCollada();
  93.                 addEventListener(Event.ENTER_FRAME, this.loop);
  94.                 root.addEventListener(MouseEvent.CLICK, this.mouseClicked);
  95.                 this.collada1.visible = true;
  96.                 this.collada2.visible = false;
  97.                 this.collada3.visible = false;
  98.                 this.screenButton = new HoldButton();
  99.             }
  100.             return;
  101.         }
  102.  
  103.         private function initDetector() : void
  104.         {
  105.             this.detector = new ObjectDetector();
  106.             var _loc_1:* = new ObjectDetectorOptions();
  107.             _loc_1.min_size = 30;
  108.             this.detector.options = _loc_1;
  109.             this.detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, this.detectionHandler);
  110.             return;
  111.         }
  112.  
  113.         private function getAndDetectCamImage(event:Event) : void
  114.         {
  115.             this.bmpdFromVideo.draw(this.video);
  116.             this.detectionMap.draw(this.bmpdFromVideo, this.drawMatrix, null, "normal", null, true);
  117.             this.detector.detect(this.detectionMap);
  118.             return;
  119.         }
  120.  
  121.         private function startTimer() : void
  122.         {
  123.             var _loc_1:* = new Timer(this.intervallToFetch);
  124.             _loc_1.addEventListener("timer", this.getAndDetectCamImage);
  125.             _loc_1.start();
  126.             return;
  127.         }
  128.  
  129.         private function detectionHandler(event:ObjectDetectorEvent) : void
  130.         {
  131.             if (this.collada1.z > 822000)
  132.             {
  133.                 this.movingForward = false;
  134.             }
  135.             if (this.collada1.z < 15000)
  136.             {
  137.                 this.movingForward = true;
  138.             }
  139.             this.graph = this.faceRectContainer.graphics;
  140.             this.graph.clear();
  141.             if (this.collada1.visible == true)
  142.             {
  143.                 this.moveObjectByDetectedData(event, this.collada1);
  144.             }
  145.             else if (this.collada2.visible == true)
  146.             {
  147.                 this.moveObjectByDetectedData(event, this.collada2);
  148.             }
  149.             else if (this.collada3.visible == true)
  150.             {
  151.                 this.moveObjectByDetectedData(event, this.collada3);
  152.             }
  153.             return;
  154.         }
  155.  
  156.         private function setupPV3D() : void
  157.         {
  158.             this.scene = new Scene3D();
  159.             this.cam = new Camera3D();
  160.             this.cam.z = -400;
  161.             this.vp = new Viewport3D();
  162.             this.bre = new BasicRenderEngine();
  163.             addChild(this.vp);
  164.             return;
  165.         }
  166.  
  167.         private function bitmapLoadedAssignTransparency(event:Event) : void
  168.         {
  169.             this.mat.bitmap.colorTransform(this.mat.bitmap.rect, new ColorTransform(1, 1, 1, 0.5));
  170.             return;
  171.         }
  172.  
  173.         private function loop(event:Event) : void
  174.         {
  175.             this.bre.renderScene(this.scene, this.cam, this.vp);
  176.             return;
  177.         }
  178.  
  179.         private function initWebcam(event:Event) : void
  180.         {
  181.             trace("#INIT\nVideo started:", event, "\n");
  182.             this.video.removeEventListener(Event.ACTIVATE, init);
  183.             this.webcam.camera.addEventListener(ActivityEvent.ACTIVITY, this.activated);
  184.             var _loc_2:* = this.video.transform.matrix;
  185.             _loc_2.a = -1;
  186.             _loc_2.tx = this.video.width + this.video.x;
  187.             this.video.transform.matrix = _loc_2;
  188.             return;
  189.         }
  190.  
  191.         private function mouseClicked(event:Event) : void
  192.         {
  193.             if (this.collada1.visible == true)
  194.             {
  195.                 this.collada2.x = this.collada2.x;
  196.                 this.collada2.y = this.collada2.y;
  197.                 this.collada2.z = this.collada2.z;
  198.                 this.collada1.visible = false;
  199.                 this.collada2.visible = true;
  200.             }
  201.             else if (this.collada2.visible == true)
  202.             {
  203.                 this.collada1.x = this.collada2.x;
  204.                 this.collada1.y = this.collada2.y;
  205.                 this.collada1.z = this.collada2.z;
  206.                 this.collada2.visible = false;
  207.                 this.collada3.visible = true;
  208.             }
  209.             else if (this.collada3.visible == true)
  210.             {
  211.                 this.collada3.x = this.collada2.x;
  212.                 this.collada3.y = this.collada2.y;
  213.                 this.collada3.z = this.collada2.z;
  214.                 this.collada3.visible = false;
  215.                 this.collada1.visible = true;
  216.             }
  217.             return;
  218.         }
  219.  
  220.         private function handleColladaComplete(event:FileLoadEvent) : void
  221.         {
  222.             this.scene.addChild(this.collada1);
  223.             this.collada1.scale = 0.65;
  224.             this.scene.addChild(this.collada2);
  225.             this.collada2.scale = 0.65;
  226.             this.scene.addChild(this.collada3);
  227.             this.collada3.scale = 0.7;
  228.             addChild(this.screenButton);
  229.             trace("#Materials of Model: " + this.collada1.materials.materialsByName);
  230.             var _loc_2:* = this.getColladaDisplayObj(this.collada1);
  231.             return;
  232.         }
  233.  
  234.         private function activated(event:Event) : void
  235.         {
  236.             if (event.type == "activity")
  237.             {
  238.                 this.webcam.camera.removeEventListener(ActivityEvent.ACTIVITY, this.activated);
  239.                 this.startTimer();
  240.             }
  241.             return;
  242.         }
  243.  
  244.         private function moveObjectByDetectedData(event:ObjectDetectorEvent, root:Collada) : void
  245.         {
  246.             var count:int;
  247.             var e:* = event;
  248.             var collObj:* = root;
  249.             if (e.rects)
  250.             {
  251.                 this.graph.lineStyle(2, 128);
  252.                 count;
  253.                 e.rects.forEach(function (root:Rectangle, root:int, root:Array) : void
  254.             {
  255.                 if (showRectangle)
  256.                 {
  257.                     graph.drawRect(root.x * scaleFactor, root.y * scaleFactor, root.width * scaleFactor, root.height * scaleFactor);
  258.                 }
  259.                 HeadCenterX = 640 - (root.width / 2 + root.x) * scaleFactor;
  260.                 HeadCenterY = (root.height / 2 + root.y) * scaleFactor;
  261.                 collObj.x = (-sceneWidth) / 2 + HeadCenterX;
  262.                 collObj.y = sceneHeight / 2 - HeadCenterY - 0;
  263.                 collObj.rotationY = 0 + (70 - HeadCenterX / stage.width * 140);
  264.                 return;
  265.             }
  266.             );
  267.             }
  268.             return;
  269.         }
  270.  
  271.         private function addCollada() : void
  272.         {
  273.             this.mat = new BitmapFileMaterial("models/images/texture0.jpg");
  274.             this.mat.addEventListener(Event.COMPLETE, this.bitmapLoadedAssignTransparency);
  275.             this.mp = new MaterialsList();
  276.             this.mp.addMaterial(this.mat, "material_0_8");
  277.             this.collada1 = new Collada("models/glasses-kai.dae", null, 1, true);
  278.             this.collada2 = new Collada("models/glasses-kai-black.dae", null, 1, true);
  279.             this.collada3 = new Collada("models/glasses-kai-round.dae", null, 1, true);
  280.             this.collada1.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, this.handleColladaComplete);
  281.             return;
  282.         }
  283.  
  284.         public function getColladaDisplayObj(activity:DisplayObject3D) : DisplayObject3D
  285.         {
  286.             var returnObj:DisplayObject3D;
  287.             var childname:String;
  288.             var tempCO:* = activity;
  289.             try
  290.             {
  291.                 childname = String(tempCO.childrenList()).substring(0, String(tempCO.childrenList()).indexOf(": "));
  292.                 trace("childname: " + childname);
  293.                 returnObj = tempCO.getChildByName(childname);
  294.                 trace("found colladaNode: " + childname);
  295.             }
  296.             catch (errObject:Error)
  297.             {
  298.                 trace(errObject.message);
  299.             }
  300.             return returnObj;
  301.         }
  302.  
  303.     }
  304. }
  305.  
  306.  
go to heaven