How to use MTCNN face detection to monitor your background

Imagine your boss walks in while you're watching that funny cat video at work. Can AI solve this problem? Yes! You can implement face recognition to monitor your background.

This article is about using a webcam to monitor your background while you are doing something secret things on your PC or laptop. We use MTCNN face detection module in python to fulfill our purpose.

By using face detection we can create events like on more than one face found, on no any face found. If you would like to learn more about MTCNN Face Detection, you can check our other article, MTCNN Face Detection and Matching using Facenet Tensorflow. 


What are the goals ?

  • Use opencv to capture images from webcam
  • Use imutils to resize the image
  • Detect face using MTCNN face detect module from Facenet
  • Use pyautogui for keyboard simulation for hotkey

Logic in Brief:

I have used opencv to open webcam and capture camera frames in infinite loop. I have used 0.1 second of sleep for low CPU usages. I first use one frame and use face detect module from facenet to detect multiple faces. MTCNN has great face detection accuracy so that it will work on real case environment but with enough light.

Switch Desktop

When it detect one face then it will be obviously me working there, if someone is approaching to me that it will get two faces at the frame and it will trigger an event (ctrl + win + right) which is switch to right desktop. This works for Windows 10 but it required at least two virtual desktop enabled. You can create a new  virtual desktop by going to TaskView (left taskbar). The program will monitor the faces and keeps the new desktop if it finds a single face again then it will switch back to left desktop by triggering hotkey (ctrl + win + left). It is a simple logic you can create your own logic.

Lock Screen

When it found no any faces for 3 seconds then it will trigger an event for lock screen to enable. I use ctypes in python to communicate with windows external dll library to call lock screen function in user32.dll . 

Face Recognition

Currently i am not using face recognition because it is not required for above events but can be implemented by calculating face embedding and embeds distance or classifying. But it will use more CPU for continuous usages. 

Where is the CODE ?

You can get the code here.

Be the first to comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.