The purpose of this repository is to provide code for a Console Application in C# that uses the Microsoft Azure Face API to add people to Person Groups and then identify people given images. Below, you can find the instructions for setting up the app and using and modifying the code for your own purposes.
You simply have to give the program an image of a person's face for it to recognize. I also have a relevant Android app repository which can be used for face identification after adding people to Person Groups in C# below.
If you have not already done so, refer to the Setup Section to setup the Console Application using your own Microsoft Face API Key and images of people.
Using the application is simple once it is setup. Simply run each of the functions in Program.cs
to do different tasks:
definePeple()
: Use this to create aCreatePersonResult
object for each person. Inside,definePeople
, the functiondetectRegisterFace()
is called to add images for each face in an image and add it to the corresponding person. IMPORTANT: It is indetectRegisterFace()
, lines 122, 139, and 151 that you must specify the directory of a folder containjpg
images of each person.trainPersonGroup()
is called to train the Person Group on the people and their images. There are no parameters or specific directories required for this function.identifyFace()
is called to identify a face. It accepts one parameter: the directory containing an image of a person. It then gets the best match of the person in the image and prints their name in the Console. If it is not able to identify the person in the image, then it doesn't display anything.
Microsoft Azure has documentation on Detecting Faces and Identifying Faces from images. Below are the steps to setup the Console App from the Program.cs
file in this repository.
- In Visual Studio create a new Console App from the .Net Framework.
- Get the required NuGet Packages -
- In the Solution Explorer, right-click on your project.
- Select "Manage NuGet Packages"
- Click the "Browse" tab and select "Include prerelease"
- Find and install this package or a similar package, (if there has been an update):
Microsoft.Azure.CognitiveServices.Vision.Face 2.2.0-preview
- After creating the project, open the
.cs
file and paste the code from this repository'sProgram.cs
into your.cs
file. Then, follow the steps below to get the Microsoft API Key, Add the API Key and Endpoint, and Specify the Directory of the Images.
- Making the Azure Account: In order to use the Face API, you must get an API Subscription Key from the Azure Portal. This page by Microsoft provides the features and capabilities of the Face API. You can create a free Azure account that doesn't expire at this link here by clicking on the "Get API Key" button and choosing the option to create an Azure account.
- Getting the Face API Key from Azure Portal:
- Once you have created your account, head to the Azure Portal. Follow these steps:
- Click on "Create a resource" on the left side of the portal.
- Underneath "Azure Marketplace", click on the "AI + Machine Learning" section.
- Now, under "Featured" you should see "Face". Click on that.
- You should now be at this page. Fill in the required information and press "Create" when done.
- Now, click on "All resources" on the left hand side of the Portal.
- Click on the name you gave the API.
- Underneath "Resource Management", click on "Manage Keys".
- Once you have created your account, head to the Azure Portal. Follow these steps:
You should now be able to see two different subscription keys that you can use. Follow the additional instructions to see how to use the API Key in the application.
In Program.cs
, on Line 17 you may see:
FaceServiceClient faceServiceClient = new FaceServiceClient("<YOUR API KEY HERE>", "<YOUR API ENDPOINT HERE>");
<YOUR API KEY HERE>
should be replaced with the API Key that you got from the Azure Portal. <YOUR ENDPOINT HERE>
should be replaced with one of the following examples from this API Documentation link. The format should be similar to:
"https://<LOCATION>/face/v1.0"
where <LOCATION>
should be replaced with something like uksouth.api.cognitive.microsoft.com
or japaneast.api.cognitive.microsoft.com
. All of these can be found, listed at this link.
Now that you have gotten your API Key and Endpoint and used them as the constructors of FaceServiceClient
on Line 17, you have to specify the directories of the images for the people that will be identified. The identifyFace()
function accepts a String as a parameter with the directory of an image containing a face, but the other functions, such as addIndividual
and definePeople
, require you to change the directory in the function itself.
Where it says:
@"<Directory with images of person>" //Line 40.
//OR:
@"<Directory with images of Person #>" //Lines 122, 139, 151.
replace the <Directory with images of person>
with the directory of an images folder on your computer. Images have been provided in this repository which you can download to test the Console Application. Once you have replaced <Directory with images of Person 1
with the actual directories of images, you can run the functions one by one, starting with definePeople()
to:
- add people to Person Groups
- train the Person Groups
- and identify people
While this is a simple Console Application that can be used for facial identification, I also have repositories for Android apps that use the Microsoft Face API for facial analysis and Kairos's API to do facial recognition. Below is a list of some of those repositories:
- Face API - Identification in Android: A closely-related repository for an Android app that also uses the Microsoft Face API for facial detection and identification. README
- Face Analyzer: The purpose of this Android app is to utilize the Microsoft Face API to not only detect individual faces in an image, but also for each face provide information such as emotions, the estimated age, gender, and more. README
- Kairos Face Recognition: The purpose of this Android app is to use Kairos's SDK for Android in order to implement facial recognition. Features of this app include: registering and identifying users when given an image. README