Detecting Faces from Image and Video Using Python

Introduction

 
The process of detecting faces plays a major role in the process of automation and machine learning process. The face detection concept needs some process for preparing your system for the process. Here, I am using the Python programming language for detecting faces in images and videos. It requires the installation of additional packages such as OpenCV. OpenCV stands for Open Source Computer Vision. To know more about OpenCV and its installation read my article on the installation of OpenCV in python by clicking here. It helps you to install the OpenCV on your computer. In this article, we will discuss detecting the faces from the images and videos using Python programming.
 

Detecting Faces

 
Detecting faces is a process of machine learning. To apply that, we need some trained data sets and library files for that process. For this process, I am using the pre-trained data sets for the face detection process using OpenCV for this process. The file used should be saved as a RAW file in the same directory where your Python program for face detection exists. I provide the file below for your usage. After that, while coding, you need to import the OpenCV and specify the name of the file in your program.
 

Python Program for Detecting Faces In Images

 
For detecting the faces from the images, you need to ensure that that image should be clear, and it is in the same directory where the python file exists. Then you can use the source code given below by me for any further use.
 
Detecting Faces From Image And Video Using Python
  1. import cv2  
  2.   
  3. # Load the cascade  
  4. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
  5.   
  6. # Read the input image  
  7. img = cv2.imread('test.jpg')  
  8.   
  9. # Convert into grayscale  
  10. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
  11.   
  12. # Detect faces  
  13. faces = face_cascade.detectMultiScale(gray, 1.14)  
  14.   
  15. # Draw rectangle around the faces  
  16. for (x, y, w, h) in faces:  
  17.     cv2.rectangle(img, (x, y), (x + w, y + h), (25500), 2)  
  18.   
  19. # Display the output  
  20. cv2.imshow('image', img)  
  21. cv2.waitKey()  
Detecting Faces From Image And Video Using Python
 

Python Program for Detecting Face from Video

 
To detect a face from a live video, here I am using my webcam for detection. You can use existing video also for detecting the faces. Make sure that the existing video file is available in the same directory where the program is available.
 
Detecting Faces From Image And Video Using Python
 
Code for detecting a face from live video:
  1. import cv2  
  2.   
  3. # Load the cascade  
  4. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
  5.   
  6. # To capture video from webcam.   
  7. cap = cv2.VideoCapture(0)  
  8.   
  9. while True:  
  10.     # Read the frame  
  11.     _, img = cap.read()  
  12.   
  13.     # Convert to grayscale  
  14.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
  15.   
  16.     # Detect the faces  
  17.     faces = face_cascade.detectMultiScale(gray, 1.14)  
  18.   
  19.     # Draw the rectangle around each face  
  20.     for (x, y, w, h) in faces:  
  21.         cv2.rectangle(img, (x, y), (x+w, y+h), (25500), 2)  
  22.   
  23.     # Display  
  24.     cv2.imshow('Video', img)  
  25.   
  26.     # Stop if escape key is pressed  
  27.     k = cv2.waitKey(30) & 0xff  
  28.     if k==27:  
  29.         break  
  30.           
  31. # Release the VideoCapture object  
  32. cap.release()  
Detecting Faces From Image And Video Using Python
 
Code for detecting a face from an existing video:
 
Detecting Faces From Image And Video Using Python
  1. import cv2  
  2.   
  3. # Load the cascade  
  4. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
  5.   
  6. # To capture video from existing video.   
  7. cap = cv2.VideoCapture('test.mp4')  
  8.   
  9. while True:  
  10.     # Read the frame  
  11.     _, img = cap.read()  
  12.   
  13.     # Convert to grayscale  
  14.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
  15.   
  16.     # Detect the faces  
  17.     faces = face_cascade.detectMultiScale(gray, 1.14)  
  18.   
  19.     # Draw the rectangle around each face  
  20.     for (x, y, w, h) in faces:  
  21.         cv2.rectangle(img, (x, y), (x+w, y+h), (25500), 2)  
  22.   
  23.     # Display  
  24.     cv2.imshow('Video', img)  
  25.   
  26.     # Stop if escape key is pressed  
  27.     k = cv2.waitKey(30) & 0xff  
  28.     if k==27:  
  29.         break  
  30.           
  31. # Release the VideoCapture object  
  32. cap.release()  

Applications of Face Detection

 
This face detection system can be used for many purposes. In automation systems, it used to detect the face and match it for verification. Mobile phone companies use it for detecting faces using their camera to make autofocus on the people who are taking images. It helps scrutinize the people who are entering a particular area. It can also help police and criminal investigation officials to detect criminal's faces.
 

Conclusion

 
This system helps you to detect the faces of different persons. You can add the machine learning concept and train the data for the purpose of recognizing faces. I added the files below for you. Feel free to use it for any future projects.


Similar Articles