Laughing Man with pyOpenCV


If you have watched Ghost In The Shell - Stand Alone Complex series, then Laughing Man should be familiar to you. In the series he is a "villain", with the trademark of having a rotating logo covering his face in all image streams. After finding out that there was OpenCV bindings for Python, I wanted to try to implement similar effect myself. This is in no way the first nor the best implementation of this, only mine.


The actual operation of the program is relatively simple. It relies on pyOpenCV to do the heavy lifting on image capture and face detection, and PIL for the image processing. The program captures a frame from the first video source on the computer (e.g., webcam), detects the largest face in it, and then draws the Laughing Man image over the detected face. Note, that the program detects only the largest face. It would be possible to detect all faces in the image, but that seems to be quite slow, at least on my computer.

This code has a couple of "extra" features added on top of the plain face detection and covering. First of all, the mask image is made of two parts: the static face, and a rotating text. The text is rotated in 5 degree steps on every frame update. Because the rotation takes a while, the created images are cached on the first run. This causes the first few seconds to be slightly slower, but after it is possible to use the cached versions, the operation should be more smooth. The second added feature is IIR-type low-pass filtering on the mask location. The detected face ROI has small changes between frames and this causes the overlaid image to shake annoyingly without the filtering. With the filtering, the image overlay reacts a bit more slowly to changes in face location, but it's also much more calm. And the third extra feature is that when no face is detected, the old location is retained and the overlay image size is just gradually decreased instead of removing the mask completely.

When running, the result should look a bit like
Laughing Man screen shot


To run the program, you'll need these files (in addition to all "normal" Python libraries): (the source code), limg.png (static image), ltext.png (rotating image), and haarcascade_frontalface_alt.xml (face detection cascade).

black dot

Valid HTML 4.01 Transitional paulus, 28.11.2010