Symmetri Developer Blog

March 27, 2008

Webcam fun with Flash

General, Flash/Flex, Algorithms - By Shourov Bhattacharya

We are well on the way to putting together a prototype of a motion-controlled environment in Flash using the webcam. The thinking is to capture video from the webcam, and then periodically call a server backend that uses AForge.NET for image processing and AI, and then present data back to the Flash client that can then be used for UI interaction or something similar. draw.logic blog has a decent little rundown of webcam capture and simple motion detection using Actionscript, and Mr doob has a nice example of an arty 3D transform of webcam content. There are a few other examples out there, but no one really seems to have taken the bull by the horns and tried to harness the image content in real time to do something more useful. There are challenges of course, but the possibilities are exciting. If we can build even a simple feedback system that is robust and has adequate accuracy, the applications will surely follow.

March 12, 2008

Untyped variables in AS3

Flash/Flex - By Shourov Bhattacharya

Actionscript 3 is typed, but not strongly typed - meaning that you can initialize variables without specifying type and have them implicitly cast at runtime. It’s a well established fact that this is not good programming practice, as it creates ambiguity in code and bypasses type checking, which is an important layer of bug detection. However I have come across a situation where keeping the type ambiguous is not only helpful but positively necessary. The scenario is a button on a Flash form; we want the button to run some code when it is either clicked or activated from the keyboard (by tabbing and then enter). We set up the event listeners like so:

this.btn.addEventListener(MouseEvent.CLICK, this.doClickAction);
this.btn.addEventListener(KeyboardEvent.KEY_DOWN, this.doClickAction);

Now, it’s when we implement the doClickAction() function that we run into a quandary. The mouse click event brings with it an event variable of type MouseEvent; the key press an event variable of type KeyboardEvent. Which should we use in our function signature? Well, if you don’t feel queasy about it, don’t specify the type of the input parameter at all:

public function doClickAction(event):void
{
// blah blah blah
}

Problem solved. Sloppy? Maybe. Useful? Absolutely. I suppose the more correct way might be to use two different functions for the two listeners. But that gets old pretty fast, believe me.

March 10, 2008

3D Sound Visualiser

Flash/Flex - By Shourov Bhattacharya

Sandy 3D and the new SoundMixer.computeSpectrum function can be combined to create some interesting visualisations. As a first effort, I have simply laid out the spectrum in a rough spiral shape and then displaced each element by a combination of amplitude and its first derivative, and colour coded by smoothed amplitude. Click on the movie to stop and start the music.



March 2, 2008

PHP timezones example

PHP - By Simon Hutchison

———————

PHP timezones example
written by Simon Hutchison - http://www.econfirm.com.au/

———————
date_default_timezone_set("Australia/Sydney")
Sun, 02 Mar 2008 14:57:11 +1100
date_default_timezone_set("Australia/Perth")
Sun, 02 Mar 2008 12:57:11 +0900

———————
The comments in this example assumes server is Sydney time.
———————

Demonstrating that timezone is not important for mktime(), date(”U”) and time(). Get GMT 0. Uses the server time to adjust to GMT 0 so need to ensure server time is correct for these functions to be accurate.

date_default_timezone_set("Australia/Perth")
mktime() : 1204430231
date("u") : 1204430231
time() : 1204430231

date_default_timezone_set("Australia/Sydney")
mktime() : 1204430231
date("u") : 1204430231
time() : 1204430231

———————

Now that we know this we can use these functions to compare to other GMT 0 adjusted times.

Demonstrating that timezone is critical for date(”date format string”) and strtotime(”date string”) because it takes a user string from anywhere in the world!!

date_default_timezone_set("Australia/Sydney")

The next values of time() and strtotime will be very close (within a second) because both the default timezone and the server time is Sydney.

strtotime(): 1204426991
time() : 1204430231 just after the strtotime converted date/time
date("r") : Sun, 02 Mar 2008 14:57:11 +1100x
date_default_timezone_set("Australia/Perth")

The next values of time() and strtotime will be about two hours apart because the default timezone is Perth and the server time is Sydney.

strtotime(): 1204434191
time() : 1204430231 about two hours before the strtotime converted date/time for Perth time. (Remember the server is in Sydney)
date("r") : Sun, 02 Mar 2008 12:57:11 +0900

This means that mktime() can be used to compare any normalised (strtotime with timezone set) time Normalise all incoming user dates using strtotime with date_default_timezone_set set correctly.

———————

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph