| 1 |
// |
|---|
| 2 |
// Project Marilena |
|---|
| 3 |
// Object Detection in Actionscript3 |
|---|
| 4 |
// based on OpenCV (Open Computer Vision Library) Object Detection |
|---|
| 5 |
// |
|---|
| 6 |
// Copyright (C) 2008, Masakazu OHTSUKA (mash), all rights reserved. |
|---|
| 7 |
// contact o.masakazu(at)gmail.com |
|---|
| 8 |
// |
|---|
| 9 |
// Redistribution and use in source and binary forms, with or without modification, |
|---|
| 10 |
// are permitted provided that the following conditions are met: |
|---|
| 11 |
// |
|---|
| 12 |
// * Redistribution's of source code must retain the above copyright notice, |
|---|
| 13 |
// this list of conditions and the following disclaimer. |
|---|
| 14 |
// |
|---|
| 15 |
// * Redistribution's in binary form must reproduce the above copyright notice, |
|---|
| 16 |
// this list of conditions and the following disclaimer in the documentation |
|---|
| 17 |
// and/or other materials provided with the distribution. |
|---|
| 18 |
// |
|---|
| 19 |
// This software is provided by the copyright holders and contributors "as is" and |
|---|
| 20 |
// any express or implied warranties, including, but not limited to, the implied |
|---|
| 21 |
// warranties of merchantability and fitness for a particular purpose are disclaimed. |
|---|
| 22 |
// In no event shall the Intel Corporation or contributors be liable for any direct, |
|---|
| 23 |
// indirect, incidental, special, exemplary, or consequential damages |
|---|
| 24 |
// (including, but not limited to, procurement of substitute goods or services; |
|---|
| 25 |
// loss of use, data, or profits; or business interruption) however caused |
|---|
| 26 |
// and on any theory of liability, whether in contract, strict liability, |
|---|
| 27 |
// or tort (including negligence or otherwise) arising in any way out of |
|---|
| 28 |
// the use of this software, even if advised of the possibility of such damage. |
|---|
| 29 |
// |
|---|
| 30 |
package jp.maaash.ObjectDetection |
|---|
| 31 |
{ |
|---|
| 32 |
import flash.events.EventDispatcher; |
|---|
| 33 |
import flash.events.Event; |
|---|
| 34 |
import jp.maaash.net.ZipLoader; |
|---|
| 35 |
|
|---|
| 36 |
public class HaarCascadeLoader extends EventDispatcher { |
|---|
| 37 |
private var debug :Boolean = false; |
|---|
| 38 |
|
|---|
| 39 |
private var ziploader :ZipLoader = new ZipLoader; |
|---|
| 40 |
public var cascade :HaarCascade = new HaarCascade; |
|---|
| 41 |
public function HaarCascadeLoader( url :String ) { |
|---|
| 42 |
ziploader.url = url; |
|---|
| 43 |
ziploader.addEventListener(Event.COMPLETE,function(e:Event):void{ |
|---|
| 44 |
logger("[Event.COMPLETE]e: "+e); |
|---|
| 45 |
ziploader.removeEventListener(Event.COMPLETE,arguments.callee); |
|---|
| 46 |
decodeHaarCascadeXML( new XML(ziploader.getContentAsString()) ); |
|---|
| 47 |
dispatchEvent( new Event(Event.COMPLETE) ); |
|---|
| 48 |
}); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
public function load():void{ |
|---|
| 52 |
ziploader.load(); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
private function decodeHaarCascadeXML(x:XML):void{ |
|---|
| 56 |
//logger("[decodeHaarCascadeXML]x: ",x); |
|---|
| 57 |
var size:String = x.children()[0].size; |
|---|
| 58 |
cascade.base_window_w = size.split(" ")[0]; |
|---|
| 59 |
cascade.base_window_h = size.split(" ")[1]; |
|---|
| 60 |
|
|---|
| 61 |
var stages:XML = x.children()[0].stages[0]; |
|---|
| 62 |
var stage_nums:int = stages._.length(); |
|---|
| 63 |
logger("stage_nums: ",stage_nums); |
|---|
| 64 |
|
|---|
| 65 |
var stagexml :XML; |
|---|
| 66 |
var treexml :XML; |
|---|
| 67 |
var tree :FeatureTree; |
|---|
| 68 |
var feature_nums :int; |
|---|
| 69 |
var featurexml :XML; |
|---|
| 70 |
var rects :XML; |
|---|
| 71 |
var rectnums :int; |
|---|
| 72 |
var rect1 :HaarRect; |
|---|
| 73 |
var rect2 :HaarRect; |
|---|
| 74 |
var rect3 :HaarRect; |
|---|
| 75 |
var feature :FeatureBase; |
|---|
| 76 |
for( var i:int=0; i<stage_nums; i++ ){ // trees |
|---|
| 77 |
stagexml = stages._[i]; |
|---|
| 78 |
treexml = stagexml.trees[0]; |
|---|
| 79 |
tree = new FeatureTree; |
|---|
| 80 |
tree.stage_threshold = stagexml.stage_threshold[0]; |
|---|
| 81 |
feature_nums = treexml._.length(); |
|---|
| 82 |
logger("feature_nums: ",feature_nums); |
|---|
| 83 |
for( var j:int=0; j<feature_nums; j++ ){ |
|---|
| 84 |
featurexml = treexml._[j]._[0]; |
|---|
| 85 |
rects = featurexml.feature[0].rects[0]; |
|---|
| 86 |
rectnums = rects._.length(); |
|---|
| 87 |
rect1 = new HaarRect(rects._[0]); |
|---|
| 88 |
rect2 = new HaarRect(rects._[1]); |
|---|
| 89 |
switch(rectnums){ |
|---|
| 90 |
case 2: |
|---|
| 91 |
feature = new Feature2Rects(featurexml.tilted,featurexml.threshold[0],featurexml.left_val[0],featurexml.right_val[0]); |
|---|
| 92 |
feature.setRect(rect1,0); |
|---|
| 93 |
feature.setRect(rect2,1); |
|---|
| 94 |
break; |
|---|
| 95 |
case 3: |
|---|
| 96 |
feature = new Feature3Rects(featurexml.tilted,featurexml.threshold[0],featurexml.left_val[0],featurexml.right_val[0]); |
|---|
| 97 |
feature.setRect(rect1,0); |
|---|
| 98 |
feature.setRect(rect2,1); |
|---|
| 99 |
rect3 = new HaarRect(rects._[2]); |
|---|
| 100 |
feature.setRect(rect3,2); |
|---|
| 101 |
break; |
|---|
| 102 |
} |
|---|
| 103 |
tree.features.push(feature); |
|---|
| 104 |
} |
|---|
| 105 |
cascade.trees.push(tree); |
|---|
| 106 |
} |
|---|
| 107 |
logger("trees: ",cascade.trees); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
private function logger(... args):void{ |
|---|
| 111 |
if(!debug){ return; } |
|---|
| 112 |
log(["[HaarCascadeLoader]"+args.shift()].concat(args)); |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|