Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
Cognitive Services

Infuse your apps, websites, and bots with intelligent algorithms to see, hear, speak, understand and interpret your user needs through natural methods of communication. Transform your business with AI today.
Use AI to solve business problems
- Vision
- Speech
- Knowledge
- Search
- Language
Face API
- Face API provides developers with access to advanced face algorithms. Microsoft Face algorithms enable face attribute detection and face recognition.
- Face API is a cognitive service that provides algorithms for detecting, recognizing, and analyzing human faces in images.
- Face API can detect human faces in an image and return the rectangle coordinates of their locations. Optionally, face detection can extract a series of face-related attributes such as pose, gender, age, head pose, facial hair, and glasses.
Prerequisites
- Visual Studio 2017(Windows or Mac)
- Face API Key
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. you’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.

Name your app, select “Use .NET Standard” for shared code, and target both Android and iOS.

You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.

You now have a basic Xamarin.Forms app. Click the play button to try it out.

Get Face API Key
In this step, get Face API Key. Go to the following link.
https://azure.microsoft.com/en-in/services/cognitive-services/
Click "Try Cognitive Services for free".

Now, you can choose Face under Vision APIs. Afterward, click "Get API Key".
Read the terms, and select your country/region. Afterward, click "Next".

Now, log in using your preferred account.
Now, the API Key is activated. You can use it now.
The trial key is available only 7 days. If you want a permanent key, refer to the following article.
Setting up the User Interface
Go to MainPage.Xaml and write the following code.
MainPage.xaml
- <?xml version="1.0" encoding="utf-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinCognitive" x:Class="XamarinCognitive.MainPage">
- <StackLayout>
- <StackLayout>
- <StackLayout HorizontalOptions="Center" VerticalOptions="Start">
- <Image Margin="0,50,0,0" x:Name="imgBanner" Source="banner.png" ></Image>
- <Image Margin="0,0,0,10" x:Name="imgEmail" HeightRequest="100" Source="cognitiveservice.png" ></Image>
- <Label Margin="0,0,0,10" Text="Emotion Recognition" FontAttributes="Bold" FontSize="Large" TextColor="Gray" HorizontalTextAlignment="Center" ></Label>
- <Image Margin="0,0,0,10" x:Name="imgSelected" HeightRequest="150" Source="defaultimage.png" ></Image>
- <Button x:Name="btnPick" Text="Pick" Clicked="btnPick_Clicked" />
- <StackLayout HorizontalOptions="CenterAndExpand" Margin="10,0,0,10">
- <Label x:Name="lblTotalFace" ></Label>
- <Label x:Name="lblGender"></Label>
- <Label x:Name="lblAge"></Label>
- </StackLayout>
- </StackLayout>
- </StackLayout>
- </StackLayout>
- </ContentPage>
Click the Play button to try it out.

NuGet Packages
Now, add the following NuGet Packages.
- Xam.Plugin.Media
- Newtonsoft.Json
Add Xam.Plugin.Media NuGet
In this step, add Xam.Plugin.Media to your project. You can install Xam.Plugin.Media via NuGet or you can browse the source code on GitHub.
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xam.Plugin.Media" and add Package. Remember to install it for each project (PCL, Android, iOS, and UWP).

Permissions
In this step, give the following required permissions to your app.
Permissions - for Android
- CAMERA
- READ_EXTERNAL_STORAGE
- WRITE_EXTERNAL_STORAGE
Permissions - for iOS
- NSCameraUsageDescription
- NSPhotoLibraryUsageDescription
- NSMicrophoneUsageDescription
- NSPhotoLibraryAddUsageDescription
Create a Model
In this step, you can create a model for deserializing your response.
ResponseModel.cs

Devangini DubeyPosted Apr 12, 2021, 11:45 AM
Hi, I am facing error " No face detected " in xamarin iOS. Its working fine in xamarin android but issue in Xamarin iOS.Can any one help for this?
Chittaranjan SwainPosted Oct 22, 2019, 10:25 PM
Nice article....
User GPosted Sep 13, 2019, 12:04 AM
Is there any way to use this for authentication purpose? I want to implement face recognition authentication for my android app.(it should be the same one which is used for unlocking the phone)
Ahsan SiddiquePosted Dec 6, 2018, 3:28 AM
Good to see the simple example of cognitive services in Xamarin.
Ahsan SiddiquePosted Dec 6, 2018, 3:26 AM
Where 33 faces in this picture??
Alagunila MeganathanPosted Dec 5, 2018, 10:33 PM
Good Article pa...
Rushi MehtaPosted Dec 5, 2018, 9:42 PM
Very Nicely explained article