Face Recognition in Python
Face detection is a process of identifying human faces in images or videos. It is a rapidly expanding area of computer vision that offers a variety of useful applications, such as security systems, face identification, and picture analysis.
In this article, we may examine the many approaches that can be used to enforce face detection in Python.
There are several ways to write a face detection code in Python, using different libraries and frameworks. Some of the most popular libraries and frameworks include:
- OpenCV: OpenCV is a powerful open-source computer vision library that can be used to detect faces in images and videos. It provides a pre-trained Cascade Classifier for face detection, which can be used out-of-the-box.
- Dlib: Dlib is a machine learning library that can be used for face detection, among other tasks. It provides a pre-trained shape predictor for face detection, which can be used to detect faces in images and videos.
- Face_recognition: face_recognition is a python library that provides an easy-to-use interface for face detection and Recognition. Modern facial recognition created with deep learning is used in the library by dlib.
- MTCNN: Multi-task Cascaded Convolutional Networks (MTCNN) is a popular face detection algorithm that can be used to detect faces in images and videos. It provides a pre-trained model that can be used out-of-the-box.
- TensorFlow: TensorFlow is a popular machine learning framework that can be used to build custom face detection models. It gives users access to a potent API for developing and implementing system mastery models.
Face Detection using the OpenCV Approach
An open-source computer recognition library called OpenCV provides a large selection of image processing and laptop vision techniques. It is widely used in industry and academia, providing developers with a simple and easy-to-use interface.
Installing the OpenCV library is required before we can begin face detection in Python.
This can be done using the following command:
Once the library is installed, we can start writing our code. The relevant modules must first be imported and read in an image as the early phase:
Next, we will use the CascadeClassifier class to detect faces in the image. This class takes in a pre-trained cascading classifier that can be used to detect faces in an image.
The classifier can be trained using a dataset of images of faces, and it uses a combination of features such as edges, shapes, and textures to detect faces.
The detect MultiScale method takes in the image and a few parameters such as the scale factor and the minimum number of neighbors.
The scale factor is used to control the size of the detection window, and the minimum number of neighbors is used to control the number of false positives.
Once the faces are detected, we can draw rectangles around them using the rectangle function:
Implementation using OpenCV
Output
Dlib method for face recognition code
- Dlib is a powerful library for machine learning and computer vision in Python. One of the features of Dlib is its ability to detect faces in images and video streams.
- The library includes a pre-trained model for face detection that can be used out of the box, making it easy to add face detection functionality to your Python projects.
- The Dlib library includes a function called "get_frontal_face_detector()," which returns a pre-trained object detector that is specifically designed for detecting faces in images.
- This detector can be used to locate faces in an image and return the coordinates of the bounding boxes for each face.
- Dlib also provides functionality for detecting facial landmarks, such as the eyes, nose, and mouth, which can be useful for applications such as facial recognition and face tracking.
Implementation of the Dlib method:-
Output
Explanation of the Dlib method :-
- "import dlib" - This line imports the Dlib library, which provides functionality for machine learning and computer vision in Python.
- "detector = dlib.get_frontal_face_detector()" - This line creates a face detector object using the "get_frontal_face_detector()" function provided by the Dlib library. This function returns a pre-trained object detector specifically designed for detecting faces in images.
- "img = dlib.load_rgb_image("image.jpg")" - This line loads an image named "image.jpg" using the Dlib library's "load_rgb_image" function, which loads an image from the specified file path and returns it as an array of RGB values.
- "faces = detector(img)" - This line uses the face detector object to detect faces in the image by calling the detector function on the image. The detector function returns a list of "rect" objects, each representing a bounding box for a detected face in the image.
- "print("Number of faces detected: ", len(faces))" - This line prints the number of faces detected in the image using the "len(faces)" function, which returns the number of elements in the list of "rect" objects.
- "for face in faces:" - This line starts a loop that iterates through all the "rect" objects in the list of faces returned by the detector function.
- "print("Left: ", face.left())" - This line, inside the loop, prints the left coordinate of the bounding box of the current face using the "left()" method of the "rect" object.
- "print("Top: ", face.top())" - This line, inside the loop, prints the top coordinate of the bounding box of the current face using the "top()" method of the "rect" object.
- "print("Right: ", face.right())" - This line, inside the loop, prints the right coordinate of the bounding box of the current face using the "right()" method of the "rect" object.
- "print("Bottom: ", face.bottom())" - This line, inside the loop, prints the bottom coordinate of the bounding box of the current face using the "bottom()" method of the "rect" object.
Using Face Recognition Library
One of the most popular libraries for face recognition is the face_recognition library, which is built on top of dlib. This collection offers a wide range of methods for finding and identifying faces in still photos and moving movies.
The face_recognition library also provides other methods for face detection, such as face_landmarks() which detect facial features like eyes, nose, and mouth, and face_encodings() which encodes the face into a 128-dimensional vector.
The dlib library, a powerful package for image processing, is used in face recognition.
Implementation of the code :-
Output
Explanation for code :-
- The first line imports the face_recognition library, which detects and recognizes faces in images and videos.
- The next line loads the image from the file "image.jpg" using the face_recognition.load_image_file() method.
- The line after that detects faces in the image using the face_recognition.face_locations() method, which returns a list of face locations represented by four coordinates (top, right, bottom, left) for each face.
- The following line starts a for loop that loops through each face location in the list. Each iteration of the loop assigns the four coordinates of a face location to the variables top, right, bottom, and left.
- The next line inside the for loop uses the cv2. To create a rectangle over the face, use the rectangle() method. The picture, the top-left quadrant of the rectangle, the lower right quadrent of the rectangle, the colour of the rectangle, and the thickness of the rectangle are all inputs that the function accepts. In this case, the rectangle is red (0, 0, 255), and the thickness is 2 pixels.
- The line after that uses the cv2.imshow() method to display the image with the rectangles drawn around the faces.
- The last line uses the cv2.waitKey() method to wait for the user to press a key before closing the window. The argument against this method is the amount of time in milliseconds before the window closes. In this case, it is set to 0, which means the window will wait indefinitely for the user to press a key.
MTCCN Method for Face Recognition
MTCCN (Multi-task Cascaded Convolutional Networks) is a deep learning-based method for detecting and aligning faces in images. It is composed of three networks: the P-Net, R-Net, and O-Net.
For the purpose of identifying individuals and define and articulate in images, the P-net is a simple community.
The R-Net is a more complex network used to refine the bounding boxes and facial landmarks detected by the P-Net.
The O-Net is a network that is used to output the final bounding boxes and facial
landmarks.
- To write a code for face detection using MTCNN in Python, you would first need to install the necessary libraries, such as TensorFlow, Keras, and MTCNN.
- Then, you would need to load the pre-trained weights for the P-Net, R-Net, and O-Net.
- Next, you would need to create an instance of the MTCNN class and pass in the loaded weights.
- Finally, you would use the detect_faces() method of the MTCNN class to detect faces in an image, which would return the bounding boxes and facial landmarks for each face detected.
Implementation of the code
Output
Explanation of the code
- "from mtcnn import MTCNN" imports the MTCNN class from the mtcnn library. This class will be used to detect faces in an image.
- "import cv2" imports the OpenCV library, which will be used to read and display the image.
- "filename = "image.jpg" assigns the file name of the image that will be processed to a variable called "filename".
- "pixels = cv2.imread(filename)" reads the image file specified by the "filename" variable and assigns the pixel data to the "pixels" variable.
- "detector = MTCNN()" creates an instance of the MTCNN class and assigns it to the "detector" variable. The default weights for the P-Net, R-Net, and O-Net are used.
- "faces = detector.detect_faces(pixels)" uses the "detect_faces()" method of the MTCNN class to detect faces in the image stored in the "pixels" variable. Every recognised face is returned, and its surrounding containers and visual cues are added to the "faces" parameter.
- "for face in faces:" starts a loop that will iterate through each face detected.
- "x, y, width, height = face['box']" assigns the x and y coordinates, width, and height of the bounding box for the current face to the corresponding variables.
- "cv2.rectangle(pixels, (x, y), (x+width, y+height), (0, 0, 255), 2)" draws a red rectangle around the current face on the original image stored in the "pixels" variable.
- "cv2.imshow('Face', pixels)" displays the image with the faces outlined in red.
- "cv2.waitKey(0)" waits for the user to press a key before closing the window displaying the image. This enables the user to view the image of the individuals before the system closes.
TensorFlow Method for Face Recognition
TensorFlow is an open-source machine learning library widely used for building deep learning models. It provides various pre-built models and functions that can be easily customized to suit the specific needs of a project.
One such model is the Multi-task Cascaded Convolutional Networks (MTCNN), which we discussed in our previous method.
The process of writing a face detection code in Python using TensorFlow involves the following steps:-
- Importing the necessary libraries: The first step is to import the required libraries, such as TensorFlow, OpenCV, and Numpy.
- Loading the MTCNN model: TensorFlow provides a pre-trained MTCNN model that can be loaded into the code. This model is trained to detect faces in an image and predict the coordinates of the bounding box around the face.
- Reading the visual: The next phase is reading the picture, during which the faces needs to be located. The OpenCV library might be used to do this.
- Preprocessing a photograph:- Before the image can be submitted to the MTCNN model, it must first undergo preprocessing. This includes resizing the image to the appropriate size, converting it to a tensor, and normalizing the pixel values.
- Running the model: The preprocessed image is then passed to the MTCNN model, which will detect the faces in the image and predict the bounding box coordinates.
- Drawing the bounding box: The coordinates of the bounding box are then used to draw a rectangle around the detected face using the OpenCV library.
- Displaying the output: The final step is to display the output image with the bounding box on the screen.
Example of Tenserflow method
Output
|