Here’s a simple motion detection utility that you can use to do motion detection on a webcam, or video, or any other component in your Flex application. It uses a bitmap technique to compare image snapshots and calculate the number of pixels that were changed between snapshots. This gives you a percentage of the total pixels that were changed, which is a crude way to figure out motion. Basically what you do is draw one frame onto the previous frame using the “difference” blend mode. Then you threshold the image and you can figure out the number of pixels that had any change.
See the full example after the jump.
The example below uses the motion detector and ties it to a webcam. You’ll have to allow the app to access your webcam to see it in action. If the motion in the webcam exceeds a certain amount (20%), then it takes a snapshot and sends it to the CIA (ok, ok, it doesn’t really do that, it doesn’t send the snapshot anywhere).
Someone emailed me recently asking about this code, since it was shown in a previous video I created when I was working on TileUI a while back. This is the first in a series of chunks of code that I’m thinking about taking from that project and releasing open source. No, I don’t think I’ll be releasing all of TileUI, but just some of the components within it (like the radial menu component maybe).
activityLevel property of the Camera. I guess I should’ve known that before posting P.S. I’ve been very quiet on the blogging front. This is because I’ve been super crazy mega busy between my job and writing Flex for Dummies, which is available for pre-order on Amazon. I expect to get back to blogging much more sometime after May 13 (after I sleep for a week).
















Entries (RSS)
April 3rd, 2008 at 11:55 pm
Will it throw an error if it cant find a Webcam? Sounds really interesting though
April 4th, 2008 at 1:22 am
It doesn’t work in the dark. What? No night vision?
April 4th, 2008 at 1:33 am
is it possible to write face detection scripts using as3?
April 4th, 2008 at 2:20 am
Good idea to create a component like that, and great work! Thanks!
April 4th, 2008 at 5:26 am
it doesn’t work, i’m on a ppc mac 10.4.11, firefox, flash player 9.0.115.. - i really wanted to see it working!
April 4th, 2008 at 10:01 am
This is good as a learning demo. For anyone interested in implementing this in a project, it’s worth noting that this functionality is already built into ActionScript, via the camera.activityLevel property, which returns a value from 0-100 indicating the amount of motion in the video stream.
Cheers.
April 4th, 2008 at 10:07 am
@Grant - yeah, I actually didn’t know about Camera.activityLevel (but someone else emailed me about 5 minutes after I posted this to tell me).
So yes, the activityLevel property of the Camera class will do this exact same thing. You can still use this method for detecting motion in other, non webcam stuff. So you could detect motion in a FLV video, or even in a Flex chart or any UI component that has visible changes.
April 4th, 2008 at 6:17 pm
[…] http://dougmccune.com/blog/2008/04/03/simple-flex-motion-detection-example/ […]
April 8th, 2008 at 1:09 pm
[…] I created this tool as a little experiment in utilization of the webcam and motion detection. I was first made aware of this possibility by Doug McCune’s “TiileUI” experiment. (Who in fact used a different methodology which he has now posted to his blog.) […]
April 8th, 2008 at 1:39 pm
Hi Doug,
Yeah sorry about that… (being the one who inquired of Doug about the motion detection). I wound up writing a small app using activityLevel. And was going to send it to you after I posted it up to my blog.
So well, here it is, a day later…
http://thesaj.wordpress.com/2008/04/08/antiassassin-an-experiment-in-flexair-utilization-of-webcams/
(BTW, for whatever reason, the AIR file fails to download. I had to encapsulate it within a .zip file. Not sure why, though I believe it to be a server setting. Any one have any ideas?)
April 17th, 2008 at 11:37 pm
Quick question, is there anyway (using AIR) to detected (mouse, keyboard activity) and take snap shot of the desktop?
April 25th, 2008 at 12:38 pm
Doug, this is nice and I keep learning knew stuff, being new to Flex. Question: can the images you put in the tile list be saved to the harddrive instead? I wanted to motion capture my doggy door while at work, seeing what the dogs and cats do. I wanted to save the files and look at them later. To be honest, the idea popped into my head while looking at this app and I have not looked into it at all, just being lazy.
Nice apps you’ve done and I will be using some of them in the near future.
Gary