Delpin Susai Raj Wednesday, 23 May 2018

Xamarin.Forms - Computer Vision API using Cognitive Service

In this blog, you will learn how to use Computer Vision API using Cognitive Service in Xamarin.Forms




Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the 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
  1. Vision
  2. Speech
  3. Knowledge
  4. Search
  5. Language
Because the Cognitive Services APIs harness the power of machine learning, we were able to bring advanced intelligence into our product without the need to have a team of data scientists on hand.

For more information


Prerequisites
  • Visual Studio 2017(Windows or Mac)
  • Vision API Key
Download code here

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  Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.



Now Select the Blank App and Choose Portable Class Library(PCL).



Subsequently, go to the solution. In there, you get all the files and sources of your project (PCL). Now, select XAML page and double-click to open the MainPage.Xaml page.
You now have a basic Xamarin.Forms app. Click the Play button to try it out.



Get Computer Vision API Key

In this step, get computer vision 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 Computer Vision under  Vision APIs. Afterward, click "Get API Key".



Read the terms, and select your country/region. Afterward, click "Next".



Now, Login using your preferred account.



Now, the API key is activated.you can use now.



The trial key is available only 7 days. if you want permanent key refer to the following article

https://www.c-sharpcorner.com/article/getting-start-with-microsoft-azure-cognitive-services-computer-vision-api/

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:XamarinComputerVision" x:Class="XamarinComputerVision.MainPage">
<ContentPage.Content>
<ScrollView>
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="StartAndExpand">
<Image x:Name="imgBanner"></Image>
<Image x:Name="imgChoosed" HeightRequest="200"></Image>
<Button x:Name="btnPick" Text="Pick" Clicked="btnPick_Clicked"></Button>
<Button x:Name="btnTake" Text="Take" Clicked="btnTake_Clicked"></Button>
<Label Text="Result" x:Name="lblResult"></Label> </StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
view raw MainPage.xaml hosted with ❤ by GitHub

Click the Play button to try it out.



NuGet Packages

Now, add following NuGet Packages.
  1. Xam.Plugin.Media
  2. Microsoft.ProjectOxford.Vision
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution".

Xam.Plugin.Media



Microsoft.ProjectOxford.Vision



Permissions - for Android

In this step give required permissions to your app. the following permissions must for this app.


  1. CAMERA
  2. READ_EXTERNAL_STORAGE
  3. WRITE_EXTERNAL_STORAGE




AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.XamarinComputerVision" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="XamarinComputerVision.Android">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.XamarinComputerVision.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
In this step, create an XML file under Resource-->xml folder for get file paths.
Go to Solution—>Android —>Right click—>New—>Xml—> file_paths.xml



Now, write the following code to get file paths.


file_paths.xml

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
view raw file_paths.xml hosted with ❤ by GitHub
Analyze Image

Now, write the following code to analyze image using vision API Cognitive Service.


MainPage.xaml.cs


public async Task<AnalysisResult> GetImageDescription(Stream imageStream)
{
VisionServiceClient visionClient = new VisionServiceClient("a338648c0df347c6b3b9e46ea2022fcd", "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0");
VisualFeature[] features = { VisualFeature.Tags, VisualFeature.Categories, VisualFeature.Description };
return await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null);
}



Pick  Image

Now, write the following code to pick an image from your device.



MainPage.xaml.cs

private async void btnPick_Clicked(object sender, EventArgs e) {
await CrossMedia.Current.Initialize();
try {
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions {
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium
});
if (file == null) return;
imgChoosed.Source = ImageSource.FromStream(() => {
var stream = file.GetStream();
return stream;
});
var result = await GetImageDescription(file.GetStream());
lblResult.Text = null;
file.Dispose();
foreach(string tag in result.Description.Tags) {
lblResult.Text = lblResult.Text + "\n" + tag;
}
} catch (Exception ex) {
string test = ex.Message;
}
}



Take  Image

Now, write the following code to Take the image using the camera.



MainPage.xaml.cs

private async void btnTake_Clicked(object sender, EventArgs e) {
await CrossMedia.Current.Initialize();
try {
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) {
await DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions {
Directory = "Sample",
Name = "xamarin.jpg"
});
if (file == null) return;
imgChoosed.Source = ImageSource.FromStream(() => {
var stream = file.GetStream();
return stream;
});
var result = await GetImageDescription(file.GetStream());
file.Dispose();
lblResult.Text = null;
//lblResult.Text = result.Description.Captions.First().Text;
foreach(string tag in result.Description.Tags) {
lblResult.Text = lblResult.Text + "\n" + tag;
}
} catch (Exception ex) {
string test = ex.Message;
}
}




Full Code - MainPage.Xaml.cs

MainPage.Xaml.cs

using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using Plugin.Media;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinComputerVision {
public partial class MainPage: ContentPage {
public MainPage() {
InitializeComponent();
imgBanner.Source = ImageSource.FromResource("XamarinComputerVision.images.banner.png");
imgChoosed.Source = ImageSource.FromResource("XamarinComputerVision.images.thumbnail.jpg");
}
private async void btnPick_Clicked(object sender, EventArgs e) {
await CrossMedia.Current.Initialize();
try {
var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions {
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium
});
if (file == null) return;
imgChoosed.Source = ImageSource.FromStream(() => {
var stream = file.GetStream();
return stream;
});
var result = await GetImageDescription(file.GetStream());
lblResult.Text = null;
file.Dispose();
foreach(string tag in result.Description.Tags) {
lblResult.Text = lblResult.Text + "\n" + tag;
}
} catch (Exception ex) {
string test = ex.Message;
}
}
public async Task < AnalysisResult > GetImageDescription(Stream imageStream) {
VisionServiceClient visionClient = new VisionServiceClient("a338648c0df347c6b3b9e46ea2022fcd", "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0");
VisualFeature[] features = {
VisualFeature.Tags,
VisualFeature.Categories,
VisualFeature.Description
};
return await visionClient.AnalyzeImageAsync(imageStream, features.ToList(), null);
}
private async void btnTake_Clicked(object sender, EventArgs e) {
await CrossMedia.Current.Initialize();
try {
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) {
await DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions {
Directory = "Sample",
Name = "xamarin.jpg"
});
if (file == null) return;
imgChoosed.Source = ImageSource.FromStream(() => {
var stream = file.GetStream();
return stream;
});
var result = await GetImageDescription(file.GetStream());
file.Dispose();
lblResult.Text = null;
//lblResult.Text = result.Description.Captions.First().Text;
foreach(string tag in result.Description.Tags) {
lblResult.Text = lblResult.Text + "\n" + tag;
}
} catch (Exception ex) {
string test = ex.Message;
}
}
}
}
Click the Play button to try it out.



I hope you have understood how to use Computer Vision API using Cognitive Service in Xamarin.Forms.

Thanks for reading. Please share comments and feedback.

1 comment:

  1. Thanks for sharing this informative content , Great work
    Leanpitch provides online training inScrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training

    ReplyDelete