Links

Data Artist

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

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

Since the next 360|Flex is fast approaching, I thought I'd post the full recording of my session at the last 360|Flex in Seattle. You can do whatever you want with this video, embed it wherever, watch it wherever, you can even download the original mp4 video file. Hopefully the embedded…

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.

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

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.

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.

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!