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.
- import cv2
-
-
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
-
-
- img = cv2.imread('test.jpg')
-
-
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
-
-
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
-
-
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
-
-
- cv2.imshow('image', img)
- cv2.waitKey()
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.
Code for detecting a face from live video:
- import cv2
-
-
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
-
-
- cap = cv2.VideoCapture(0)
-
- while True:
-
- _, img = cap.read()
-
-
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
-
-
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
-
-
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
-
-
- cv2.imshow('Video', img)
-
-
- k = cv2.waitKey(30) & 0xff
- if k==27:
- break
-
-
- cap.release()
Code for detecting a face from an existing video:
- import cv2
-
-
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
-
-
- cap = cv2.VideoCapture('test.mp4')
-
- while True:
-
- _, img = cap.read()
-
-
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
-
-
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
-
-
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
-
-
- cv2.imshow('Video', img)
-
-
- k = cv2.waitKey(30) & 0xff
- if k==27:
- break
-
-
- 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.