![]()
Please update your flash player
Update
Ok, you can now grab the MotionTracker source code (AS2 & AS3). Version 2 eventually will include the other methods for detecting and tracking motion which I mentioned. For now I have just included code for the technique used in the demo.
For those of you without access to a webcam (and as an example of a practical use for this class) here is a short video demonstrating the program I wrote for the installation piece.
End of update
Webcam required (obviously…)
I’m currently working on putting up a show, part of which will be a live generative piece, constructed from the movement of visitors in the gallery space. So of course I needed a method for not only detecting but tracking motion. I’ve seen some of Grant Skinner and Mario Klingemann’s incredible experiments in this field, the most accurate of which used a coloured object in the camera’s field of vision (such as a ball or glove) to track movement, however I wanted the live response of the artwork to be a surprise to the viewer, and so asking them to hold a coloured object would have detracted from the overall impact.
If you ever want to be amazed by the human mind, try recreating the tasks that it performs, particularly those which we take for granted; you will quickly realise the incredible amount of computation which must be happening just to, for example, detect movement and track an object, or to discern more than one object and their relative motions, velocities and distances. This isn’t an essay on Neuroscience though so I’ll save that for another time…
Anyway; I researched the concept of motion tracking, and made some notes on my own ideas. A few possible approaches emerged.
One of the most attractive ideas was to divide the screen into a grid, and average the colours within each segment at regular intervals. The difference in brightness within each segment (from one calculation to the next) would determine whether or not motion had occurred in this area of the screen. Using this data, clusters of segments with a high rate of change could be used to assume an object in motion. This approach is quite accurate, however it would require a lot of getPixel and setPixel, as well as converting these results into Hue, Saturation, Brightness objects; all of which would be quite processor heavy. A decent computer could handle this fairly easily, however I had to also consider the processing load of the actual generative work running over the top.
The method I used in the end was much lighter, and providing the parameters could be adjusted to suit the environment, it would yield enough accuracy to provide a quite responsive tracking point for the artwork to follow.
Here’s how it works
- After the camera object is set up, it is passed to a MotionTracker class I wrote, which extends the Point object. The MotionTracker class contains two BitmapData objects; one representing the current data from the webcam, the other is used to store the previous frame.
- When the update method is called, the new frame is drawn on top of the previous using the difference filter.
- The result then has a ColorMatrixFilter applied to it, increasing the contrast of the image and dropping the darker areas (with less movement) further into the background.
- A blur filter is then applied in order to further reduce noise and to form blobs from areas where more movement is occurring.
- The threshold method is then called in the resulting BitmapData, effectively causing all near black pixels to be ignored and all light pixels (where movement has been detected) to be set to a certain colour.
So that takes care of the motion detection, but what about the tracking?
- First we call the getColorBoundsRect method on the BitmapData object, which gives us a Rectangle object representing the area occupied by pixels of the colour we used when applying the threshold filter.
- If the rectangle has an area larger than a specified constant, then sufficient movement is occurring and it can be tracked (choosing to ignore small areas of movement helps to eradicate noise caused by background and other interferences).
- We then find the centre point of this rectangle, and then move the x and y position of the MotionTracker instance to these coordinates, applying some gentle easing to give more continuity to the movement.
I will post some source code once the MotionTracker class has been refined, and once I have build a nicer API so that, amongst other things, you can create your own UI for tweaking parameters at runtime; a useful feature if you have requirements such as mine where getting the most accuracy from a particular environment is important.
58 Responses to AS3 webcam Motion Tracking
J.Toolenaar
Jun 6th 2008
7:29 pm
Great stuff, I want to go experiment with it right away. So cant wait for some code :d.
keep it comming
Liam
Jun 10th 2008
10:47 am
So where is this show going to be? I’d like to see it. Interactive installations is a direction I’d like to head in - just don’t know where to start.
Soulwire
Jun 11th 2008
1:03 am
Hi Liam,
There’s 2 shows actually, one on the south coast (Bournemouth) and the other in London. I’m new to installations myself, though I’ll be learning Max/Msp/Jitter over summer to try and make myself more available for VJing and other live performances. Processing is also a good tool to know, though I think I’ll get more of what I need out of VVVV, though thats just a personal prefference.
Knowing where to start is always the hard part ;) There are a lot of great forums for this kind of thing, and I’ve recently read a few books which are at the very least inspiring, if not technically useful.
I’ll post the code next week once the first show opens and I’ve got the time to clean it up; I’d love to know about any use you might find for it.
vonramsey
Jun 11th 2008
9:21 am
This is amazing, can you post more information how it works with more details? Thanks!
This is a guru stuff, you can make so many thinks with it!
vonramsey
Jun 11th 2008
9:22 am
How much should it cost?
Soulwire
Jun 11th 2008
9:36 am
Hi Vonramsey,
I’ll post the code next week mate, all commented of course. I mentioned some other methods of achieving the same thing; maybe I’ll include them in the class too, if you need accuracy over speed.
Cost? It won’t cost you anything!
vonramsey
Jun 11th 2008
9:57 am
This is majestic to see such a guru stuff! What about the license when you post the code next week?
pault107
Jun 15th 2008
6:59 pm
This sounds interesting, I’d love to see it in action but I don’t have a webcam. Any chance of posting up a video?
KURT
Jun 16th 2008
10:28 am
You have to see it in action. It works realy good.
BennyB
Jun 16th 2008
3:40 pm
Is it possible to make this one in as2?
MikeM
Jun 19th 2008
12:21 pm
Hey this is realy cooool! Respect budy
Soulwire
Jun 21st 2008
11:02 am
Hi all,
I’ve been using this code now in a live generative piece, and it works well. I’ll see about uploading a video of that in action for those of you without a webcam.
BennyB; it is possible to do this in AS2. I’ll include an AS2 version in the download.
BennyB
Jun 21st 2008
11:55 am
Wow! Can’t wait. Thanks brother.
BennyB
Jun 26th 2008
7:18 am
Is it possible to make the motion (black one) like a mask? Or something like a combination with other animations that can follow the motion? Thanks.
m@
Jun 27th 2008
6:19 am
Okay, this code is ideal for a fun little personal project I am working on. I can’t wait for it to be posted! You’ll be my hero for doing so.
Soulwire
Jun 28th 2008
1:00 pm
Looking forward to hearing about the project! I had to use my webcam in the installation, it comes down on monday so I’ll tidy up the code then and release it. Sorry for the delay, it’s been mentally busy recently :)
BennyB
Jun 28th 2008
1:15 pm
Thanks again! how about the posibilities and combinations with other animations?
Soulwire
Jun 28th 2008
5:01 pm
Hi Benny. Yeah, this was in fact the original purpose of it; the motion tracking was used to influence the movement of a particle system, which in turn powered a visualisation. I’ll upload a video of the use I found for it when I add post the code. I’m sure it has many possible applications.
BennyB
Jul 1st 2008
12:25 pm
Hi,
Thanks for your reply, i see you are realy busy. Hope you can post more about your work!
Beebs
Jul 2nd 2008
7:29 am
Too bad I can’t see how it looks, but if the code is available please let me know.
Sounds exiciting!
vonramsey
Jul 2nd 2008
1:16 pm
Did i miss something?
BennyB
Jul 3rd 2008
10:35 am
When will you upload your stuff-Code-Video?
Soulwire
Jul 3rd 2008
7:15 pm
Alright chaps and chapettes, the code and demo video is now up. Sorry for the wait!
Beebs
Jul 4th 2008
2:31 am
I think you are super duper cool and generous and kind and amazing.
OMG, this opens new things that I always want to do.
Thanks a lot for sharing the code, and thanks to share the video how it looks.
Respect to you.
BennyB
Jul 4th 2008
7:22 am
Thanks a lot! I’m waiting for as2 and the other stuffs too :( Anyway your the man, thanks again.
BennyB
Jul 4th 2008
7:25 am
Stay in touch, we are watching you ;)
vonramsey
Jul 5th 2008
11:54 am
Waiting for the info and as2 too
VonRamsey
Jul 10th 2008
7:35 am
Hi there, can you please upload as2 version and more informations? I will learn how to do this, if you help-teach me. I have waiting so long for the as2 & info, i know you are busy :(
Soulwire
Jul 12th 2008
4:23 pm
Hi Benny and Vonramsey. I’ve been away in Gambia for the last 8 days, so sorry the source was rushed and that I didn’t have time to code up the AS2 version. I’ll post it asap though!
And Beebs, you’re welcome - glad it could be useful to you.
VonRamsey
Jul 14th 2008
7:45 am
Thank you for your help i realy appreciate it. I wait for new posts of you. Thank you again!
dbis interactive limited - dbis interactive communication blog » Motion Tracking in Flash
Jul 15th 2008
8:06 am
[...] is another reference of Flash based motion tracking. This entry is filed under Uncategorized. You can follow any [...]
phil wulf
Jul 18th 2008
7:55 am
Hi Windle, Great stuff. Especially like the random organic “Soul Wires” at the top of your page. Can your motion capture do multiple object tracking? I am making an interactive wall for a mall and I need a program that will allow for multiple tracking of people. I was thinking schools of fish swarming the objects, or maybe huge Banyan trees growing from people’s shadows when they stand still and twirling leaves when they walk by. The Soul Wires sped up would be cool too. Do you have any code that would facilitate this project? Would you have time to create something like this? If so, please approximate a cost range. Thank You. Great work.
Phil Wulf, Hawaii
VonRamsey
Jul 18th 2008
12:22 pm
SoulWire can do it for sure! Hope Soul finished the as2.
Soulwire
Jul 18th 2008
2:08 pm
Hi Phil,
Multiple object tracking is a different kettle of fish altogether. As you can see from the demo, this technique uses all of the image data coming in from the camera and averages it into the assumed motion of an object. If you were to track multiple objects, you would have to track each blob, which could be problematic as one object (say a person walking in front of the camera) may be comprised of multiple blobs. http://muonics.net/site_docs/work.php?id=41 is a good example of multiple object tracking, built using C++ openframeworks library - http://www.openframeworks.cc/, though it uses motion sensors, which lend themselves more to this kind of project, the webcam is a kind of poor man’s motion sensor in this instance!
That said, I have been thinking about techniques for multi-object tracking. The easiest and most accurate way would be to track coloured objects. The best way to do this would be for someone to hold an object of a specific colour, but one way of dealing with this autonomously would be to:
The first hurdle would be making sure light and shadow doesn’t adversely affect the colour tracking, which could simply be done by allowing a specific change in brightness / hue to take place.
The other problem would be that overlapping objects of similar colour could cause the motion tracker to make the wrong guess as to which object is which.
If you want to get in touch via the contact page and let me know your budget we can talk more about the project. In terms of adapting this script to track multiple objects, that is something I am keen to work on and will implement when I get the time. Mega busy at the moment!
Pitra
Jul 19th 2008
2:32 pm
wow.. it works great. love to experiment with it. thank you for the source code.
i made a quick test combining your class with flint particles class.
check it at: http://client.strategocorp.com/webcam/tes03.html
VonRamsey
Jul 19th 2008
3:22 pm
Nice but works little bit to slow imo! How about some info how you do this?
Grandy
Jul 22nd 2008
9:39 am
Hi Windle, you have realy Great skills. I’m a big newbie in Flash (i started with actionSkript2) and i will thank you for the source and for this web site with many Great informations and cool stuffs. I read that you are mega busy, I will not take your time when can you add the AS2 Version of the tracker, like i said i’m a newbie and don’t know AS3 (maybe it is easy but i will start to learn it after i learned AS2). Is there a way to make a textfield of the tracking area what gives us the x and y numbers in realtime? Maybe the area in coordinates? I just wonder if is realize-able? Thanks onemoretime. Keep the good works coming.
Golfie
Jul 22nd 2008
11:50 pm
Faaaarking great stuff dude
Grandy
Jul 30th 2008
9:29 am
Are you with us? Sorry if i bother you but i’m gone die for wondering of as2 and more infos.
Evan Grant
Jul 30th 2008
4:45 pm
Hey loving the work. We’ve just done a motion track project using OpenFrameWorks at Glastonbury http://www.vimeo.com/1315243. Be great to find out more about what you’re up to and see if we can collaborate?
Soulwire
Aug 9th 2008
6:37 pm
Just to let you know the AS2 version is now included in the download. Sorry for the wait.
It’s a little bit of a grubby conversion, but only in so far as I’ve hacked the demo to work in AS2, the MotionTracker class itself works and is coded almost identically to the AS3 version.
Vonramsey
Aug 12th 2008
4:56 pm
Thanks a lot. For the as2 version. You r the man. i will disturb you ;)
John Irving
Aug 16th 2008
4:44 pm
If been thinking that a lot of your examples here could be used for automated visualisations in clubs. I guess that all you would need to do is take in an audio signal (like you do for the motion capture script here) and process that through the flash player then send the output of the computer (laptop) screen to a projector. This would make for some easy-to-use visualisations (pre-prepared of course) which could have a simple time component and allow for fading from one visualisation to another after an elected period of time, or even just a keystroke which would start the fade manually.
Another use i’ve thought of for this motion capture code (as you described in your comment of July 18th at 2.08pm) could be to use a simple light/dark comparison of the captured image to adjust settings in the visualisations. Using it this way you could point the webcam at the crowd in the club and allow the changes in light and dark which occur due to changes in the dynamic lighting setup in the venue to adjust the visualisations settings, maybe not by much, but depending on the visualisation, this could be a nice effect.
Alternatively, you could have the webcam pointing at the DJ or VJ allowing them to manually adjust some of the visualisations settings themselves, maybe the fading i was talking about earlier or any other settings based on an X/Y axis which is the position of the image capture blob.
There are so many possibilities here that the imagination is the only true limiting factor.
Nice work mate. :)
Condor
Aug 20th 2008
1:18 pm
http://ssandler.wordpress.com/MTmini/ ???
Soulwire
Aug 20th 2008
2:25 pm
Awesome! That would work great. :)
Grandy
Aug 20th 2008
3:46 pm
Thanks for the as2, but i can’t use the motion tracker. I tried so many things but does not work, where is the error? What is wrong?
His is a simple code where i found in the Net, i tried to trace it but does nothing :(
Simple Code:
I will only that the red pointer (tracker) folows the flowers!… :(
Soulwire
Aug 20th 2008
5:13 pm
Have you modified the code so that (a) the logic for adding flowers appears within an enterFrame or timer event so that it doesn’t need mouse movement to work and (b) it uses tracker.x and tracker.y rather than _xmouse and _ymouse?
Condor
Aug 20th 2008
5:43 pm
No i don’t modified the code :( What sould i do?
Grandy
Aug 20th 2008
5:45 pm
? CONDOR what the hack Soulwire meanig me nothing to do with you. I found the flowers stuff and can’t modifie it :(
Condor
Aug 20th 2008
5:48 pm
Ups. sorry Grany. I add the link for the multiTouch stuff
Vonramsey
Aug 24th 2008
1:00 pm
What about the v2 ? U r realy bussy i see, can’t wait for more!
Vonramsey
Aug 24th 2008
1:53 pm
This is why i respect Windle, a man who has future in the Flash scene. Thumbs up.
http://labs.grinant.com/ ;)
Soulwire
Aug 24th 2008
2:56 pm
Haha, good stuff. It’s great to see it used. Yeah, I’m so eager to get V2 out as it seems it’ll be popular and of use to people. Next time a break in work comes up I’ll pick up where I left off. Cheers for the link :)
Grandy
Aug 25th 2008
3:31 pm
And how about this one? I changed the _xmouse and _ymouse in to _target._x and _target._y but does not work? Where is the error:
Soulwire
Aug 27th 2008
9:34 am
Grandy, how are you integrating the Motion Tracker? I don’t see any refference to it… Once you have the Motion Tracker running, I presume you want to replace refferences to the mouse position with trackerInstance.x / y (no underscore). Perhaps try getting the motion tracker running first, with just a dot following to get the flavour of it, then stick in your funcky fx!
Grandy
Aug 27th 2008
5:01 pm
The Motion tracker your shared works fine, the dot what you asked works too. i used directly _target._x and _target._y to track it (as2). Gona Wild, it’s bad to be a newbie, thanks anyway. I will try it one more time :(
Grandy
Aug 28th 2008
3:09 pm
Now i get it, it works good but how can i disable the mouse listener?
Vonramsey
Aug 28th 2008
4:07 pm
Like GuruWindle sayed. Delete this :
//This is the listener that listens for mouse movements
var myListener = new Object();
Mouse.addListener(myListener);
change myListener.onMouseMove this to this.onFrameEnter after this you need no more mouseDown function.
Leave a Reply