Facial Recognition and Analysis
AI Code for Business
Facial recognition is a way of recognizing a human face through computer vision. A facial recognition system uses biometrics to map facial features from a photograph or video. Face detection technology can be applied to various fields -- including security, biometrics, law enforcement, entertainment, and personal safety -- by providing surveillance and tracking of people in real time.
Importance
Human face recognition has progressed from fundamental computer vision applications to advances in machine learning to increasingly sophisticated artificial neural networks and related technologies with continuous ongoing performance improvements. It now plays an important role as the first step in many key applications.
Benefits
The primary function of the detection algorithm is to determine if the image has a face. Such identification helps to identify age, gender and emotions using expressions, and to identify which parts of the image or video to focus on. It is a class of object detection.
Enhanced Security
Face detection improves surveillance efforts and helps track down criminals and terrorists. A facial recognition system works by mapping an individual's facial features mathematically and storing the data as a faceprint. Different systems and algorithms capture distinct parts of an image or video to create the faceprint. The archived fingerprint is then used to match real-time security footage to determine if there is a match.
Another broad application is the use of facial recognition as a key as the security is enhanced with no phyical keys/keycards to misplace or lose, or compromised/forgotten passwords. A prime example is the unlocking of your laptop and mobile phones with your face.
Efficient Shopping
While identifying and finding missing persons and criminals are arguably the most important benefits of facial recognition, they extend beyond security to convenience. Instead of making cash or credit purchases at stores, facial recognition technology can be used to recognize your face and charge the goods to your account. Use of this increased during the Covid pandemic serving both convenience and security purposes. Additionally, this approach allow businesses for a smaller ratio of staff to customers, recognise and improve service to loyalty club members, and facilitate employees cloacking in and out.
Challenges
Face detection is the most substantial function of face recognition operations. It is designed to focus all the available computational resources on the section of an image holding a face. The method of face detection in pictures is complicated because of the variability present across human faces such as pose, expression, position and orientation, skin colour, the presence of glasses or facial hair, differences in camera gain, lighting conditions, and image resolution. Recent years have brought advances in face detection using deep learning, which have proven to significantly outperform traditional computer vision methods.
Example
In this exercise, we will use the webcam to capture an image of you and perform facial feature detection. The detection and recognition will be done with the face-recognition library, a state-of-the-art face recognition built with deep learning.
Follow the steps below and run the code on the colab notebook linked here. (To run the code, click on the round ▶️ next to each cell)
Cell 1: Imports the python libraries needed.
!pip3 install face_recognition
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
import face_recognition
Cell 2: Define the function that will allow colab to access your webcam and take a picture.
def take_photo(filename='base_pic.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
Cell 3: Initializes your webcam. Allow camera access if you get a pop-up. Click on Capture to take a photo. Don't like the picture? Run this cell again. The detection model is able to detect multiple faces in the image. Be sure to try this out. (Do note that it will not work if you are using a computer that does not have a webcam. See next cell on using an existing image.)
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
display(Image(filename))
except Exception as err:
print(str(err))
Cell 4: Take the captured image, perform face detection on it, and output the detection results. (Note the code on the first line. You can upload an image and change the file name in between quotation marks to that of the uploaded image to perform the detection on said image.)
from PIL import Image, ImageDraw
image = face_recognition.load_image_file("base_pic.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
print("found {} face(s) in this photograph.".format(len(face_landmarks_list)))
landmark_image = Image.fromarray(image)
drawer = ImageDraw.Draw(landmark_image)
for landmarks in face_landmarks_list:
for facial_feature in landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, landmarks[facial_feature]))
for facial_feature in landmarks.keys():
drawer.line(landmarks[facial_feature], width=3)
Cell 5: Show the detected facial landmarks drawn on the image.
landmark_image
Cell 6: As an addendum to this example, we will make use of the Py-Feat (Python Facial Expression Analysis Toolbox) library to extract the displayed facial emotions on the image. We first install and load the library and initialize the detector (note that this may take a few minutes).
%%capture
!pip install py-feat
from feat import Detector
detector = Detector()
Cell 7: Perform detection on the image and visualize the results. (To test another image, upload this, update the file name in between the quotes, and rerun the cell.)
image_prediction = detector.detect_image("base_pic.jpg")
image_prediction.iloc[[0]].plot_detections();
Cell 8: Display the emotion scores that the py-feat model predicted.
image_prediction.iloc[[0]].emotions()
Conclusion
Continued advances in computer vision technology has broaden the use cases for human face detection and recognition. Furthermore, the field is seeing significant performance gains with the impovements brought on by machine learning. In consideration of your business, what can facial recognition technology do for you?
Check out the other articles to see more applications and related code on maching learning. If you need support and would like to find out more, get in touch with the contact link.