Simple Flex Motion Detection example

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).

This movie requires Flash Player 9.

View source

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).

UPDATE: As various people have pointed out, there is built in motion detection in the Camera class, so if you want to detect motion in a webcam feed all you have to do is monitor the activityLevel property of the Camera. I guess I should’ve known that before posting 😛 oh well. This motion detector example is still useful if you’re trying to detect motion in anything other than a webcam feed though, such as another video playing in your app, or any other UI component that has visible changes.

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).


14 thoughts on “Simple Flex Motion Detection example

  1. Amit says:

    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!

  2. 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.


  3. @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.

  4. Pingback: ?? » ?????

  5. Pingback: antiAssassin - an experiment in Flex/AIR utilization of webcams « The Saj - S.A.S.S.I.E.

  6. 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…


    (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?)

  7. 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.


  8. This is awesome! I’m so glad that you didn’t take this down after discovering the built in motion detection. I’m working on some Wireless / Wired IP software and this is exactly what I was looking for! Very nice work!

Comments are closed.