From 793b65e6781350b5a896ffbf99ee0158b6bb0db4 Mon Sep 17 00:00:00 2001 From: "skyzorg@gmail.com" Date: Fri, 23 Dec 2011 03:24:35 +0000 Subject: [PATCH] delete --- flex/oculus_grabber/oculus/OculusImage.as | 461 ---------------------- flex/oculus_grabber/oculus_grabber.mxml | 316 --------------- 2 files changed, 777 deletions(-) delete mode 100755 flex/oculus_grabber/oculus/OculusImage.as delete mode 100755 flex/oculus_grabber/oculus_grabber.mxml diff --git a/flex/oculus_grabber/oculus/OculusImage.as b/flex/oculus_grabber/oculus/OculusImage.as deleted file mode 100755 index 0001f6b..0000000 --- a/flex/oculus_grabber/oculus/OculusImage.as +++ /dev/null @@ -1,461 +0,0 @@ -package oculus -{ - // import flash.display.BitmapData; - import flash.utils.ByteArray; - - public class OculusImage - { - private var parr:Array = []; // working pixels, whole image, 8-bit greyscale - private var width:int; - private var height:int; - public var lastThreshhold:int; - private var threshholdMult:Number; // = 0.65; - private var lastBlobRatio:Number; - private var lastTopRatio:Number; - private var lastBottomRatio:Number; - private var lastMidRatio:Number; - private var parrorig:Array; - private var imgaverage:int; - - public function OculusImage() - { - } - - public function dockSettings(str:String):void { - var a:Array = str.split("_"); - lastBlobRatio = a[0]; - lastTopRatio = a[1]; - lastMidRatio = a[2]; - lastBottomRatio = a[3]; - } - - public function convertToGrey(pixelRGB:ByteArray):void { - // uses 30-59-11 RGB weighting from: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale - var p:int; - parr = []; - var n:int = 0; - var runningttl:int = 0; - for (var i:int=0; i < pixelRGB.length; i+=4) { - p = pixelRGB[i+1]*0.3 + pixelRGB[i+2]*0.59 + pixelRGB[i+3]*0.11 ; - parr[n]=p; - n++; - runningttl += p; - } - imgaverage = runningttl/n; - threshholdMult = 0.65 - 0.2 + (0.40*( imgaverage/255)); - } - - private function floodFill(ablob:Array, start:int):Array { - // from http://en.wikipedia.org/wiki/Flood_fill - var q:Array = [start]; - var blob:Array = []; - var n:int; - var w:int; - var e:int; - var i:int; - while (q.length > 0) { - n = q.pop(); - if (ablob[n]) { - w = n; - e = n; - while (ablob[w]) { w-=1; } - while (ablob[e]) { e+=1; } - for (i=w+1; i<=e-1; i++) { - ablob[i]=false; - blob[i]=true; - if (ablob[i-width]) { q.push(i-width); } - if (ablob[i+width]) { q.push(i+width); } - } - } - } - return blob; - } - - public function findBlobStart(x:int, y:int, w:int, h:int, bar:ByteArray):Array { // calibrate only... - lastThreshhold = 0; - var r:Array; - findBlobStartSub(x,y,w,h,bar); - r = findBlobStartSub(x,y,w,h,bar); // do it again, with contrast averaged - return r; - } - - public function findBlobStartSub(x:int, y:int, w:int, h:int, bar:ByteArray):Array { // calibrate sub - width = w; - height = h; - convertToGrey(bar); - parrorig = parr.slice(); // save original image for re-threshholding after - var start:int = x + y*width; - var result:Array = [0,0,0,0,0]; - - var startavg:int = (parr[start-1]+parr[start]+parr[start+1])/3; //includes 2 adjacent pixels in contract threshhold to counteract grainyness a bit - var threshhold:int = startavg*threshholdMult; - // var threshhold:int = parr[start]*threshholdMult; - - if (lastThreshhold !=0) { - threshhold = lastThreshhold; - } - var i:int; - for (i=0;ithreshhold) { parr[i]=true; } - else { parr[i]=false; } - } - var blob:Array = floodFill(parr, start); - if (blob.length>1) { // error, its always greater than 1!! should be 'contains at least one TRUE'? or just remove condition - var r:Array = getRect(blob,start); - var minx:int = r[0]; - var maxx:int = r[1]; - var miny:int = r[2]; - var maxy:int = r[3]; - var blobSize:int = r[4]; - var blobBox:int = (maxx-minx)*(maxy-miny); - /* - lastTopRatio = getPixelEqTrueCount(blob, minx, maxx, miny, miny+(maxy-miny)*0.333) / blobBox; - lastBottomRatio = getPixelEqTrueCount(blob, minx, maxx, miny+(maxy-miny)*0.666, maxy) / blobBox; - lastMidRatio = getPixelEqTrueCount(blob,minx,maxx,miny+(maxy-miny)*0.333, miny+(maxy-miny)*0.666) / blobBox; - */ - lastTopRatio = getPixelEqTrueCount(blob, minx, minx+(maxx-minx)*0.333, miny, maxy) / blobBox; // left - lastMidRatio = getPixelEqTrueCount(blob,minx+(maxx-minx)*0.333,minx+(maxx-minx)*0.666, miny, maxy) / blobBox; - lastBottomRatio = getPixelEqTrueCount(blob, minx+(maxx-minx)*0.666, maxx, miny, maxy) / blobBox; // left - lastBlobRatio = (maxx-minx)/(maxy-miny); - var slope:Number = getBottomSlope(blob,minx,maxx,miny,maxy); - //result = x,y,width,height,,slope,lastBlobRatio,lastTopRatio,lastMidRatio,lastBottomRatio - result = [minx,miny,maxx-minx,maxy-miny,slope,lastBlobRatio,lastTopRatio,lastMidRatio,lastBottomRatio]; - } - if (lastThreshhold==0) { - var runningttl:int = 0; - for (i=0; i<=width*height; i++) { // zero to end - if (blob[i]) { - runningttl += parrorig[i]; - } - } - lastThreshhold = (runningttl/blobSize)*threshholdMult; // adaptive threshhold - } - return result; - } - - public function getThreshholdxy(x:int, y:int, w:int, h:int, bar:ByteArray):Array { // unused - width = w; - height = h; - convertToGrey(bar); - var start:int = x + y*width; - var result:Array = [0,0,0,0,0]; - var startavg:int = (parr[start-1]+parr[start]+parr[start+1])/3; //includes 2 adjacent pixels in contract threshhold to counteract grainyness a bit - var threshhold:int = startavg*threshholdMult; - lastThreshhold = threshhold; - var i:int; - var parrinv:Array = []; // inverse, used to check for inner black blob - for (i=0; ithreshhold) { - parr[i]=true; - parrinv[i]=false; - } - else { - parr[i]=false; - parrinv[i]=true; - } - } - var blob:Array = floodFill(parr, start); - var r:Array = getRect(blob,start); - var minx:int = r[0]; - var maxx:int = r[1]; - var miny:int = r[2]; - var maxy:int = r[3]; - var blobSize:int = r[4]; - - var xx:int = minx+((maxx-minx)/2); - var yy:int = miny+((maxy-miny)/2); - i = xx + yy*width; // dead center of winner blob - var ctrblob:Array = floodFill(parrinv,i); - var rctr:Array = getRect(ctrblob,i); - if (minxrctr[1] && minyrctr[3] && ctrblob.length > 1 && rctr[4] 255) { - dir = -1; - deleteddir = 1; - lastThreshhold = 255-n; - } - n += inc; - } - else { break; } - //trace ("attempt: "+attemptnum+" lastThreshhold: "+lastThreshhold); - attemptnum ++; - } - return result; - } - - public function findBlobsSub(bar:ByteArray, w:int, h:int):Array { - width = w; - height = h; - blobs = []; - var result:Array = [0,0,0,0,0,0]; - //var imgaverageOld:int = imgaverage; - convertToGrey(bar); - parrorig = parr.slice(); - if (lastThreshhold == 999) { lastThreshhold = imgaverage+45; } // auto contrast finding with magical constant - var threshhold:int = lastThreshhold; - var i:int; - var parrinv:Array = []; // inverse, used to check for inner black blob - for (i=0; ithreshhold) { - parr[i]=true; - parrinv[i]=false; - } - else { - parr[i]=false; - parrinv[i]=true; - } - } - //var allBlobsPixels:Array = parr.slice(); //devel only, normally use parr instead of allBlobsPixels - var blobnum:int = 0; - var maxdiff:Number = 99; - var diff:Number; - var winner:int =-1; - // var winnerBlobSize:int; - var winRect:Array; - var winnerTopRatio:Number; - var winnerBlobRatio:Number; - var winnerBottomRatio:Number; - var winnerMidRatio:Number; - var minx:int; - var miny:int; - var maxx:int; - var maxy:int; - var topRatio:Number; - var blobRatio:Number; - var bottomRatio:Number; - var midRatio:Number; - var blobSize:int; - var r:Array; - var pixel:int; - var blobs:Array = [] - var blobBox:int; - var blobstarts:Array = []; - for (pixel=0; pixel<=width*height; pixel++) { // zero to end, find all blobs - if (parr[pixel]) { // finds a white one >> production uses parr[pixel] - blobs[blobnum] = floodFill(parr, pixel); - blobstarts[blobnum]=pixel; - blobnum++; - } - } - var rejectedBlobs:Array = []; - while (rejectedBlobs.length < blobs.length) { - for (blobnum=0; blobnum 150) { // discard tiny blobs - minx = r[0]; - maxx = r[1]; - miny = r[2]; - maxy = r[3]; - blobBox = (maxx-minx)*(maxy-miny); - topRatio = getPixelEqTrueCount(blobs[blobnum], minx, minx+(maxx-minx)*0.333, miny, maxy) / blobBox; // left - midRatio = getPixelEqTrueCount(blobs[blobnum],minx+(maxx-minx)*0.333,minx+(maxx-minx)*0.666, miny, maxy) / blobBox; - bottomRatio = getPixelEqTrueCount(blobs[blobnum], minx+(maxx-minx)*0.666, maxx, miny, maxy) / blobBox; // left - blobRatio = (maxx-minx)/(maxy-miny); - diff = Math.abs(topRatio - lastTopRatio) + Math.abs(bottomRatio- lastBottomRatio) + Math.abs(midRatio- lastMidRatio); - if (diff < maxdiff) { // && diff < 1.1 && blobRatio < lastBlobRatio * 1.2 && blobRatio > lastBlobRatio * 0.5) { - //if (diff < maxdiff && blobRatio < lastBlobRatio * 1.1) { - winner=blobnum; - // winnerBlobSize = r[4]; - maxdiff = diff; - winRect = r.slice(); - winnerTopRatio = topRatio; - winnerBottomRatio = bottomRatio; - winnerMidRatio = midRatio; - winnerBlobRatio = blobRatio; - } - } //size condition end bracket - } - } - if (winner == -1) { break; } - else { // best looking blob chosen, now check if it has ctr blob - minx = winRect[0]; - maxx = winRect[1]; - miny = winRect[2]; - maxy = winRect[3]; - var x:int = minx+((maxx-minx)/2); - var y:int = miny+((maxy-miny)/2); - i = x + y*width; // dead center of winner blob - var ctrblob:Array = floodFill(parrinv,i); - r = getRect(ctrblob,i); - if (minxr[1] && minyr[3] && ctrblob.length > 1 && r[4] maxx) { maxx = tempx; } - if (tempy < miny) { miny = tempy; } - if (tempy > maxy) { maxy = tempy; } - size++; - } - } - var result:Array=[minx,maxx,miny,maxy,size]; - return result; - } - - private function getPixelEqTrueCount(blob:Array,startx:int,endx:int,starty:int,endy:int):int { - var result:int = 0; - for (var yy:int = starty; yy=minx+miny*width; i-=1) { - if (blob[i]) { - start = i; - break; - } - } - var starty:int = start/width; - var startx:int = start-(starty*width); - var direction:int = 1; - if (startx > minx+(maxx-minx)/2) { direction = -1; } - if (direction == -1) { - while (blob[start+1]) { start ++; } - } - else { - while (blob[start-1]) { start -= 1; } - } - var end:int = start; - while (blob[end + direction] || blob[end-width+direction]) { //crawl up diagonally or flat until hit vert wall - end += direction; - if (!blob[end]) { end -= width; } - } - var endy:int = end/width; - var endx:int = end-(endy*width); - //trace("slope function: "+startx+" "+starty+" "+endx+" "+endy+" "+start); - return (endy-starty)/(endx-startx); - } - - public function edges(argb:ByteArray, width:int, height:int):void { - convertToGrey(argb); - var lastparr:Array = parr.slice(); - var n:int=0; - for each (var pixel:int in lastparr) { - var darkest:int = parr[n]; - var lightest:int = darkest; - //var closesteight:Array = [n-width-1, n-width, n-width+1, n-1, n+1, n+width-1, n+width, n+width+1]; - var closesteight:Array = [n-width, n-1, n+1, n+width]; - for each (var i:int in closesteight) { - if (( i >=0 && i <= lastparr.length ) && !((n as Number)/(width as Number) == n/width && i == n+1) - && !(((n-1) as Number)/(width as Number) == n/width && i == n-1)) { - if (lastparr[i] < darkest) { - darkest = lastparr[i]; - } - if (lastparr[i] > lightest) { - lightest = lastparr[i] - } - } - } - if (lightest - darkest > 30) { - parr[n] = true; - } - else { parr[n] = false; } - n ++; - } - } - - public function processedImage():ByteArray { - var newPixels:ByteArray = new ByteArray(); - var n:int = 0; - var p:int; - for each (var i:Boolean in parr) { - if (i) { p=255; } - else { p=0; } - newPixels[n]=255; - newPixels[n+1]=p; - newPixels[n+2]=p; - newPixels[n+3]=p; - n+=4; - } - return newPixels; - } - - } -} \ No newline at end of file diff --git a/flex/oculus_grabber/oculus_grabber.mxml b/flex/oculus_grabber/oculus_grabber.mxml deleted file mode 100755 index 42f353e..0000000 --- a/flex/oculus_grabber/oculus_grabber.mxml +++ /dev/null @@ -1,316 +0,0 @@ - - - - - - - - - import mx.controls.Alert; - import mx.graphics.codec.JPEGEncoder; - - import oculus.OculusImage; - import flash.media.VideoStreamSettings; - - private var nc:NetConnection; - private var ns1:NetStream; - private var camera:Camera = Camera.getCamera(); - private var mic:Microphone = Microphone.getMicrophone(); - //private var mic:Microphone = Microphone.getEnhancedMicrophone(); //new - private var connected:Boolean = false; - private var ns2:NetStream; - private var video:Video = new Video(); - - private var videoFramegrab:Video; - private var dockFramegrab:Video; - private var camattachtimer:Number; - private var playdelaytimer:Number; - private var ocls:OculusImage = new OculusImage(); - private var rtmpPort:String; - private var playing:Boolean = false; - private var h264Settings:H264VideoStreamSettings = new H264VideoStreamSettings(); -// private var file:FileReference = new FileReference(); - - private function init():void - { - //Alert.show("ready!"); - if (!camera) { - Alert.show("Camera not found"); - } - ExternalInterface.addCallback("flashCallServer", callServer); // called from Javascript - ExternalInterface.addCallback("playlocal",playlocal); // called from Javascript - ExternalInterface.addCallback("connect", connect); //called from Javascript - ExternalInterface.addCallback("setRtmpPort", setRtmpPort); //called from Javascript - ExternalInterface.addCallback("sizeChanged", sizeChanged); //called from Javascript - ExternalInterface.call("flashloaded"); - - // stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenRedraw); - - if (mic) { - mic.codec = SoundCodec.SPEEX; - mic.encodeQuality = 5; - mic.setUseEchoSuppression(true); - -// var options:MicrophoneEnhancedOptions = new MicrophoneEnhancedOptions(); -// options.echoPath = 128; -// options.mode = MicrophoneEnhancedMode.FULL_DUPLEX; -// options.nonLinearProcessing = true; -// mic.enhancedOptions = options; - - mic.setSilenceLevel(0); -// mic.rate = 16; - - } - else { Alert.show("Microphone not found"); } - - sizeChanged(videoDisplay.width, videoDisplay.height); - - h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_2); - - } - - private function setRtmpPort(str:String):void { - rtmpPort = str; - //Alert.show("port: "+str); - } - - private function connect(mode:String):void - { - nc = new NetConnection(); - var urlarray:Array = ExternalInterface.call("window.location.href.toString").split("/"); -// var address:String = "rtmp://"+ExternalInterface.call("window.location.hostname.toString")+"/"+urlarray[3]; - var address:String = "rtmp://"+ExternalInterface.call("window.location.hostname.toString")+":"+rtmpPort+"/"+urlarray[3]; - - nc.connect(address,""); - //Alert.show(address); - nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); - nc.client = this; - nc.addEventListener("publish",publish); - nc.addEventListener("play",play); - nc.addEventListener("message",message); - nc.addEventListener("framegrab",framegrab); - nc.addEventListener("dockgrab",dockgrab); - nc.addEventListener("muteROVMic",muteROVMic); - nc.addEventListener("unmuteROVMic",unmuteROVMic); - nc.call("grabbersignin",null,mode); - // ns1 = new NetStream(nc); // doesn't work here... too soon or something? - } - - private function netStatusHandler(e:NetStatusEvent):void { - var code:String = e.info.code; - if(code == "NetConnection.Connect.Success") { - connected = true; -// video = new Video(); -// ns2 = new NetStream(nc); - } - if(code == "NetConnection.Connect.Closed") { - message("shutdown",""); - } - } - - public function publish(mode:String,width:int, height:int, fps:int, quality:int):void - { - if (connected) { - try - { - if (ns1) { - ns1.attachCamera(null); // newly added, not sure if helps - ns1.attachAudio(null); // newly added, not sure if helps - ns1.close(); - videoFramegrab.attachCamera(null); - videoFramegrab.clear(); - dockFramegrab.attachCamera(null); - dockFramegrab.clear(); - } - if (mode != "stop") { - camera.setMode(width,height,fps); - camera.setQuality(0,quality); - ns1 = new NetStream(nc); - ns1.videoStreamSettings = h264Settings; - - //var n:int = 1; - if (mode == "camera" || mode == "camandmic") { ns1.attachCamera(camera); } - if (mode == "mic" || mode == "camandmic") { ns1.attachAudio(mic); } - ns1.publish("mp4:stream1", 'live'); - //playdelaytimer = setInterval(play,2000,n); - dockFramegrab = new Video(320,240); - videoFramegrab = new Video(width, height); - camattachtimer = setInterval(camAttach,200); - // videoFramegrab.attachCamera(camera); - } - callServer("streammode",mode); - } - catch(err:Error) - { - Alert.show( err.toString() ); - } - } - } - - private function camAttach():void { - clearInterval(camattachtimer); - videoFramegrab.attachCamera(camera); - dockFramegrab.attachCamera(camera); - } - - - public function play(nostreams:int):void { - if (connected) { - if (ns2) { ns2.close(); } - if (video) { video.clear(); } - if (nostreams==0) { - message("player stream stopped",null); - eyeball.visible = true; - playing=false; - } - else { - ns2 = new NetStream(nc); - video = new Video(); - - video.attachNetStream(ns2); - video.width = videoDisplay.width; - video.height = videoDisplay.height; - - videoDisplay.addChild(video); - - ns2.play("mp4:stream2"); -// ns2.play("blob.flv") - message("playing player stream",null); - eyeball.visible = false; - playing = true; - } - } - } - - public function muteROVMic():void { - if (mic && ns1) { - ns1.attachAudio(null); // trying this instead - } - } - - public function unmuteROVMic():void { - if (mic && ns1) { - ns1.attachAudio(mic); - } - } - - public function playlocal():void { // called via init.html only - eyeball.visible = false; - camera.setMode(320,240,15); - camera.setQuality(0,95); - ns1 = new NetStream(nc); - ns1.attachCamera(camera); - ns1.publish("mp4:stream1", 'live'); - - ns2 = new NetStream(nc); - video = new Video(); - - video.attachNetStream(ns2); - video.width = 400; - video.height = 300; - videoDisplay.addChild(video); - ns2.play("mp4:stream1"); - message("showing streaming video",null); - } - - private function callServer(fn:String, str:String):void { - nc.call("grabberCallServer", null, fn, str); - } - - public function message(str:String, value:String):void { - if (str == "docksettings") { - str = ""; - ocls.dockSettings(value); - } - else { - ExternalInterface.call("message",str,value); - } - } - - public function dockgrab(x:int,y:int,mode:String):void { - - //get cam image - var snapshot:BitmapData = new BitmapData( dockFramegrab.width, dockFramegrab.height, false ); - var rect:Rectangle = new Rectangle(0,0,dockFramegrab.width, dockFramegrab.height); - var results:Array; - var filter:BlurFilter = new BlurFilter(2,2); - var pt:Point = new Point(0, 0); - - snapshot.draw(dockFramegrab); - - snapshot.applyFilter(snapshot, rect, pt, filter); - if (mode=="calibrate") { // calibrate - ocls = new OculusImage(); - results = ocls.findBlobStart(x,y,dockFramegrab.width, dockFramegrab.height, snapshot.getPixels(rect)); - callServer("dockgrabbed","calibrate "+results[0]+" "+results[1]+" "+results[2]+" "+results[3]+" "+results[4].toPrecision(5)+" "+ - +results[5].toPrecision(5)+" "+results[6].toPrecision(5)+" "+results[7].toPrecision(5)+" "+results[8].toPrecision(5)); - // results = x,y,width,height,slope,lastBlobRatio,lastTopRatio,lastMidRatio,lastBottomRatio - } - - if (mode=="start") { - ocls.lastThreshhold = 999; - mode = "find"; - } - if (mode=="find") { - results = ocls.findBlobs(snapshot.getPixels(rect),dockFramegrab.width, dockFramegrab.height); - callServer("dockgrabbed","find "+results[0]+" "+results[1]+" "+results[2]+" "+results[3]+" "+results[4].toPrecision(5)); - // results = x,y,width,height,slope - } - - } - - // regular version - public function framegrab():void { - try { - var snapshot:BitmapData = new BitmapData(videoFramegrab.width, videoFramegrab.height, false); - snapshot.draw(videoFramegrab); - var jpg:JPEGEncoder = new JPEGEncoder(); - var jpgBytes:ByteArray = jpg.encode(snapshot); - nc.call("frameGrabbed", null, jpgBytes); -// file.save(jpgBytes,'frame.jpg'); - } catch (err:Error) { Alert.show( err.toString() ); } - } - - public function sizeChanged(w:int, h:int):void { - videoDisplay.width = w; - videoDisplay.height =h; - video.width = w; - video.height = h; - video.x = 0; - video.y = 0; - eyeball.width = w; - eyeball.height = h; - eyeball.x = 0; - eyeball.y = 0; - } - - /* // edge detection version - public function framegrab():void { - var snapshot:BitmapData = new BitmapData(videoFramegrab.width, videoFramegrab.height, false); - snapshot.draw(videoFramegrab); - var rect:Rectangle = new Rectangle(0,0,videoFramegrab.width, videoFramegrab.height); - - ocls.edges(snapshot.getPixels(rect), videoFramegrab.width, videoFramegrab.height); - var pp:BitmapData = new BitmapData(videoFramegrab.width, videoFramegrab.height, false); - pp.setPixels(rect,ocls.processedImage()); - - var pngEncoder:PNGEncoder = new PNGEncoder(); - var pngBytes:ByteArray = pngEncoder.encode(pp); - nc.call("frameGrabbed", null, pngBytes); - } - */ - - ]]> - - -