Automated mugshots with Python and OpenCV

Tags: programming, software, projects

Published on
« Previous post: — Next post: Synchronous multiplexing for sockets … »

One of my most favourite books, Cryptonomicon by Neal Stephenson contains a scene in which one of the protagonists, Randy, is letting other people use a demo on his laptop in a business meeting. Since Randy does not know all the people in the meeting he figures it would be a good idea to clandestinely collect their mugshots by writing a program that captures all the different faces appearing in front of his webcam.

I always wanted to do this because it sounds like a very fun idea. I sometimes leave my laptop open and lock it when I am on a conference and know that I shall be shortly returning to my seat. It was always a pet peeve of mine to know whether other people were trying to unlock the laptop…

So, with the power of Python and OpenCV, years after reading Cryptonomicon for the first time, I finally wrote my own version of Randy’s program. This turned out to be surprisingly easy. It is a testament to the versatility of Python (and its libraries of course) that this problem can be solved by a really short program.

You can download the script from its GitHub repository. Further improvements may include (in the hopes that I get around to do this):

  • Recognizing and categorizing different faces
  • Taking mugshots when certain actions happen (a git hook could trigger this, for example)
  • Investigating different methods of face recognition
  • Detecting basic emotions

The script is dead simple to use. Just call



./ --hide

in case you do not want to get visual feedback about the recognized faces. uses a simple logic to not fill about its working directory with useless faces: If either the number of faces changes between two frames or more than 10 seconds passed between the current face detection and the previous one, a new mugshot will be taken. The mugshots are named using the current time, with a suffix in case multiple faces are detected.

Have fun!