Archive for the ‘Actionscript 3.0’ Category

Face detection using Flash and C++, revisited

Thursday, March 6th, 2008

Over the past few months I’ve been asked several times for the code for face detection in Flash. So I’ve taken some time to make a short demo application, grabbed from my original Virtual Mirror game (see my two previous blogs).

Here’s what it does: Flash grabs the webcam image every 60ms, and sends it using a socket connection to a separate C++ server that runs on your localhost. The server uses the OpenCV software library to detect faces, using a haar classifier cascade. When it detects one or more faces it sends the result (coordinates) back to Flash. There, Flash draws a transparent box or image at the coordinates of the face, like this:

ActionScript3 + C++, Face detected

What makes this cool is that you can perform quite complex image analysis (at least something you can’t easily do in Flash), and use this data on a platform that is highly suitable for multimedia development (animation, sound, nice graphics, etc).

The demo only handles one face now, but this could be easily expanded.


At the moment there are no installation instructions included. If you need help you can contact me, or wait untill I create the instructions (which I will) :)
A few quick pointers to get you past some obstacles:

  • Binary: You probably have to add the folder you run the .swf from as trusted directory in your Flash settings. Otherwise you get a security / sandbox violation error.
  • Binary: Start the server before starting the .swf (client). The .swf tries to connect to the server when it starts.
  • Binary: The needed OpenCV dll’s and .xml file for face detection are included in the same folder.
  • Source: (Server) If I remember correctly, only pthread is a required dependency. UPX is a file packer for releasing, Visual Leak detector can be turned off if you don’t want to use it.
  • Source: (Server) Additional dependencies you should include when compiling/linking: wsock32.lib cv.lib cxcore.lib highgui.lib

Last note: This was both my first AS3 and C++ project, I’m always open to suggestions, improvements and cool new ideas. Also, when you find this code useful, I would appreciate it if you’d drop me a line.

AS3 font anti-aliasing in AIR and browser

Friday, August 3rd, 2007

First of all, I’m a noob on fonts in AS3. Just posting some differences between AIR (Adobe Integrated Runtime) and the browser I noticed while developing.

Font anti-aliasing is done automatically in the browser when you don’t add any effects to it like a GlowFilter. When running AIR the font is not anti-aliased:

Verdana, Not embedded, Browser
Font quality Browser

Verdana, Not embedded, AIR
Font quality AIR

If you want to do effects like alpha and rotation on fonts, you have to embed them into your SWF. This also results in anti-aliasing when you run your application as AIR file:

Verdana, Embedded, Browser
Font quality Browser (embedded font)

Verdana, Embedded, AIR
Font quality AIR (embedded font)

Though the strange thing is the 0 (zero) and o (O) are missing a bit on the top right.

Face tracking, adding virtual objects in Flash

Thursday, July 12th, 2007

In a previous blog I talked about using Flash and C++ for face detection. I’m going to use face detection to determine if the player of the game has the right distance to the webcam and is standing in the middle of the screen. But it’s also lots of fun to attach virtual objects to the face (like done in the more expensive Logitech webcam series), or to put filters and effects on the facial area.

Here is a quick example. The Jason Voorhees hockey mask moves along with my head in real time (about 60ms delay). The size of the sprite also changes as you move back and forward.

Face detection fun

Edit: Or go on a killing spree in style ;)

Face detection fun (keep your hat on)

Face detection using Flash and C++

Sunday, June 24th, 2007

As you could read in a previous blog, I’m trying to create a small webcam game in Flash that supports ‘physical input’ using a webcam. The frames are processed by a C++ server that uses existing computer vision libraries. This way it should be fairly easy to build in cool features like face detection.

Now I’ve finally managed to get some things working on my Flash / C++ computer vision project. Zoran (my project advisor at the University of Amsterdam) helped me a lot on the C++ computer vision part. Using the OpenCV library we’ve managed to put face detection in a Flash application! Input from the webcam is sent via a Socket to a light weight server running localhost. When a face is detected, a message is sent back to Flash.

The boundaries of the face in the image are also known. I just have to think of some kind of protocol to send and retrieve different kinds (and sizes) of data.

The result of the image detection from OpenCV (using the red color channel here):
CV face detection

Zinc DLL interface = slow?

Monday, April 30th, 2007

At the moment I’m working on a webcam game as part of my Master thesis research. There are many Computer Vision libraries available in C/C++ (for example with optical flow implementations or face detection), but C++ isn’t very suited for rapid prototyping in respect to interface and game graphics.

Flash is very useful for this. Also, reading the webcam input is a lot easier in Flash than in C. So, what’s the idea: Flash reads the webcam, sends the image data to C++. Then the C++ application sends back the meta information about the images / frames, which Flash uses to draw the game graphics and execute actions in the game.

My plan was to use Zinc. With Zinc you can convert a .swf file (Flash) to a .exe (Executable). Zinc is actually a container like a browser, using an integrated Flash Player. What’s nice about Zinc is that you can shape the window of your application like you want, and they provide additional API functionality. With the extra API calls you can access low level system commands like rebooting the PC, or information like the CPU speed. It also provides database connectivity with MySQL and Access.

Using the Zinc API you can also access external DLL’s. So, what I tried to do was use this DLL access to analyse the webcam data in an external C++ application that uses existing Computer Vision libraries.
To test the DLL connection, I wrote tiny test DLL (source). It consists of two functies, Init and Process. Init is a pure dummy function, Process returns a small test string.

Calling the external DLL in Flash (AS3):

// Create the Bitmap
var bmd:BitmapData = new BitmapData(30, 30, false);
var rect:Rectangle = new Rectangle(0, 0, 30, 30);
var bytes:ByteArray = bmd.getPixels(rect);

// DLL
var myDLL:DLL = new DLL("TestDLL.dll");
parameterIndex = myDLL.addParameter("integer", "120");
parameterIndex = myDLL.addParameter("integer", "120");"none", "Init");

// Send data to DLL
parameterIndex = myDLL.addParameter("unsigned char*", bytes);
var result:String ="unsigned char*", "Process");

And we’re done! You would say… Unfortunately, some time profiling shows even the Init dummy function takes 30ms to execute. Sending a 120×120 bitmap takes 180ms. As 10FPS can be considered a minimum you want for analysis, we can conclude this is too slow to be useful.
The next and final attempt (because of time constraints) is to use a C++ server and use a Socket to communicate between Flash and the server.