Skip to main content

Face Detection And Recognition With Azure Cognitive Services

 This article is a hands-on tutorial to create a face detection and recognition system using Azure Cognitive Services. Face Detection and Recognition are non-trivial computer vision problems and are solved with a machine learning approach. There are multitudes of libraries that offer the solution for these problems. Howsoever, with Azure Cognitive Services, it becomes a lot easier.  

Face Detection

Face detection as the name suggests in a technology using which human faces can be detected in images, videos, and other digital forms. This has huge implications for systems from airports to shopping malls. Its applications and usage are only limited to human imagination.  

Face Recognition

Face Recognition is the capability of a system to recognize and distinguish a specific person through matching the digital image or video from the database of faces. From usage for security to unlock phones to identify people in social media platforms like Facebook, face recognition has huge implications.  

Azure Cognitive Services

Azure Cognitive Services enables organizations to build cognitive intelligence for applications with client library SDKs and REST APIs. Azure Cognitive Services allows developers to integrate cognitive features into applications with no prior knowledge of Machine Learning, Data Science, and Artificial Intelligence skillset. From Computer Vision to Natural Language Processing and Conversational AI, Azure Cognitive Services enables all kinds of applications.  

Python 

Python is one of the easiest and widely used programming languages across the globe, 

  • Taught as a beginning programming language to students 
  • Clear syntax facilitates, ease of understanding and code indentation 
  • Active communities of libraries and modules developers 

Anaconda 

Anaconda is a distribution for scientific computing which is an easy-to-install free package manager and environment manager and has a collection of over 720 open-source packages offering free community support for R and Python programming languages. It supports Windows, Linux, and Mac OS and also ships with Jupyter Notebook. 

Jupyter Notebook 

Jupyter Notebook is an amalgamation of an IDE and also an educational tool for presentation which is used extensively and widely mostly for programming for scientific computing. 

Today, we’ll learn to develop a face detection and recognition system using Azure Cognitive Services.  The tutorial is programmed using Python in Jupyter Notebook with Anaconda Environment. To check out the codes and dependencies for your setup, kindly visit Github.

Create a Cognitive Service in Azure

Step 1

This would be important for the features in Azure Cognitive Service such as Face Service will use AI algorithms to process the data we supply and perform the calculations as we require.  

To Create the Cognitive Service, check out the previous article How to Create a Cognitive Service in Azure? 

Step 2

Obtain Key 1 and Endpoint from the running Azure Cognitive Service to replace in the following code for cog_key and cog_endpoint respectively.  

Step 3

cog_key = 'Key1 value'
cog_endpoint = 'Endpoint value eg. https://ufywyageibndlkfmslsfdabnkmasfd.cognitiveservices.azure.com/'
print('Ready to use cognitive services at {} using key {}'.format(cog_endpoint, cog_key))
Python

Step 4 - Face Detection

Since the Cognitive Services is now running, Face Service is provided by the Azure Cognitive Service for our usage.  

from azure.cognitiveservices.vision.face
import FaceClient
from msrest.authentication
import CognitiveServicesCredentials
from python_code
import faces
import os % matplotlib inline
# Create a face detection client.
face_client = FaceClient(cog_endpoint, CognitiveServicesCredentials(cog_key))
# Open an image
image_path = os.path.join('data', 'face', 'a.jpg')
image_stream = open(image_path, "rb")
# Detect faces
detected_faces = face_client.face.detect_with_stream(image = image_stream)
# Display the faces(code in python_code / faces.py)
faces.show_faces(image_path, detected_faces)
Python

Step 5

Every face that is detected is assigned a unique ID by the system. We can check the Unique IDs for different faces detected in the image as follows,

# Open an image
image_path = os.path.join('data', 'face', 'a.jpg')
image_stream = open(image_path, "rb")
# Detect faces
detected_faces = face_client.face.detect_with_stream(image = image_stream)
# Display the faces(code in python_code / faces.py)
faces.show_faces(image_path, detected_faces, show_id = True)
Python

Step 6 - Emotion Evaluator

Multitudes of facial attributes can be analyzed with the features provided by Cognitive Service. Various emotional states from the facial features and expressions can be analyzed.  

# Open an image
image_path = os.path.join('data', 'face', 'b1.jpg')
image_stream = open(image_path, "rb")
# Detect faces and specified facial attributes
attributes = ['age', 'emotion']
detected_faces = face_client.face.detect_with_stream(image = image_stream, return_face_attributes = attributes)
# Display the faces and attributes(code in python_code / faces.py)
faces.show_face_attributes(image_path, detected_faces)
Python

Step 7 - Face Recognition

Similar to the functionalities provided in Facebook and Google Photos, where one person once recognized, other images with the same person are automatically detected. This can be done by allocating a unique id to one person’s face and when another image has face with similar features, the unique id helps detect new face and compare to the previously detected faces, thus finding the match.

# Get the ID of the first face in image 1
image_1_path = os.path.join('data', 'face', 'store_cam3.jpg')
image_1_stream = open(image_1_path, "rb")
image_1_faces = face_client.face.detect_with_stream(image = image_1_stream)
face_1 = image_1_faces[0]
# Get the face IDs in a second image
image_2_path = os.path.join('data', 'face', 'store_cam2.jpg')
image_2_stream = open(image_2_path, "rb")
image_2_faces = face_client.face.detect_with_stream(image = image_2_stream)
image_2_face_ids = list(map(lambda face: face.face_id, image_2_faces))
# Find faces in image 2 that are similar to the one in image 1
similar_faces = face_client.face.find_similar(face_id = face_1.face_id, face_ids = image_2_face_ids)
# Show the face in image 1, and similar faces in image 2(code in python_code / face.py)
faces.show_similar_faces(image_1_path, face_1, image_2_path, image_2_faces, similar_faces)
Python

Here, obtaining the unique id for the first image and comparing the facial features of all the people from the second image, the system has efficiently recognized the same individual.  

Thus, in this article, we learned about face detection and face recognition, and using azure cognitive services a hands-on tutorial for the face detection and face recognition system was developed. We learned how to use the keys and endpoints from Azure in our system in jupyter notebook. Face detection from image, unique Id allocation for each different detected face, analyzing the emotion of the individual and even face recognition using similar features from different images were shown. This tutorial shows how powerful Azure Cognitive Services is and recognizing its role to enable developers, engineer and organizations to build, develop and deploy AI capable system is paramount. From novice developers without any knowledge of domains of Artificial Intelligence to seasoned engineers, anyone can benefit from the services Azure provides. 

Comments

Popular posts from this blog

What is Anaconda? Downloading and Installing

In this article we are going to learn the following - What is Anaconda? How to Download Anaconda? And, Installing and setting up Anaconda environment.

Guide to Spyder IDE for Machine Learning

Spyder , that stands for S cientific Py thon D evelopment E nvi r onment is very popular among engineers, scientists, and data analysts for machine learning and data science purpose. It’s powerful scientific environment is written in Python and is also meat for Python development. It supports editing, debugging, analysis and profiling functionality for an amazing too for data science purpose. It has built-in popular libraries like Numpy, Pandas, SciPy, IPython, SymPy, QtCOnsole, Matplotlib and many more. Installation If you have not downloaded Spyder, then you must read (***) that will help you set up Machine Learning Environment. Alternatively, you can also watch the video for the same. Major Components of Spyder IDE