Face detection using Flash and C++, revisited

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.

Download:

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.

14 Responses to “Face detection using Flash and C++, revisited”

  1. andrew mahon Says:

    you mentioned in a previous post that c++ is not well suited to rapid prototyping, and game development. have you looked at openFrameworks. oF is a c++ framework that wraps a lot of openGL, QT, and openCV functions making it quick and easy to write effective programs (prototypes).

    check out http://openframeworks.cc/download for a copy. [actually looks to be down right now, but im sure it will return soon!] v0.5 should be released soon.

    best
    -a

  2. Marijn Says:

    Thanks andrew, very interesting. I agree with you, a framework like that is probably also suitable.

  3. sosFree Says:

    hi, is ther a version on mac os x for the server ?

  4. joao Says:

    hi Marijn.

    tried your files and got a “Bad record received : 23

    Any idea? maybe something quite obvious to but C++ is sort of like chinese to me…

    ty and keep up the good work

    j

  5. friism Says:

    Hi Marijn, I’ve taken this a bit further, check it out:
    http://www.itu.dk/~friism/blog/?p=70

  6. Mal Says:

    That looks very cool!

    I wasn’t able to get it working here – when I ran the server, and then ran the .SWF, I got this error in the Server console…

    “Bad record received ( should be 76800 ): 17520″

    How do you start communication with the server? I can try to connect up with it using Director.

  7. Charles Says:

    Thanks to the fact that Marjin was nice and has provided his code; I think I can shed some light on the “Bad record recieved” issue. I’m pretty sure that everyone that try’s this demo will get that error. (I got it as well)

    The problem lies with the microphone and Camera authorization window that flash will pop up when you first run the swf. The swf can make a socket connection to the c++ application right away, but can only start sending images once you authorize use of the microphone and webcam.

    Looking in the c++ code, the socket server ensures that the data its recieving is always a size of 76800 bytes. Due to the fact that the connection is made right away, but no image data is sent from the swf until authorization is granted, the data will always initially be less then 76800 bytes

  8. Marco Says:

    Can any body tell us what to do with the Console.as ??
    I have set the ASFace.as as the Document class of my Flash project
    but dont know what to do with the Console.as

  9. Jens Fischer Says:

    You guys should check out the open source library “Marilena”, which allows you to do the face detection directly in Flash… Might not be as smooth as a C++ application, but it works… Check out a very basic example here: http://www.artofrawr.com/2009/01/28/face-tracking-in-flash/

  10. iphone video converter Says:

    Great informative post!

  11. Jon Martin Says:

    I know this was some time ago now, but cool work, i’m looking at crossing it into unity3d until they finally get the webcam functionality sorted, I originally tried sending images through javascript base64 but it was to slow and limited, so i’m now seeing if this is a fast and viable route and working on the integration, but if this is a sucess will keep you posted and share the code. Thanks for the head start!

  12. euhxhkb Says:

    7zuKt8 vvixyvxrwoee, [url=http://yrxvveppsffq.com/]yrxvveppsffq[/url], [link=http://gowmzhgmvllo.com/]gowmzhgmvllo[/link], http://ywiamqnpyzgz.com/

  13. fvuaxavjbz Says:

    J9QXAc lzeexdzrdysm, [url=http://aadpqniwlvbv.com/]aadpqniwlvbv[/url], [link=http://bfqtynmkphnp.com/]bfqtynmkphnp[/link], http://zhogwjcmxfmx.com/

  14. fvuaxavjbz Says:

    emailemailJ9QXAc lzeexdzrdysm, [url=http://aadpqniwlvbv.com/]aadpqniwlvbv[/url], [link=http://bfqtynmkphnp.com/]bfqtynmkphnp[/link], http://zhogwjcmxfmx.com/

Leave a Reply